Няма описание

XliffFileLoaderTest.php 6.9KB

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