From f9f53dc3e7d27dfc6fa5f48e556ce2bf4130f504 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Thu, 9 May 2024 11:12:23 +0200 Subject: [PATCH] Remove temporary solution mapper --- src/Solution/DirectorySolution.php | 1 - src/Solution/InTempSolutionMapper.php | 58 ------------- src/Solution/SingleFileSolution.php | 2 +- test/Solution/InTempSolutionMapperTest.php | 96 ---------------------- 4 files changed, 1 insertion(+), 156 deletions(-) delete mode 100644 src/Solution/InTempSolutionMapper.php delete mode 100644 test/Solution/InTempSolutionMapperTest.php diff --git a/src/Solution/DirectorySolution.php b/src/Solution/DirectorySolution.php index 2e8c9d8a..6491db05 100644 --- a/src/Solution/DirectorySolution.php +++ b/src/Solution/DirectorySolution.php @@ -88,7 +88,6 @@ public function __construct(string $directory, string $entryPoint, array $exclus */ public static function fromDirectory(string $directory, array $exclusions = [], $entryPoint = 'solution.php'): self { - $directory = InTempSolutionMapper::mapDirectory($directory); return new self($directory, $entryPoint, array_merge($exclusions, ['composer.lock', 'vendor'])); } diff --git a/src/Solution/InTempSolutionMapper.php b/src/Solution/InTempSolutionMapper.php deleted file mode 100644 index 2511bc22..00000000 --- a/src/Solution/InTempSolutionMapper.php +++ /dev/null @@ -1,58 +0,0 @@ -mkdir($tempDir); - - $dirIterator = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS); - $iterator = new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::SELF_FIRST); - - foreach ($iterator as $file) { - /** @var \SplFileInfo $file */ - $target = Path::join($tempDir, $iterator->getSubPathName()); - - if ($fileSystem->exists($target)) { - continue; - } - - $file->isDir() - ? $fileSystem->mkdir($target) - : $fileSystem->copy($file->getPathname(), $target); - } - - return $tempDir; - } - - public static function mapFile(string $file): string - { - $fileSystem = new Filesystem(); - $tempFile = Path::join(self::getDeterministicTempDir($file), basename($file)); - - if ($fileSystem->exists($tempFile)) { - return $tempFile; - } - - $fileSystem->mkdir(dirname($tempFile)); - $fileSystem->copy($file, $tempFile); - - return $tempFile; - } - - private static function getDeterministicTempDir(string $path): string - { - return Path::join(System::tempDir(), md5($path)); - } -} diff --git a/src/Solution/SingleFileSolution.php b/src/Solution/SingleFileSolution.php index 3964c91d..82e57e06 100644 --- a/src/Solution/SingleFileSolution.php +++ b/src/Solution/SingleFileSolution.php @@ -38,7 +38,7 @@ public function __construct(string $file) */ public static function fromFile(string $file): self { - return new self(InTempSolutionMapper::mapFile($file)); + return new self($file); } /** diff --git a/test/Solution/InTempSolutionMapperTest.php b/test/Solution/InTempSolutionMapperTest.php deleted file mode 100644 index 945ddd11..00000000 --- a/test/Solution/InTempSolutionMapperTest.php +++ /dev/null @@ -1,96 +0,0 @@ -remove(System::tempDir('php-school')); - - parent::tearDown(); - } - - public function testFileMapping(): void - { - $filePath = $this->getTemporaryFile('test.file'); - - $mappedFile = InTempSolutionMapper::mapFile($filePath); - - self::assertFileExists($mappedFile); - self::assertNotSame($filePath, $mappedFile); - self::assertStringContainsString(System::tempDir(), $mappedFile); - } - - public function testDirectoryMapping(): void - { - $this->getTemporaryFile('test.file'); - $this->getTemporaryFile('innerDir/test.file'); - - $mappedDir = InTempSolutionMapper::mapDirectory($this->getTemporaryDirectory()); - - self::assertDirectoryExists($mappedDir); - self::assertDirectoryExists(Path::join($mappedDir, 'innerDir')); - self::assertFileExists(Path::join($mappedDir, 'test.file')); - self::assertFileExists(Path::join($mappedDir, 'innerDir', 'test.file')); - self::assertNotSame($this->getTemporaryDirectory(), $mappedDir); - self::assertStringContainsString(System::tempDir(), $mappedDir); - } - - public function testMappingIsDeterministicTempDir(): void - { - $filePath = $this->getTemporaryFile('test.file'); - - $dirName = bin2hex(random_bytes(10)); - $tempDir = Path::join($this->getTemporaryDirectory(), $dirName); - mkdir($tempDir); - - $fileHash = md5($filePath); - $dirHash = md5($tempDir); - - self::assertSame( - InTempSolutionMapper::mapFile($filePath), - Path::join(System::tempDir(), $fileHash, 'test.file') - ); - - self::assertNotSame( - InTempSolutionMapper::mapDirectory($this->getTemporaryDirectory()), - System::tempDir(Path::join('php-school', $dirHash, dirname($dirName))) - ); - } - - public function testContentsAreNotOverwroteIfExists(): void - { - $filePath = $this->getTemporaryFile('test.file', 'Old contents'); - - $dirName = bin2hex(random_bytes(10)); - $tempDir = Path::join($this->getTemporaryDirectory(), $dirName); - - $this->getTemporaryFile(Path::join($dirName, 'test.file'), 'Old contents'); - - $tempFilePath = System::tempDir(Path::join('php-school', md5($filePath), 'test.file')); - $tempDirPath = System::tempDir(Path::join('php-school', md5($tempDir), $dirName)); - - mkdir(dirName($tempFilePath), 0777, true); - file_put_contents($tempFilePath, 'Fresh contents'); - mkdir($tempDirPath, 0777, true); - file_put_contents(Path::join($tempDirPath, 'test.file'), 'Fresh contents'); - - // These calls will invoke the copying of of dir/files to temp - InTempSolutionMapper::mapFile($filePath); - InTempSolutionMapper::mapDirectory($tempDir); - - self::assertSame('Old contents', file_get_contents($filePath)); - self::assertSame('Fresh contents', file_get_contents($tempFilePath)); - self::assertSame('Old contents', file_get_contents(Path::join($tempDir, 'test.file'))); - self::assertSame('Fresh contents', file_get_contents(Path::join($tempDirPath, 'test.file'))); - } -}