No Description

XliffFileLoaderTest.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. }
  24. public function testLoadWithInternalErrorsEnabled()
  25. {
  26. libxml_use_internal_errors(true);
  27. $this->assertSame(array(), libxml_get_errors());
  28. $loader = new XliffFileLoader();
  29. $resource = __DIR__.'/../fixtures/resources.xlf';
  30. $catalogue = $loader->load($resource, 'en', 'domain1');
  31. $this->assertEquals('en', $catalogue->getLocale());
  32. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  33. $this->assertSame(array(), libxml_get_errors());
  34. }
  35. public function testLoadWithResname()
  36. {
  37. $loader = new XliffFileLoader();
  38. $catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1');
  39. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $catalogue->all('domain1'));
  40. }
  41. public function testIncompleteResource()
  42. {
  43. $loader = new XliffFileLoader();
  44. $catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1');
  45. $this->assertEquals(array('foo' => 'bar', 'key' => '', 'test' => 'with'), $catalogue->all('domain1'));
  46. $this->assertFalse($catalogue->has('extra', 'domain1'));
  47. }
  48. public function testEncoding()
  49. {
  50. if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
  51. $this->markTestSkipped('The iconv and mbstring extensions are not available.');
  52. }
  53. $loader = new XliffFileLoader();
  54. $catalogue = $loader->load(__DIR__.'/../fixtures/encoding.xlf', 'en', 'domain1');
  55. $this->assertEquals(utf8_decode('föö'), $catalogue->get('bar', 'domain1'));
  56. $this->assertEquals(utf8_decode('bär'), $catalogue->get('foo', 'domain1'));
  57. $this->assertEquals(array('notes' => array(array('content' => utf8_decode('bäz')))), $catalogue->getMetadata('foo', 'domain1'));
  58. }
  59. /**
  60. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  61. */
  62. public function testLoadInvalidResource()
  63. {
  64. $loader = new XliffFileLoader();
  65. $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1');
  66. }
  67. /**
  68. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  69. */
  70. public function testLoadResourceDoesNotValidate()
  71. {
  72. $loader = new XliffFileLoader();
  73. $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1');
  74. }
  75. /**
  76. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
  77. */
  78. public function testLoadNonExistingResource()
  79. {
  80. $loader = new XliffFileLoader();
  81. $resource = __DIR__.'/../fixtures/non-existing.xlf';
  82. $loader->load($resource, 'en', 'domain1');
  83. }
  84. /**
  85. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  86. */
  87. public function testLoadThrowsAnExceptionIfFileNotLocal()
  88. {
  89. $loader = new XliffFileLoader();
  90. $resource = 'http://example.com/resources.xlf';
  91. $loader->load($resource, 'en', 'domain1');
  92. }
  93. /**
  94. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  95. * @expectedExceptionMessage Document types are not allowed.
  96. */
  97. public function testDocTypeIsNotAllowed()
  98. {
  99. $loader = new XliffFileLoader();
  100. $loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1');
  101. }
  102. public function testParseEmptyFile()
  103. {
  104. $loader = new XliffFileLoader();
  105. $resource = __DIR__.'/../fixtures/empty.xlf';
  106. $this->setExpectedException('Symfony\Component\Translation\Exception\InvalidResourceException', sprintf('Unable to load "%s":', $resource));
  107. $loader->load($resource, 'en', 'domain1');
  108. }
  109. public function testLoadNotes()
  110. {
  111. $loader = new XliffFileLoader();
  112. $catalogue = $loader->load(__DIR__.'/../fixtures/withnote.xlf', 'en', 'domain1');
  113. $this->assertEquals(array('notes' => array(array('priority' => 1, 'content' => 'foo'))), $catalogue->getMetadata('foo', 'domain1'));
  114. // message without target
  115. $this->assertNull($catalogue->getMetadata('extra', 'domain1'));
  116. $this->assertEquals(array('notes' => array(array('content' => 'baz'), array('priority' => 2, 'from' => 'bar', 'content' => 'qux'))), $catalogue->getMetadata('key', 'domain1'));
  117. }
  118. }