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