From dba1d8ebbff15a4b98b804a8d6d4d706b9bf945e Mon Sep 17 00:00:00 2001 From: Michael Woodward Date: Mon, 17 May 2021 23:33:03 +0100 Subject: [PATCH 1/4] Pushes all solution files into temp when created --- src/Solution/DirectorySolution.php | 1 + src/Solution/InTempSolutionMapper.php | 48 ++++++++++++++++++ src/Solution/SingleFileSolution.php | 2 +- test/Solution/InTempSolutionMapperTest.php | 57 ++++++++++++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/Solution/InTempSolutionMapper.php create mode 100644 test/Solution/InTempSolutionMapperTest.php diff --git a/src/Solution/DirectorySolution.php b/src/Solution/DirectorySolution.php index b67b55fe..ff172ab8 100644 --- a/src/Solution/DirectorySolution.php +++ b/src/Solution/DirectorySolution.php @@ -85,6 +85,7 @@ 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 new file mode 100644 index 00000000..5841c4a3 --- /dev/null +++ b/src/Solution/InTempSolutionMapper.php @@ -0,0 +1,48 @@ +mkdir($tempDir); + + $dirIterator = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS); + $iterator = new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::SELF_FIRST); + + foreach ($iterator as $file) { + $target = Path::join($tempDir, $iterator->getSubPathName()); + $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::getRandomTempDir(), basename($file)); + + $fileSystem->mkdir(System::tempDir()); + $fileSystem->copy($file, $tempFile); + + return $tempFile; + } + + private static function getRandomTempDir(): string + { + return Path::join(System::tempDir(), 'php-school', bin2hex(random_bytes(10))); + } +} diff --git a/src/Solution/SingleFileSolution.php b/src/Solution/SingleFileSolution.php index b32aac67..4a0fbfb5 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($file); + return new self(InTempSolutionMapper::mapFile($file)); } /** diff --git a/test/Solution/InTempSolutionMapperTest.php b/test/Solution/InTempSolutionMapperTest.php new file mode 100644 index 00000000..02af9d4e --- /dev/null +++ b/test/Solution/InTempSolutionMapperTest.php @@ -0,0 +1,57 @@ + Date: Mon, 17 May 2021 23:42:32 +0100 Subject: [PATCH 2/4] Fix AbstractExerciseTest::testGetSolution In temp mapper moves files of solution so we can no longer check against file paths expected as they're always random in temp. Instead we now compare contents to ensure it's as we expect. --- test/Exercise/AbstractExerciseTest.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/Exercise/AbstractExerciseTest.php b/test/Exercise/AbstractExerciseTest.php index cddc98b1..6b2d722c 100644 --- a/test/Exercise/AbstractExerciseTest.php +++ b/test/Exercise/AbstractExerciseTest.php @@ -26,12 +26,10 @@ public function testGetSolution(string $name): void $path = __DIR__ . '/../../exercises/array-we-go/solution/solution.php'; mkdir(dirname($path), 0777, true); touch($path); - $solution = $exercise->getSolution(); - $this->assertInstanceOf(SolutionInterface::class, $solution); - $files = $solution->getFiles(); - $this->assertCount(1, $files); - $this->assertInstanceOf(SolutionFile::class, $files[0]); - $this->assertSame(realpath($path), $files[0]->__toString()); + $files = $exercise->getSolution()->getFiles(); + self::assertCount(1, $files); + self::assertInstanceOf(SolutionFile::class, $files[0]); + self::assertFileEquals(realpath($path), $files[0]->__toString()); unlink($path); rmdir(__DIR__ . '/../../exercises/array-we-go/solution'); rmdir(__DIR__ . '/../../exercises/array-we-go'); From 57f6f752986a8dbb05dd482e22178b5d84f5b084 Mon Sep 17 00:00:00 2001 From: Michael Woodward Date: Tue, 18 May 2021 00:13:12 +0100 Subject: [PATCH 3/4] Make In temp solution path deterministic This ensures we can keep state allowing invoking the same solution multiple times and not doing FS operations everytime. Side benefit is solutions will not require caching --- src/Solution/InTempSolutionMapper.php | 17 ++++++-- test/Solution/InTempSolutionMapperTest.php | 46 ++++++++++++++++++++-- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/Solution/InTempSolutionMapper.php b/src/Solution/InTempSolutionMapper.php index 5841c4a3..b63517fc 100644 --- a/src/Solution/InTempSolutionMapper.php +++ b/src/Solution/InTempSolutionMapper.php @@ -13,7 +13,7 @@ class InTempSolutionMapper public static function mapDirectory(string $directory): string { $fileSystem = new Filesystem(); - $tempDir = self::getRandomTempDir(); + $tempDir = self::getDeterministicTempDir($directory); $fileSystem->mkdir($tempDir); @@ -22,6 +22,11 @@ public static function mapDirectory(string $directory): string foreach ($iterator as $file) { $target = Path::join($tempDir, $iterator->getSubPathName()); + + if ($fileSystem->exists($target)) { + continue; + } + $file->isDir() ? $fileSystem->mkdir($target) : $fileSystem->copy($file->getPathname(), $target); @@ -33,7 +38,11 @@ public static function mapDirectory(string $directory): string public static function mapFile(string $file): string { $fileSystem = new Filesystem(); - $tempFile = Path::join(self::getRandomTempDir(), basename($file)); + $tempFile = Path::join(self::getDeterministicTempDir($file), basename($file)); + + if ($fileSystem->exists($tempFile)) { + return $tempFile; + } $fileSystem->mkdir(System::tempDir()); $fileSystem->copy($file, $tempFile); @@ -41,8 +50,8 @@ public static function mapFile(string $file): string return $tempFile; } - private static function getRandomTempDir(): string + private static function getDeterministicTempDir(string $path): string { - return Path::join(System::tempDir(), 'php-school', bin2hex(random_bytes(10))); + return Path::join(System::tempDir(), 'php-school', md5($path)); } } diff --git a/test/Solution/InTempSolutionMapperTest.php b/test/Solution/InTempSolutionMapperTest.php index 02af9d4e..58edd25b 100644 --- a/test/Solution/InTempSolutionMapperTest.php +++ b/test/Solution/InTempSolutionMapperTest.php @@ -43,15 +43,53 @@ public function testDirectoryMapping(): void self::assertStringContainsString(realpath(sys_get_temp_dir()), $mappedDir); } - public function testMappingIsInFreshTempDir(): void + public function testMappingIsDeterministicTempDir(): void { $filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file'); touch($filePath); - $tempDir = Path::join(realpath(sys_get_temp_dir()), bin2hex(random_bytes(10))); + $dirName = bin2hex(random_bytes(10)); + $tempDir = Path::join(realpath(sys_get_temp_dir()), $dirName); @mkdir($tempDir); - self::assertNotSame(InTempSolutionMapper::mapFile($filePath), InTempSolutionMapper::mapFile($filePath)); - self::assertNotSame(InTempSolutionMapper::mapDirectory($tempDir), InTempSolutionMapper::mapDirectory($tempDir)); + $fileHash = md5($filePath); + $dirHash = md5($tempDir); + + self::assertSame( + InTempSolutionMapper::mapFile($filePath), + Path::join(realpath(sys_get_temp_dir()), 'php-school', $fileHash, 'test.file') + ); + + self::assertNotSame( + InTempSolutionMapper::mapDirectory($tempDir), + Path::join(realpath(sys_get_temp_dir()), 'php-school', $dirHash, $dirName) + ); + } + + public function testContentsAreNotOverwroteIfExists(): void + { + $filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file'); + file_put_contents($filePath, 'Old contents'); + + $dirName = bin2hex(random_bytes(10)); + $tempDir = Path::join(realpath(sys_get_temp_dir()), $dirName); + mkdir($tempDir); + file_put_contents(Path::join($tempDir, 'test.file'), 'Old contents'); + + $tempFilePath = Path::join(realpath(sys_get_temp_dir()), 'php-school', md5($filePath), 'test.file'); + $tempDirPath = Path::join(realpath(sys_get_temp_dir()), 'php-school', md5($tempDir), $dirName); + + 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'))); } } From 08af37af3f1d40f8e038962a7768f1c661654be0 Mon Sep 17 00:00:00 2001 From: Michael Woodward Date: Tue, 18 May 2021 00:59:35 +0100 Subject: [PATCH 4/4] Fix test breakages from InTempSolutionMapper Mainly around file paths not being what was expected I chose to compare contents rather than paths as it felt more robust, hower the temp paths are deterministic I just didn't want to crossover logic / concerns of this into these --- test/Solution/DirectorySolutionTest.php | 218 +++++++++-------------- test/Solution/SingleFileSolutionTest.php | 11 +- 2 files changed, 94 insertions(+), 135 deletions(-) diff --git a/test/Solution/DirectorySolutionTest.php b/test/Solution/DirectorySolutionTest.php index 37b18356..03709a7e 100644 --- a/test/Solution/DirectorySolutionTest.php +++ b/test/Solution/DirectorySolutionTest.php @@ -4,194 +4,154 @@ use InvalidArgumentException; use PhpSchool\PhpWorkshop\Solution\DirectorySolution; +use PhpSchool\PhpWorkshop\Utils\Path; use PHPUnit\Framework\TestCase; +use Symfony\Component\Filesystem\Filesystem; class DirectorySolutionTest extends TestCase { + /** + * @var string + */ + private $tempPath; + + public function setUp(): void + { + $this->tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); + @mkdir($this->tempPath); + } + + public function tearDown(): void + { + $fileSystem = new Filesystem(); + $fileSystem->remove(Path::join(realpath(sys_get_temp_dir()), 'php-school')); + $fileSystem->remove($this->tempPath); + } + public function testExceptionIsThrownIfEntryPointDoesNotExist(): void { - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); - @mkdir($tempPath, 0775, true); - touch(sprintf('%s/some-class.php', $tempPath)); + touch(sprintf('%s/some-class.php', $this->tempPath)); $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage(sprintf('Entry point: "solution.php" does not exist in: "%s"', $tempPath)); + $this->expectExceptionMessageMatches('/Entry point: "solution.php" does not exist in: ".*"/'); - DirectorySolution::fromDirectory($tempPath); - - unlink(sprintf('%s/some-class.php', $tempPath)); - rmdir($tempPath); + DirectorySolution::fromDirectory($this->tempPath); } public function testWithDefaultEntryPoint(): void { - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); - @mkdir($tempPath, 0775, true); - touch(sprintf('%s/solution.php', $tempPath)); - touch(sprintf('%s/some-class.php', $tempPath)); + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); - $solution = DirectorySolution::fromDirectory($tempPath); + $solution = DirectorySolution::fromDirectory($this->tempPath); - $this->assertSame($tempPath, $solution->getBaseDirectory()); - $this->assertFalse($solution->hasComposerFile()); - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); + self::assertFalse($solution->hasComposerFile()); + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); $files = $solution->getFiles(); - $this->assertCount(2, $files); - - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString()); - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); + self::assertCount(2, $files); - unlink(sprintf('%s/solution.php', $tempPath)); - unlink(sprintf('%s/some-class.php', $tempPath)); - rmdir($tempPath); + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); } public function testWithManualEntryPoint(): void { - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); - @mkdir($tempPath, 0775, true); - touch(sprintf('%s/index.php', $tempPath)); - touch(sprintf('%s/some-class.php', $tempPath)); + file_put_contents(sprintf('%s/index.php', $this->tempPath), 'ENTRYPOINT'); + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); - $solution = DirectorySolution::fromDirectory($tempPath, [], 'index.php'); + $solution = DirectorySolution::fromDirectory($this->tempPath, [], 'index.php'); - $this->assertSame($tempPath, $solution->getBaseDirectory()); - $this->assertFalse($solution->hasComposerFile()); - $this->assertSame(sprintf('%s/index.php', $tempPath), $solution->getEntryPoint()); + self::assertFalse($solution->hasComposerFile()); + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); $files = $solution->getFiles(); - $this->assertCount(2, $files); + self::assertCount(2, $files); - $this->assertSame(sprintf('%s/index.php', $tempPath), $files[0]->__toString()); - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); - - unlink(sprintf('%s/index.php', $tempPath)); - unlink(sprintf('%s/some-class.php', $tempPath)); - rmdir($tempPath); + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); } public function testHasComposerFileReturnsTrueIfPresent(): void { - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); - @mkdir($tempPath, 0775, true); - touch(sprintf('%s/solution.php', $tempPath)); - touch(sprintf('%s/some-class.php', $tempPath)); - touch(sprintf('%s/composer.lock', $tempPath)); + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); + touch(sprintf('%s/composer.lock', $this->tempPath)); - $solution = DirectorySolution::fromDirectory($tempPath); + $solution = DirectorySolution::fromDirectory($this->tempPath); - $this->assertSame($tempPath, $solution->getBaseDirectory()); - $this->assertTrue($solution->hasComposerFile()); - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); + self::assertTrue($solution->hasComposerFile()); + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); $files = $solution->getFiles(); - $this->assertCount(2, $files); - - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString()); - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); + self::assertCount(2, $files); - unlink(sprintf('%s/composer.lock', $tempPath)); - unlink(sprintf('%s/solution.php', $tempPath)); - unlink(sprintf('%s/some-class.php', $tempPath)); + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); } public function testWithExceptions(): void { - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); - @mkdir($tempPath, 0775, true); - touch(sprintf('%s/solution.php', $tempPath)); - touch(sprintf('%s/some-class.php', $tempPath)); - touch(sprintf('%s/exclude.txt', $tempPath)); + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); + touch(sprintf('%s/exclude.txt', $this->tempPath)); $exclusions = ['exclude.txt']; - $solution = DirectorySolution::fromDirectory($tempPath, $exclusions); + $solution = DirectorySolution::fromDirectory($this->tempPath, $exclusions); - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); $files = $solution->getFiles(); - $this->assertCount(2, $files); + self::assertCount(2, $files); - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString()); - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); - - unlink(sprintf('%s/solution.php', $tempPath)); - unlink(sprintf('%s/some-class.php', $tempPath)); - unlink(sprintf('%s/exclude.txt', $tempPath)); - rmdir($tempPath); + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); } public function testWithNestedDirectories(): void { - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); - @mkdir($tempPath, 0775, true); - - @mkdir(sprintf('%s/nested', $tempPath), 0775, true); - @mkdir(sprintf('%s/nested/deep', $tempPath), 0775, true); + @mkdir(sprintf('%s/nested', $this->tempPath), 0775, true); + @mkdir(sprintf('%s/nested/deep', $this->tempPath), 0775, true); - touch(sprintf('%s/solution.php', $tempPath)); - touch(sprintf('%s/some-class.php', $tempPath)); - touch(sprintf('%s/composer.json', $tempPath)); - touch(sprintf('%s/nested/another-class.php', $tempPath)); - touch(sprintf('%s/nested/deep/even-more.php', $tempPath)); + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); + file_put_contents(sprintf('%s/composer.json', $this->tempPath), 'COMPOSER DATA'); + file_put_contents(sprintf('%s/nested/another-class.php', $this->tempPath), 'ANOTHER CLASS'); + file_put_contents(sprintf('%s/nested/deep/even-more.php', $this->tempPath), 'EVEN MOAR'); - $solution = DirectorySolution::fromDirectory($tempPath); + $solution = DirectorySolution::fromDirectory($this->tempPath); - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); $files = $solution->getFiles(); - $this->assertCount(5, $files); - - $this->assertSame(sprintf('%s/composer.json', $tempPath), $files[0]->__toString()); - $this->assertSame(sprintf('%s/nested/another-class.php', $tempPath), $files[1]->__toString()); - $this->assertSame(sprintf('%s/nested/deep/even-more.php', $tempPath), $files[2]->__toString()); - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[3]->__toString()); - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[4]->__toString()); - - unlink(sprintf('%s/solution.php', $tempPath)); - unlink(sprintf('%s/some-class.php', $tempPath)); - unlink(sprintf('%s/composer.json', $tempPath)); - unlink(sprintf('%s/nested/another-class.php', $tempPath)); - unlink(sprintf('%s/nested/deep/even-more.php', $tempPath)); - rmdir(sprintf('%s/nested/deep', $tempPath)); - rmdir(sprintf('%s/nested', $tempPath)); - rmdir($tempPath); + self::assertCount(5, $files); + + self::assertSame('COMPOSER DATA', file_get_contents($files[0]->__toString())); + self::assertSame('ANOTHER CLASS', file_get_contents($files[1]->__toString())); + self::assertSame('EVEN MOAR', file_get_contents($files[2]->__toString())); + self::assertSame('ENTRYPOINT', file_get_contents($files[3]->__toString())); + self::assertSame('SOME CLASS', file_get_contents($files[4]->__toString())); } public function testExceptionsWithNestedDirectories(): void { - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); - @mkdir($tempPath, 0775, true); - - @mkdir(sprintf('%s/nested', $tempPath), 0775, true); - @mkdir(sprintf('%s/nested/deep', $tempPath), 0775, true); - @mkdir(sprintf('%s/vendor', $tempPath), 0775, true); - @mkdir(sprintf('%s/vendor/somelib', $tempPath), 0775, true); - - touch(sprintf('%s/solution.php', $tempPath)); - touch(sprintf('%s/some-class.php', $tempPath)); - touch(sprintf('%s/exclude.txt', $tempPath)); - touch(sprintf('%s/nested/exclude.txt', $tempPath)); - touch(sprintf('%s/nested/deep/exclude.txt', $tempPath)); - touch(sprintf('%s/vendor/somelib/app.php', $tempPath)); + @mkdir(sprintf('%s/nested', $this->tempPath), 0775, true); + @mkdir(sprintf('%s/nested/deep', $this->tempPath), 0775, true); + @mkdir(sprintf('%s/vendor', $this->tempPath), 0775, true); + @mkdir(sprintf('%s/vendor/somelib', $this->tempPath), 0775, true); + + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); + touch(sprintf('%s/exclude.txt', $this->tempPath)); + touch(sprintf('%s/nested/exclude.txt', $this->tempPath)); + touch(sprintf('%s/nested/deep/exclude.txt', $this->tempPath)); + touch(sprintf('%s/vendor/somelib/app.php', $this->tempPath)); $exclusions = ['exclude.txt', 'vendor']; - $solution = DirectorySolution::fromDirectory($tempPath, $exclusions); + $solution = DirectorySolution::fromDirectory($this->tempPath, $exclusions); - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); $files = $solution->getFiles(); - $this->assertCount(2, $files); - - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString()); - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); - - unlink(sprintf('%s/solution.php', $tempPath)); - unlink(sprintf('%s/some-class.php', $tempPath)); - unlink(sprintf('%s/exclude.txt', $tempPath)); - unlink(sprintf('%s/nested/exclude.txt', $tempPath)); - unlink(sprintf('%s/nested/deep/exclude.txt', $tempPath)); - unlink(sprintf('%s/vendor/somelib/app.php', $tempPath)); - rmdir(sprintf('%s/nested/deep', $tempPath)); - rmdir(sprintf('%s/nested', $tempPath)); - rmdir(sprintf('%s/vendor/somelib', $tempPath)); - rmdir(sprintf('%s/vendor', $tempPath)); - rmdir($tempPath); + self::assertCount(2, $files); + + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); } } diff --git a/test/Solution/SingleFileSolutionTest.php b/test/Solution/SingleFileSolutionTest.php index 6fa336d7..7723e37c 100644 --- a/test/Solution/SingleFileSolutionTest.php +++ b/test/Solution/SingleFileSolutionTest.php @@ -13,15 +13,14 @@ public function testGetters(): void $filePath = sprintf('%s/test.file', $tempPath); @mkdir($tempPath, 0775, true); - touch($filePath); + file_put_contents($filePath, 'FILE CONTENTS'); $solution = SingleFileSolution::fromFile($filePath); - $this->assertSame($filePath, $solution->getEntryPoint()); - $this->assertSame($tempPath, $solution->getBaseDirectory()); - $this->assertFalse($solution->hasComposerFile()); - $this->assertCount(1, $solution->getFiles()); - $this->assertSame($filePath, $solution->getFiles()[0]->__toString()); + self::assertSame('FILE CONTENTS', file_get_contents($solution->getEntryPoint())); + self::assertFalse($solution->hasComposerFile()); + self::assertCount(1, $solution->getFiles()); + self::assertSame('FILE CONTENTS', file_get_contents($solution->getFiles()[0]->__toString())); unlink($filePath); rmdir($tempPath); }