/Tests/Util/FileUtilsTest.php

https://github.com/schmittjoh/JMSTranslationBundle · PHP · 138 lines · 89 code · 24 blank · 25 comment · 1 complexity · bf321377d61d697247548cdbff3c5928 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. namespace JMS\TranslationBundle\Tests\Util;
  19. use JMS\TranslationBundle\Util\FileUtils;
  20. use PHPUnit\Framework\TestCase;
  21. use Symfony\Component\Filesystem\Filesystem;
  22. use Symfony\Component\Finder\SplFileInfo;
  23. class FileUtilsTest extends TestCase
  24. {
  25. /**
  26. * @var string
  27. */
  28. private $translationDirectory;
  29. /**
  30. * @var Filesystem
  31. */
  32. private $fileSystem;
  33. public function __construct($name = null, array $data = [], $dataName = '')
  34. {
  35. parent::__construct($name, $data, $dataName);
  36. $this->fileSystem = new Filesystem();
  37. }
  38. public function testAnEmptyDirectoryReturnsNoFiles(): void
  39. {
  40. $files = FileUtils::findTranslationFiles($this->translationDirectory);
  41. $this->assertEquals([], $files);
  42. }
  43. public function testNestedDirectoriesAreIgnored(): void
  44. {
  45. $nestedDirectory = $this->translationDirectory . '/nested';
  46. mkdir($nestedDirectory);
  47. touch($nestedDirectory . '/messages.en.xliff');
  48. $files = FileUtils::findTranslationFiles($this->translationDirectory);
  49. $this->assertEquals([], $files);
  50. }
  51. public function testOnlyTranslationFilesArePickedUp(): void
  52. {
  53. $this->createTranslationFile('not_a_translation_file.yaml');
  54. $this->createTranslationFile('not_a_translation_file.xliff');
  55. $this->createTranslationFile('not_a_translation_file.en');
  56. $this->createTranslationFile('messages.nl.xliff');
  57. $this->createTranslationFile('some-other.en.yaml');
  58. $files = FileUtils::findTranslationFiles($this->translationDirectory);
  59. $this->assertCount(2, $files);
  60. $this->assertArrayHasKey('messages', $files);
  61. $this->assertArrayHasKey('some-other', $files);
  62. }
  63. public function testRegularTranslationFileNamesAreParsed(): void
  64. {
  65. $this->createTranslationFile('messages.nl.yaml');
  66. $files = FileUtils::findTranslationFiles($this->translationDirectory);
  67. $this->assertArrayHasKey('messages', $files);
  68. $this->assertArrayHasKey('nl', $files['messages']);
  69. $this->assertEquals('yaml', $files['messages']['nl'][0]);
  70. $this->assertInstanceOf(SplFileInfo::class, $files['messages']['nl'][1]);
  71. $this->assertFalse($files['messages']['nl'][2]);
  72. }
  73. public function testIntlIcuTranslationFileNamesAreParsed(): void
  74. {
  75. $this->createTranslationFile('messages+intl-icu.en_GB.xliff');
  76. $files = FileUtils::findTranslationFiles($this->translationDirectory);
  77. $this->assertArrayHasKey('messages', $files);
  78. $this->assertArrayHasKey('en_GB', $files['messages']);
  79. $this->assertEquals('xliff', $files['messages']['en_GB'][0]);
  80. $this->assertInstanceOf(SplFileInfo::class, $files['messages']['en_GB'][1]);
  81. $this->assertTrue($files['messages']['en_GB'][2]);
  82. }
  83. /**
  84. * Creates a temporary directory which we can use to run the file utils
  85. * tests. It gets cleaned up afterwards.
  86. */
  87. protected function setUp(): void
  88. {
  89. $tempDir = sys_get_temp_dir();
  90. if (!is_writable($tempDir)) {
  91. $this->markTestSkipped(sprintf(
  92. "Can't execute FileUtils tests because %s is not writable",
  93. $tempDir
  94. ));
  95. }
  96. $this->translationDirectory = $tempDir . '/' . uniqid('jms_test_');
  97. $directoryCreated = mkdir($this->translationDirectory);
  98. if (!$directoryCreated) {
  99. $this->markTestSkipped(sprintf(
  100. "Can't execute FileUtils tests because %s could not be created",
  101. $this->translationDirectory
  102. ));
  103. }
  104. }
  105. protected function tearDown(): void
  106. {
  107. $this->fileSystem->remove($this->translationDirectory);
  108. }
  109. private function createTranslationFile(string $filename): void
  110. {
  111. touch($this->translationDirectory . '/' . $filename);
  112. }
  113. }