XliffFileLoaderTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. protected function setUp()
  16. {
  17. if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
  18. $this->markTestSkipped('The "Config" component is not available');
  19. }
  20. }
  21. public function testLoad()
  22. {
  23. $loader = new XliffFileLoader();
  24. $resource = __DIR__.'/../fixtures/resources.xlf';
  25. $catalogue = $loader->load($resource, 'en', 'domain1');
  26. $this->assertEquals('en', $catalogue->getLocale());
  27. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  28. }
  29. public function testLoadWithResname()
  30. {
  31. $loader = new XliffFileLoader();
  32. $catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1');
  33. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $catalogue->all('domain1'));
  34. }
  35. public function testIncompleteResource()
  36. {
  37. $loader = new XliffFileLoader();
  38. $catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1');
  39. $this->assertEquals(array('foo' => 'bar', 'key' => '', 'test' => 'with'), $catalogue->all('domain1'));
  40. $this->assertFalse($catalogue->has('extra', 'domain1'));
  41. }
  42. public function testEncoding()
  43. {
  44. if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
  45. $this->markTestSkipped('The iconv and mbstring extensions are not available.');
  46. }
  47. $loader = new XliffFileLoader();
  48. $catalogue = $loader->load(__DIR__.'/../fixtures/encoding.xlf', 'en', 'domain1');
  49. $this->assertEquals(utf8_decode('föö'), $catalogue->get('bar', 'domain1'));
  50. $this->assertEquals(utf8_decode('bär'), $catalogue->get('foo', 'domain1'));
  51. }
  52. /**
  53. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  54. */
  55. public function testLoadInvalidResource()
  56. {
  57. $loader = new XliffFileLoader();
  58. $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1');
  59. }
  60. /**
  61. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  62. */
  63. public function testLoadResourceDoesNotValidate()
  64. {
  65. $loader = new XliffFileLoader();
  66. $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1');
  67. }
  68. /**
  69. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
  70. */
  71. public function testLoadNonExistingResource()
  72. {
  73. $loader = new XliffFileLoader();
  74. $resource = __DIR__.'/../fixtures/non-existing.xlf';
  75. $loader->load($resource, 'en', 'domain1');
  76. }
  77. /**
  78. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  79. */
  80. public function testLoadThrowsAnExceptionIfFileNotLocal()
  81. {
  82. $loader = new XliffFileLoader();
  83. $resource = 'http://example.com/resources.xlf';
  84. $loader->load($resource, 'en', 'domain1');
  85. }
  86. /**
  87. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  88. * @expectedExceptionMessage Document types are not allowed.
  89. */
  90. public function testDocTypeIsNotAllowed()
  91. {
  92. $loader = new XliffFileLoader();
  93. $loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1');
  94. }
  95. }