From 24149df5835d1b6348f0f563caef42e98d9c0428 Mon Sep 17 00:00:00 2001 From: Michiel Roos <michiel@maxserv.nl> Date: Tue, 11 Feb 2014 11:07:12 +0100 Subject: [PATCH] [TASK] Use vfsStream for getFilesInDir() tests Change-Id: I06bb7492146a9149917bc2fc9100fbdddd9b8d09 Resolves: #55871 Releases: 6.2 Reviewed-on: https://review.typo3.org/27539 Reviewed-by: Oliver Klee Reviewed-by: Christian Kuhn Tested-by: Christian Kuhn Reviewed-by: Wouter Wolters Tested-by: Wouter Wolters --- .../Tests/Unit/Utility/GeneralUtilityTest.php | 151 +++++++----------- 1 file changed, 58 insertions(+), 93 deletions(-) diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php index 10e3f75b5a7e..f453d4b4a068 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php +++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php @@ -3330,122 +3330,95 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase { * @return string A unique directory name prefixed with test_. */ protected function getFilesInDirCreateTestDirectory() { - $directory = 'typo3temp/' . uniqid('test_'); - Utility\GeneralUtility::mkdir_deep(PATH_site, $directory); - $this->testFilesToDelete[] = PATH_site . $directory; - return $directory; + if (!class_exists('org\\bovigo\\vfs\\vfsStreamWrapper')) { + $this->markTestSkipped('getFilesInDirCreateTestDirectory() helper method not available without vfsStream.'); + } + $structure = array( + 'subDirectory' => array( + 'test.php' => 'butter', + 'other.php' => 'milk', + 'stuff.csv' => 'honey', + ), + 'excludeMe.txt' => 'cocoa nibs', + 'testB.txt' => 'olive oil', + 'testA.txt' => 'eggs', + 'testC.txt' => 'carrots', + 'test.js' => 'oranges', + 'test.css' => 'apples', + '.secret.txt' => 'sammon', + ); + \vfsStream::setup('test', NULL, $structure); + return \vfsStream::url('test'); } /** * @test */ public function getFilesInDirFindsRegularFile() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $unique = uniqid('test_'); - $content = 'getFilesInDirFindsRegularFile'; - $file = PATH_site . $directory . '/' . $unique . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $files = Utility\GeneralUtility::getFilesInDir(PATH_site . $directory); - $this->assertContains($unique . '.txt', $files); + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); + $files = Utility\GeneralUtility::getFilesInDir($vfsStreamUrl); + $this->assertContains('testA.txt', $files); } /** * @test */ public function getFilesInDirFindsHiddenFile() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $unique = uniqid('test_'); - $content = 'getFilesInDirFindsHiddenFile'; - $file = PATH_site . $directory . '/.' . $unique . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $files = Utility\GeneralUtility::getFilesInDir(PATH_site . $directory); - $this->assertContains('.' . $unique . '.txt', $files); + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); + $files = Utility\GeneralUtility::getFilesInDir($vfsStreamUrl); + $this->assertContains('.secret.txt', $files); } /** * @test */ public function getFilesInDirByExtensionFindsFiles() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $unique = uniqid('test_'); - $content = 'getFilesInDirByExtensionFindsFiles'; - $file = PATH_site . $directory . '/' . $unique . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $file = PATH_site . $directory . '/' . $unique . '.js'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $files = Utility\GeneralUtility::getFilesInDir(PATH_site . $directory, 'txt,js'); - $this->assertContains($unique . '.txt', $files); - $this->assertContains($unique . '.js', $files); + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); + $files = Utility\GeneralUtility::getFilesInDir($vfsStreamUrl, 'txt,js'); + $this->assertContains('testA.txt', $files); + $this->assertContains('test.js', $files); } /** * @test */ public function getFilesInDirByExtensionDoesNotFindFilesWithOtherExtensions() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $unique = uniqid('test_'); - $content = 'getFilesInDirFindsRegularFile'; - $file = PATH_site . $directory . '/' . $unique . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $file = PATH_site . $directory . '/' . $unique . '.js'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $file = PATH_site . $directory . '/' . $unique . '.css'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $files = Utility\GeneralUtility::getFilesInDir(PATH_site . $directory, 'txt,js'); - $this->assertNotContains($unique . '.css', $files); - $this->assertContains($unique . '.txt', $files); - $this->assertContains($unique . '.js', $files); + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); + $files = Utility\GeneralUtility::getFilesInDir($vfsStreamUrl, 'txt,js'); + $this->assertContains('testA.txt', $files); + $this->assertContains('test.js', $files); + $this->assertNotContains('test.css', $files); } /** * @test */ public function getFilesInDirExcludesFilesMatchingPattern() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $unique = uniqid('test_'); - $content = 'getFilesInDirExcludesFilesMatchingPattern'; - $file = PATH_site . $directory . '/' . $unique . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $file = PATH_site . $directory . '/' . $unique . '.js'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $excludeUnique = uniqid('excludeMe'); - $file = PATH_site . $directory . '/' . $excludeUnique . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $files = Utility\GeneralUtility::getFilesInDir(PATH_site . $directory, '', FALSE, '', 'excludeMe.*'); - $this->assertNotContains($excludeUnique . '.txt', $files); - $this->assertContains($unique . '.js', $files); + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); + $files = Utility\GeneralUtility::getFilesInDir($vfsStreamUrl, '', FALSE, '', 'excludeMe.*'); + $this->assertContains('test.js', $files); + $this->assertNotContains('excludeMe.txt', $files); } /** * @test */ public function getFilesInDirCanPrependPath() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $unique = uniqid('test_'); - $content = 'getFilesInDirCanPrependPath'; - $file = PATH_site . $directory . '/' . $unique . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $files = Utility\GeneralUtility::getFilesInDir(PATH_site . $directory, '', TRUE); - $this->assertContains(PATH_site . $directory . '/' . $unique . '.txt', $files); + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); + $this->assertContains( + $vfsStreamUrl . '/testA.txt', + Utility\GeneralUtility::getFilesInDir($vfsStreamUrl, '', TRUE) + ); } /** * @test */ public function getFilesInDirCanOrderAlphabetically() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $uniqueB = uniqid('test_B_'); - $content = 'test_B'; - $file = PATH_site . $directory . '/' . $uniqueB . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $uniqueA = uniqid('test_A_'); - $content = 'test_A'; - $file = PATH_site . $directory . '/' . $uniqueA . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); $this->assertSame( - array_values(Utility\GeneralUtility::getFilesInDir(PATH_site . $directory, '', FALSE, '1')), - array($uniqueA . '.txt', $uniqueB . '.txt') + array_values(Utility\GeneralUtility::getFilesInDir($vfsStreamUrl, '', FALSE, '1')), + array('.secret.txt', 'excludeMe.txt', 'test.css', 'test.js', 'testA.txt', 'testB.txt', 'testC.txt') ); } @@ -3453,26 +3426,22 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase { * @test */ public function getFilesInDirReturnsArrayWithMd5OfElementAndPathAsArrayKey() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $unique = uniqid('test_'); - $content = 'getFilesInDirReturnsArrayWithMd5OfElementAndPathAsArrayKey'; - $file = PATH_site . $directory . '/' . $unique . '.txt'; - $md5 = md5($file); - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $files = Utility\GeneralUtility::getFilesInDir(PATH_site . $directory); - $this->assertArrayHasKey($md5, $files); + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); + $this->assertArrayHasKey( + md5($vfsStreamUrl . '/testA.txt'), + Utility\GeneralUtility::getFilesInDir($vfsStreamUrl) + ); } /** * @test */ public function getFilesInDirDoesNotFindDirectories() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $subDirectory = $directory . '/testSubdirectory'; - Utility\GeneralUtility::mkdir_deep(PATH_site, $subDirectory); - $this->testFilesToDelete[] = PATH_site . $subDirectory; - $files = Utility\GeneralUtility::getFilesInDir(PATH_site . $directory); - $this->assertNotContains('testSubdirectory', $files); + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); + $this->assertNotContains( + 'subDirectory', + Utility\GeneralUtility::getFilesInDir($vfsStreamUrl) + ); } /** @@ -3482,12 +3451,8 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase { * @test */ public function getFilesInDirDoesNotFindDotfiles() { - $directory = $this->getFilesInDirCreateTestDirectory(); - $unique = uniqid('test_'); - $content = 'getFilesInDirDoesNotFindDotfiles'; - $file = PATH_site . $directory . '/.' . $unique . '.txt'; - Utility\GeneralUtility::writeFileToTypo3tempDir($file, $content); - $files = Utility\GeneralUtility::getFilesInDir(PATH_site . $directory); + $vfsStreamUrl = $this->getFilesInDirCreateTestDirectory(); + $files = Utility\GeneralUtility::getFilesInDir($vfsStreamUrl); $this->assertNotContains('..', $files); $this->assertNotContains('.', $files); } -- GitLab