FileDumperTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Dumper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Translation\Dumper\FileDumper;
  13. use Symfony\Component\Translation\MessageCatalogue;
  14. class FileDumperTest extends TestCase
  15. {
  16. public function testDump()
  17. {
  18. $tempDir = sys_get_temp_dir();
  19. $catalogue = new MessageCatalogue('en');
  20. $catalogue->add(['foo' => 'bar']);
  21. $dumper = new ConcreteFileDumper();
  22. $dumper->dump($catalogue, ['path' => $tempDir]);
  23. $this->assertFileExists($tempDir.'/messages.en.concrete');
  24. @unlink($tempDir.'/messages.en.concrete');
  25. }
  26. public function testDumpIntl()
  27. {
  28. $tempDir = sys_get_temp_dir();
  29. $catalogue = new MessageCatalogue('en');
  30. $catalogue->add(['foo' => 'bar'], 'd1');
  31. $catalogue->add(['bar' => 'foo'], 'd1+intl-icu');
  32. $catalogue->add(['bar' => 'foo'], 'd2+intl-icu');
  33. $dumper = new ConcreteFileDumper();
  34. @unlink($tempDir.'/d2.en.concrete');
  35. $dumper->dump($catalogue, ['path' => $tempDir]);
  36. $this->assertStringEqualsFile($tempDir.'/d1.en.concrete', 'foo=bar');
  37. @unlink($tempDir.'/d1.en.concrete');
  38. $this->assertStringEqualsFile($tempDir.'/d1+intl-icu.en.concrete', 'bar=foo');
  39. @unlink($tempDir.'/d1+intl-icu.en.concrete');
  40. $this->assertFileNotExists($tempDir.'/d2.en.concrete');
  41. $this->assertStringEqualsFile($tempDir.'/d2+intl-icu.en.concrete', 'bar=foo');
  42. @unlink($tempDir.'/d2+intl-icu.en.concrete');
  43. }
  44. public function testDumpCreatesNestedDirectoriesAndFile()
  45. {
  46. $tempDir = sys_get_temp_dir();
  47. $translationsDir = $tempDir.'/test/translations';
  48. $file = $translationsDir.'/messages.en.concrete';
  49. $catalogue = new MessageCatalogue('en');
  50. $catalogue->add(['foo' => 'bar']);
  51. $dumper = new ConcreteFileDumper();
  52. $dumper->setRelativePathTemplate('test/translations/%domain%.%locale%.%extension%');
  53. $dumper->dump($catalogue, ['path' => $tempDir]);
  54. $this->assertFileExists($file);
  55. @unlink($file);
  56. @rmdir($translationsDir);
  57. }
  58. }
  59. class ConcreteFileDumper extends FileDumper
  60. {
  61. public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
  62. {
  63. return http_build_query($messages->all($domain), '', '&');
  64. }
  65. protected function getExtension()
  66. {
  67. return 'concrete';
  68. }
  69. }