PoFileLoaderTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\PoFileLoader;
  12. use Symfony\Component\Config\Resource\FileResource;
  13. class PoFileLoaderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testLoad()
  16. {
  17. $loader = new PoFileLoader();
  18. $resource = __DIR__.'/../fixtures/resources.po';
  19. $catalogue = $loader->load($resource, 'en', 'domain1');
  20. $this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
  21. $this->assertEquals('en', $catalogue->getLocale());
  22. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  23. }
  24. public function testLoadPlurals()
  25. {
  26. $loader = new PoFileLoader();
  27. $resource = __DIR__.'/../fixtures/plurals.po';
  28. $catalogue = $loader->load($resource, 'en', 'domain1');
  29. $this->assertEquals(array('foo' => 'bar', 'foos' => 'bar|bars'), $catalogue->all('domain1'));
  30. $this->assertEquals('en', $catalogue->getLocale());
  31. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  32. }
  33. public function testLoadDoesNothingIfEmpty()
  34. {
  35. $loader = new PoFileLoader();
  36. $resource = __DIR__.'/../fixtures/empty.po';
  37. $catalogue = $loader->load($resource, 'en', 'domain1');
  38. $this->assertEquals(array(), $catalogue->all('domain1'));
  39. $this->assertEquals('en', $catalogue->getLocale());
  40. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  41. }
  42. /**
  43. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
  44. */
  45. public function testLoadNonExistingResource()
  46. {
  47. $loader = new PoFileLoader();
  48. $resource = __DIR__.'/../fixtures/non-existing.po';
  49. $loader->load($resource, 'en', 'domain1');
  50. }
  51. public function testLoadEmptyTranslation()
  52. {
  53. $loader = new PoFileLoader();
  54. $resource = __DIR__.'/../fixtures/empty-translation.po';
  55. $catalogue = $loader->load($resource, 'en', 'domain1');
  56. $this->assertEquals(array('foo' => ''), $catalogue->all('domain1'));
  57. $this->assertEquals('en', $catalogue->getLocale());
  58. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  59. }
  60. public function testEscapedId()
  61. {
  62. $loader = new PoFileLoader();
  63. $resource = __DIR__.'/../fixtures/escaped-id.po';
  64. $catalogue = $loader->load($resource, 'en', 'domain1');
  65. $messages = $catalogue->all('domain1');
  66. $this->assertArrayHasKey('escaped "foo"', $messages);
  67. $this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
  68. }
  69. public function testEscapedIdPlurals()
  70. {
  71. $loader = new PoFileLoader();
  72. $resource = __DIR__.'/../fixtures/escaped-id-plurals.po';
  73. $catalogue = $loader->load($resource, 'en', 'domain1');
  74. $messages = $catalogue->all('domain1');
  75. $this->assertArrayHasKey('escaped "foo"', $messages);
  76. $this->assertArrayHasKey('escaped "foos"', $messages);
  77. $this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
  78. $this->assertEquals('escaped "bar"|escaped "bars"', $messages['escaped "foos"']);
  79. }
  80. public function testSkipFuzzyTranslations()
  81. {
  82. $loader = new PoFileLoader();
  83. $resource = __DIR__.'/../fixtures/fuzzy-translations.po';
  84. $catalogue = $loader->load($resource, 'en', 'domain1');
  85. $messages = $catalogue->all('domain1');
  86. $this->assertArrayHasKey('foo1', $messages);
  87. $this->assertArrayNotHasKey('foo2', $messages);
  88. $this->assertArrayHasKey('foo3', $messages);
  89. }
  90. }