FileDumperTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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(array('foo' => 'bar'));
  21. $dumper = new ConcreteFileDumper();
  22. $dumper->dump($catalogue, array('path' => $tempDir));
  23. $this->assertFileExists($tempDir.'/messages.en.concrete');
  24. }
  25. public function testDumpBackupsFileIfExisting()
  26. {
  27. $tempDir = sys_get_temp_dir();
  28. $file = $tempDir.'/messages.en.concrete';
  29. $backupFile = $file.'~';
  30. @touch($file);
  31. $catalogue = new MessageCatalogue('en');
  32. $catalogue->add(array('foo' => 'bar'));
  33. $dumper = new ConcreteFileDumper();
  34. $dumper->dump($catalogue, array('path' => $tempDir));
  35. $this->assertFileExists($backupFile);
  36. @unlink($file);
  37. @unlink($backupFile);
  38. }
  39. public function testDumpCreatesNestedDirectoriesAndFile()
  40. {
  41. $tempDir = sys_get_temp_dir();
  42. $translationsDir = $tempDir.'/test/translations';
  43. $file = $translationsDir.'/messages.en.concrete';
  44. $catalogue = new MessageCatalogue('en');
  45. $catalogue->add(array('foo' => 'bar'));
  46. $dumper = new ConcreteFileDumper();
  47. $dumper->setRelativePathTemplate('test/translations/%domain%.%locale%.%extension%');
  48. $dumper->dump($catalogue, array('path' => $tempDir));
  49. $this->assertFileExists($file);
  50. @unlink($file);
  51. @rmdir($translationsDir);
  52. }
  53. }
  54. class ConcreteFileDumper extends FileDumper
  55. {
  56. public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
  57. {
  58. return '';
  59. }
  60. protected function getExtension()
  61. {
  62. return 'concrete';
  63. }
  64. }