SymfonyComponentTranslationTestsLoaderXliffFileLoaderTest

The Simple Calendar Symfony Component Translation Tests Loader XliffFileLoaderTest class.

Defined (1)

The class is defined in the following location(s).

/vendor/symfony/translation/Tests/Loader/XliffFileLoaderTest.php  
  1. class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase 
  2. public function testLoad() 
  3. $loader = new XliffFileLoader(); 
  4. $resource = __DIR__.'/../fixtures/resources.xlf'; 
  5. $catalogue = $loader->load($resource, 'en', 'domain1'); 
  6.  
  7. $this->assertEquals('en', $catalogue->getLocale()); 
  8. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); 
  9. $this->assertSame(array(), libxml_get_errors()); 
  10. $this->assertContainsOnly('string', $catalogue->all('domain1')); 
  11.  
  12. public function testLoadWithInternalErrorsEnabled() 
  13. $internalErrors = libxml_use_internal_errors(true); 
  14.  
  15. $this->assertSame(array(), libxml_get_errors()); 
  16.  
  17. $loader = new XliffFileLoader(); 
  18. $resource = __DIR__.'/../fixtures/resources.xlf'; 
  19. $catalogue = $loader->load($resource, 'en', 'domain1'); 
  20.  
  21. $this->assertEquals('en', $catalogue->getLocale()); 
  22. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); 
  23. $this->assertSame(array(), libxml_get_errors()); 
  24.  
  25. libxml_clear_errors(); 
  26. libxml_use_internal_errors($internalErrors); 
  27.  
  28. public function testLoadWithExternalEntitiesDisabled() 
  29. $disableEntities = libxml_disable_entity_loader(true); 
  30.  
  31. $loader = new XliffFileLoader(); 
  32. $resource = __DIR__.'/../fixtures/resources.xlf'; 
  33. $catalogue = $loader->load($resource, 'en', 'domain1'); 
  34.  
  35. libxml_disable_entity_loader($disableEntities); 
  36.  
  37. $this->assertEquals('en', $catalogue->getLocale()); 
  38. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); 
  39.  
  40. public function testLoadWithResname() 
  41. $loader = new XliffFileLoader(); 
  42. $catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1'); 
  43.  
  44. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $catalogue->all('domain1')); 
  45.  
  46. public function testIncompleteResource() 
  47. $loader = new XliffFileLoader(); 
  48. $catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1'); 
  49.  
  50. $this->assertEquals(array('foo' => 'bar', 'extra' => 'extra', 'key' => '', 'test' => 'with'), $catalogue->all('domain1')); 
  51.  
  52. public function testEncoding() 
  53. $loader = new XliffFileLoader(); 
  54. $catalogue = $loader->load(__DIR__.'/../fixtures/encoding.xlf', 'en', 'domain1'); 
  55.  
  56. $this->assertEquals(utf8_decode('f'), $catalogue->get('bar', 'domain1')); 
  57. $this->assertEquals(utf8_decode('br'), $catalogue->get('foo', 'domain1')); 
  58. $this->assertEquals(array('notes' => array(array('content' => utf8_decode('bz'))), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1')); 
  59.  
  60. public function testTargetAttributesAreStoredCorrectly() 
  61. $loader = new XliffFileLoader(); 
  62. $catalogue = $loader->load(__DIR__.'/../fixtures/with-attributes.xlf', 'en', 'domain1'); 
  63.  
  64. $metadata = $catalogue->getMetadata('foo', 'domain1'); 
  65. $this->assertEquals('translated', $metadata['target-attributes']['state']); 
  66.  
  67. /** 
  68. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException 
  69. */ 
  70. public function testLoadInvalidResource() 
  71. $loader = new XliffFileLoader(); 
  72. $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1'); 
  73.  
  74. /** 
  75. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException 
  76. */ 
  77. public function testLoadResourceDoesNotValidate() 
  78. $loader = new XliffFileLoader(); 
  79. $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1'); 
  80.  
  81. /** 
  82. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException 
  83. */ 
  84. public function testLoadNonExistingResource() 
  85. $loader = new XliffFileLoader(); 
  86. $resource = __DIR__.'/../fixtures/non-existing.xlf'; 
  87. $loader->load($resource, 'en', 'domain1'); 
  88.  
  89. /** 
  90. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException 
  91. */ 
  92. public function testLoadThrowsAnExceptionIfFileNotLocal() 
  93. $loader = new XliffFileLoader(); 
  94. $resource = 'http://example.com/resources.xlf'; 
  95. $loader->load($resource, 'en', 'domain1'); 
  96.  
  97. /** 
  98. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException 
  99. * @expectedExceptionMessage Document types are not allowed. 
  100. */ 
  101. public function testDocTypeIsNotAllowed() 
  102. $loader = new XliffFileLoader(); 
  103. $loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1'); 
  104.  
  105. public function testParseEmptyFile() 
  106. $loader = new XliffFileLoader(); 
  107. $resource = __DIR__.'/../fixtures/empty.xlf'; 
  108. $this->setExpectedException('Symfony\Component\Translation\Exception\InvalidResourceException', sprintf('Unable to load "%s":', $resource)); 
  109. $loader->load($resource, 'en', 'domain1'); 
  110.  
  111. public function testLoadNotes() 
  112. $loader = new XliffFileLoader(); 
  113. $catalogue = $loader->load(__DIR__.'/../fixtures/withnote.xlf', 'en', 'domain1'); 
  114.  
  115. $this->assertEquals(array('notes' => array(array('priority' => 1, 'content' => 'foo')), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1')); 
  116. // message without target 
  117. $this->assertEquals(array('notes' => array(array('content' => 'bar', 'from' => 'foo')), 'id' => '2'), $catalogue->getMetadata('extra', 'domain1')); 
  118. // message with empty target 
  119. $this->assertEquals(array('notes' => array(array('content' => 'baz'), array('priority' => 2, 'from' => 'bar', 'content' => 'qux')), 'id' => '123'), $catalogue->getMetadata('key', 'domain1')); 
  120.  
  121. public function testLoadVersion2() 
  122. $loader = new XliffFileLoader(); 
  123. $resource = __DIR__.'/../fixtures/resources-2.0.xlf'; 
  124. $catalogue = $loader->load($resource, 'en', 'domain1'); 
  125.  
  126. $this->assertEquals('en', $catalogue->getLocale()); 
  127. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); 
  128. $this->assertSame(array(), libxml_get_errors()); 
  129.  
  130. $domains = $catalogue->all(); 
  131. $this->assertCount(3, $domains['domain1']); 
  132. $this->assertContainsOnly('string', $catalogue->all('domain1')); 
  133.  
  134. // target attributes 
  135. $this->assertEquals(array('target-attributes' => array('order' => 1)), $catalogue->getMetadata('bar', 'domain1'));