FileDumperTest.php 2.3 KB

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