FileDumperTest.php 2.2 KB

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