TextBundleWriterTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Intl\Tests\Data\Bundle\Writer;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. use Symfony\Component\Intl\Data\Bundle\Writer\TextBundleWriter;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. *
  17. * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
  18. */
  19. class TextBundleWriterTest extends TestCase
  20. {
  21. /**
  22. * @var TextBundleWriter
  23. */
  24. private $writer;
  25. private $directory;
  26. /**
  27. * @var Filesystem
  28. */
  29. private $filesystem;
  30. protected function setUp()
  31. {
  32. $this->writer = new TextBundleWriter();
  33. $this->directory = sys_get_temp_dir().'/TextBundleWriterTest/'.mt_rand(1000, 9999);
  34. $this->filesystem = new Filesystem();
  35. $this->filesystem->mkdir($this->directory);
  36. }
  37. protected function tearDown()
  38. {
  39. $this->filesystem->remove($this->directory);
  40. }
  41. public function testWrite()
  42. {
  43. $this->writer->write($this->directory, 'en', array(
  44. 'Entry1' => array(
  45. 'Array' => array('foo', 'bar', array('Key' => 'value')),
  46. 'Integer' => 5,
  47. 'IntVector' => array(0, 1, 2, 3),
  48. 'NotAnIntVector' => array(0 => 0, 2 => 1, 1 => 2, 3 => 3),
  49. 'IntVectorWithStringKeys' => array('a' => 0, 'b' => 1, 'c' => 2),
  50. 'TableWithIntKeys' => array(0 => 0, 1 => 1, 3 => 3),
  51. 'FalseBoolean' => false,
  52. 'TrueBoolean' => true,
  53. 'Null' => null,
  54. 'Float' => 1.23,
  55. ),
  56. 'Entry2' => 'String',
  57. ));
  58. $this->assertFileEquals(__DIR__.'/Fixtures/en.txt', $this->directory.'/en.txt');
  59. }
  60. public function testWriteTraversable()
  61. {
  62. $this->writer->write($this->directory, 'en', new \ArrayIterator(array(
  63. 'Entry1' => new \ArrayIterator(array(
  64. 'Array' => array('foo', 'bar', array('Key' => 'value')),
  65. 'Integer' => 5,
  66. 'IntVector' => array(0, 1, 2, 3),
  67. 'NotAnIntVector' => array(0 => 0, 2 => 1, 1 => 2, 3 => 3),
  68. 'IntVectorWithStringKeys' => array('a' => 0, 'b' => 1, 'c' => 2),
  69. 'TableWithIntKeys' => array(0 => 0, 1 => 1, 3 => 3),
  70. 'FalseBoolean' => false,
  71. 'TrueBoolean' => true,
  72. 'Null' => null,
  73. 'Float' => 1.23,
  74. )),
  75. 'Entry2' => 'String',
  76. )));
  77. $this->assertFileEquals(__DIR__.'/Fixtures/en.txt', $this->directory.'/en.txt');
  78. }
  79. public function testWriteNoFallback()
  80. {
  81. $data = array(
  82. 'Entry' => 'Value',
  83. );
  84. $this->writer->write($this->directory, 'en_nofallback', $data, $fallback = false);
  85. $this->assertFileEquals(__DIR__.'/Fixtures/en_nofallback.txt', $this->directory.'/en_nofallback.txt');
  86. }
  87. public function testEscapeKeysIfNecessary()
  88. {
  89. $this->writer->write($this->directory, 'escaped', array(
  90. // Keys with colons must be escaped, otherwise the part after the
  91. // colon is interpreted as resource type
  92. 'EntryWith:Colon' => 'Value',
  93. // Keys with spaces must be escaped
  94. 'Entry With Spaces' => 'Value',
  95. ));
  96. $this->assertFileEquals(__DIR__.'/Fixtures/escaped.txt', $this->directory.'/escaped.txt');
  97. }
  98. }