diff --git a/test/BaseTest.php b/test/BaseTest.php index fbb15f6b..426b12ec 100644 --- a/test/BaseTest.php +++ b/test/BaseTest.php @@ -5,6 +5,7 @@ namespace PhpSchool\PhpWorkshopTest; use PhpSchool\PhpWorkshop\Utils\Path; +use PhpSchool\PhpWorkshop\Utils\System; use PHPUnit\Framework\TestCase; use Symfony\Component\Filesystem\Filesystem; @@ -15,7 +16,7 @@ abstract class BaseTest extends TestCase public function getTemporaryDirectory(): string { if (!$this->tempDirectory) { - $tempDirectory = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); + $tempDirectory = System::tempDir($this->getName()); mkdir($tempDirectory, 0777, true); $this->tempDirectory = realpath($tempDirectory); @@ -24,7 +25,7 @@ public function getTemporaryDirectory(): string return $this->tempDirectory; } - public function getTemporaryFile(string $filename): string + public function getTemporaryFile(string $filename, string $content = null): string { $file = Path::join($this->getTemporaryDirectory(), $filename); @@ -33,7 +34,10 @@ public function getTemporaryFile(string $filename): string } @mkdir(dirname($file), 0777, true); - touch($file); + + $content !== null + ? file_put_contents($file, $content) + : touch($file); return $file; } diff --git a/test/Solution/DirectorySolutionTest.php b/test/Solution/DirectorySolutionTest.php index 03709a7e..7c2f74e9 100644 --- a/test/Solution/DirectorySolutionTest.php +++ b/test/Solution/DirectorySolutionTest.php @@ -4,46 +4,35 @@ use InvalidArgumentException; use PhpSchool\PhpWorkshop\Solution\DirectorySolution; -use PhpSchool\PhpWorkshop\Utils\Path; -use PHPUnit\Framework\TestCase; +use PhpSchool\PhpWorkshop\Utils\System; +use PhpSchool\PhpWorkshopTest\BaseTest; use Symfony\Component\Filesystem\Filesystem; -class DirectorySolutionTest extends TestCase +class DirectorySolutionTest extends BaseTest { - /** - * @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); + (new Filesystem())->remove(System::tempDir('php-school')); + + parent::tearDown(); } public function testExceptionIsThrownIfEntryPointDoesNotExist(): void { - touch(sprintf('%s/some-class.php', $this->tempPath)); + $this->getTemporaryFile('some-class.php'); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessageMatches('/Entry point: "solution.php" does not exist in: ".*"/'); - DirectorySolution::fromDirectory($this->tempPath); + DirectorySolution::fromDirectory($this->getTemporaryDirectory()); } public function testWithDefaultEntryPoint(): void { - file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); - file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); + $this->getTemporaryFile('solution.php', 'ENTRYPOINT'); + $this->getTemporaryFile('some-class.php', 'SOME CLASS'); - $solution = DirectorySolution::fromDirectory($this->tempPath); + $solution = DirectorySolution::fromDirectory($this->getTemporaryDirectory()); self::assertFalse($solution->hasComposerFile()); self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); @@ -56,10 +45,10 @@ public function testWithDefaultEntryPoint(): void public function testWithManualEntryPoint(): void { - file_put_contents(sprintf('%s/index.php', $this->tempPath), 'ENTRYPOINT'); - file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); + $this->getTemporaryFile('index.php', 'ENTRYPOINT'); + $this->getTemporaryFile('some-class.php', 'SOME CLASS'); - $solution = DirectorySolution::fromDirectory($this->tempPath, [], 'index.php'); + $solution = DirectorySolution::fromDirectory($this->getTemporaryDirectory(), [], 'index.php'); self::assertFalse($solution->hasComposerFile()); self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); @@ -72,11 +61,11 @@ public function testWithManualEntryPoint(): void public function testHasComposerFileReturnsTrueIfPresent(): void { - 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)); + $this->getTemporaryFile('solution.php', 'ENTRYPOINT'); + $this->getTemporaryFile('some-class.php', 'SOME CLASS'); + $this->getTemporaryFile('composer.lock'); - $solution = DirectorySolution::fromDirectory($this->tempPath); + $solution = DirectorySolution::fromDirectory($this->getTemporaryDirectory()); self::assertTrue($solution->hasComposerFile()); self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); @@ -89,13 +78,13 @@ public function testHasComposerFileReturnsTrueIfPresent(): void public function testWithExceptions(): void { - 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)); + $this->getTemporaryFile('solution.php', 'ENTRYPOINT'); + $this->getTemporaryFile('some-class.php', 'SOME CLASS'); + $this->getTemporaryFile('exclude.txt'); $exclusions = ['exclude.txt']; - $solution = DirectorySolution::fromDirectory($this->tempPath, $exclusions); + $solution = DirectorySolution::fromDirectory($this->getTemporaryDirectory(), $exclusions); self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); $files = $solution->getFiles(); @@ -107,16 +96,13 @@ public function testWithExceptions(): void public function testWithNestedDirectories(): void { - @mkdir(sprintf('%s/nested', $this->tempPath), 0775, true); - @mkdir(sprintf('%s/nested/deep', $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'); - 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'); + $this->getTemporaryFile('solution.php', 'ENTRYPOINT'); + $this->getTemporaryFile('some-class.php', 'SOME CLASS'); + $this->getTemporaryFile('composer.json', 'COMPOSER DATA'); + $this->getTemporaryFile('nested/another-class.php', 'ANOTHER CLASS'); + $this->getTemporaryFile('nested/deep/even-more.php', 'EVEN MOAR'); - $solution = DirectorySolution::fromDirectory($this->tempPath); + $solution = DirectorySolution::fromDirectory($this->getTemporaryDirectory()); self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); $files = $solution->getFiles(); @@ -131,21 +117,16 @@ public function testWithNestedDirectories(): void public function testExceptionsWithNestedDirectories(): void { - @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)); + $this->getTemporaryFile('solution.php', 'ENTRYPOINT'); + $this->getTemporaryFile('some-class.php', 'SOME CLASS'); + $this->getTemporaryFile('exclude.txt'); + $this->getTemporaryFile('nested/exclude.txt'); + $this->getTemporaryFile('nested/deep/exclude.txt'); + $this->getTemporaryFile('vendor/somelib/app.php'); $exclusions = ['exclude.txt', 'vendor']; - $solution = DirectorySolution::fromDirectory($this->tempPath, $exclusions); + $solution = DirectorySolution::fromDirectory($this->getTemporaryDirectory(), $exclusions); self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); $files = $solution->getFiles(); diff --git a/test/Solution/InTempSolutionMapperTest.php b/test/Solution/InTempSolutionMapperTest.php index 58edd25b..de6e3253 100644 --- a/test/Solution/InTempSolutionMapperTest.php +++ b/test/Solution/InTempSolutionMapperTest.php @@ -2,83 +2,84 @@ declare(strict_types=1); -namespace Solution; +namespace PhpSchool\PhpWorkshopTest\Solution; use PhpSchool\PhpWorkshop\Solution\InTempSolutionMapper; use PhpSchool\PhpWorkshop\Utils\Path; -use PHPUnit\Framework\TestCase; +use PhpSchool\PhpWorkshop\Utils\System; +use PhpSchool\PhpWorkshopTest\BaseTest; +use Symfony\Component\Filesystem\Filesystem; -class InTempSolutionMapperTest extends TestCase +class InTempSolutionMapperTest extends BaseTest { + public function tearDown(): void + { + (new Filesystem())->remove(System::tempDir('php-school')); + + parent::tearDown(); + } + public function testFileMapping(): void { - $filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file'); - touch($filePath); + $filePath = $this->getTemporaryFile('test.file'); $mappedFile = InTempSolutionMapper::mapFile($filePath); self::assertFileExists($mappedFile); self::assertNotSame($filePath, $mappedFile); - self::assertStringContainsString(realpath(sys_get_temp_dir()), $mappedFile); + self::assertStringContainsString(System::tempDir('php-school'), $mappedFile); } public function testDirectoryMapping(): void { - $tempDir = Path::join(realpath(sys_get_temp_dir()), bin2hex(random_bytes(10))); - $file = Path::join($tempDir, 'test.file'); - $inner = Path::join($tempDir, 'innerDir'); - $innerFile = Path::join($inner, 'test.file'); - @mkdir($tempDir); - touch($file); - @mkdir($inner); - touch($innerFile); + $this->getTemporaryFile('test.file'); + $this->getTemporaryFile('innerDir/test.file'); - $mappedDir = InTempSolutionMapper::mapDirectory($tempDir); + $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($tempDir, $mappedDir); - self::assertStringContainsString(realpath(sys_get_temp_dir()), $mappedDir); + self::assertNotSame($this->getTemporaryDirectory(), $mappedDir); + self::assertStringContainsString(System::tempDir('php-school'), $mappedDir); } public function testMappingIsDeterministicTempDir(): void { - $filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file'); - touch($filePath); + $filePath = $this->getTemporaryFile('test.file'); $dirName = bin2hex(random_bytes(10)); - $tempDir = Path::join(realpath(sys_get_temp_dir()), $dirName); - @mkdir($tempDir); + $tempDir = Path::join($this->getTemporaryDirectory(), $dirName); + mkdir($tempDir); $fileHash = md5($filePath); $dirHash = md5($tempDir); self::assertSame( InTempSolutionMapper::mapFile($filePath), - Path::join(realpath(sys_get_temp_dir()), 'php-school', $fileHash, 'test.file') + Path::join(System::tempDir(), 'php-school', $fileHash, 'test.file') ); self::assertNotSame( - InTempSolutionMapper::mapDirectory($tempDir), - Path::join(realpath(sys_get_temp_dir()), 'php-school', $dirHash, $dirName) + InTempSolutionMapper::mapDirectory($this->getTemporaryDirectory()), + System::tempDir(Path::join('php-school', $dirHash, dirname($dirName))) ); } public function testContentsAreNotOverwroteIfExists(): void { - $filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file'); - file_put_contents($filePath, 'Old contents'); + $filePath = $this->getTemporaryFile('test.file', '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'); + $tempDir = Path::join($this->getTemporaryDirectory(), $dirName); + + $this->getTemporaryFile(Path::join($dirName, '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); + $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'); diff --git a/test/Solution/SingleFileSolutionTest.php b/test/Solution/SingleFileSolutionTest.php index 7723e37c..a6ad8354 100644 --- a/test/Solution/SingleFileSolutionTest.php +++ b/test/Solution/SingleFileSolutionTest.php @@ -3,17 +3,13 @@ namespace PhpSchool\PhpWorkshopTest\Solution; use PhpSchool\PhpWorkshop\Solution\SingleFileSolution; -use PHPUnit\Framework\TestCase; +use PhpSchool\PhpWorkshopTest\BaseTest; -class SingleFileSolutionTest extends TestCase +class SingleFileSolutionTest extends BaseTest { public function testGetters(): void { - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); - $filePath = sprintf('%s/test.file', $tempPath); - - @mkdir($tempPath, 0775, true); - file_put_contents($filePath, 'FILE CONTENTS'); + $filePath = $this->getTemporaryFile('test.file', 'FILE CONTENTS'); $solution = SingleFileSolution::fromFile($filePath); @@ -21,7 +17,5 @@ public function testGetters(): void self::assertFalse($solution->hasComposerFile()); self::assertCount(1, $solution->getFiles()); self::assertSame('FILE CONTENTS', file_get_contents($solution->getFiles()[0]->__toString())); - unlink($filePath); - rmdir($tempPath); } } diff --git a/test/Solution/SolutionFileTest.php b/test/Solution/SolutionFileTest.php index bdaee5d0..6c0350af 100644 --- a/test/Solution/SolutionFileTest.php +++ b/test/Solution/SolutionFileTest.php @@ -4,9 +4,10 @@ use InvalidArgumentException; use PhpSchool\PhpWorkshop\Solution\SolutionFile; -use PHPUnit\Framework\TestCase; +use PhpSchool\PhpWorkshop\Utils\Path; +use PhpSchool\PhpWorkshopTest\BaseTest; -class SolutionFileTest extends TestCase +class SolutionFileTest extends BaseTest { public function testExceptionIsThrowIfFileNotExists(): void { @@ -18,83 +19,56 @@ public function testExceptionIsThrowIfFileNotExists(): void public function testPaths(): void { - $tempPath = sprintf('%s/%s', sys_get_temp_dir(), $this->getName()); - $filePath = sprintf('%s/test.file', $tempPath); - - @mkdir($tempPath, 0775, true); - touch($filePath); + $filePath = $this->getTemporaryFile('test.file'); $file = SolutionFile::fromFile($filePath); $this->assertSame($filePath, $file->__toString()); $this->assertSame('test.file', $file->getRelativePath()); - $this->assertSame($tempPath, $file->getBaseDirectory()); - - unlink($filePath); - rmdir($tempPath); + $this->assertSame($this->getTemporaryDirectory(), $file->getBaseDirectory()); } public function testEmptyContents(): void { - $tempPath = sprintf('%s/%s', sys_get_temp_dir(), $this->getName()); - $filePath = sprintf('%s/test.file', $tempPath); - - @mkdir($tempPath, 0775, true); - touch($filePath); + $filePath = $this->getTemporaryFile('test.file'); $file = SolutionFile::fromFile($filePath); $this->assertSame($filePath, $file->__toString()); $this->assertSame('test.file', $file->getRelativePath()); - $this->assertSame($tempPath, $file->getBaseDirectory()); + $this->assertSame($this->getTemporaryDirectory(), $file->getBaseDirectory()); $this->assertSame('', $file->getContents()); - unlink($filePath); - rmdir($tempPath); } public function testGetContents(): void { - $tempPath = sprintf('%s/%s', sys_get_temp_dir(), $this->getName()); - $filePath = sprintf('%s/test.file', $tempPath); - - @mkdir($tempPath, 0775, true); - file_put_contents($filePath, 'epiccontentz'); + $filePath = $this->getTemporaryFile('test.file', 'epiccontentz'); $file = SolutionFile::fromFile($filePath); $this->assertSame($filePath, $file->__toString()); $this->assertSame('test.file', $file->getRelativePath()); - $this->assertSame($tempPath, $file->getBaseDirectory()); + $this->assertSame($this->getTemporaryDirectory(), $file->getBaseDirectory()); $this->assertSame('epiccontentz', $file->getContents()); - unlink($filePath); - rmdir($tempPath); } public function testConstructionWithManualBaseDirectory(): void { - $tempPath = sprintf('%s/%s/sub-dir', sys_get_temp_dir(), $this->getName()); - $filePath = sprintf('%s/test.file', $tempPath); - - @mkdir($tempPath, 0775, true); - touch($filePath); - + $tempPath = Path::join($this->getTemporaryDirectory(), 'sub-dir'); + $filePath = $this->getTemporaryFile('sub-dir/test.file', 'epiccontentz'); $file = new SolutionFile('test.file', $tempPath); $this->assertSame($filePath, $file->__toString()); $this->assertSame('test.file', $file->getRelativePath()); $this->assertSame($tempPath, $file->getBaseDirectory()); - $this->assertSame('', $file->getContents()); - unlink($filePath); - rmdir($tempPath); + $this->assertSame('epiccontentz', $file->getContents()); } + public function testGetExtension(): void { - $tempPath = sprintf('%s/%s/sub-dir', sys_get_temp_dir(), $this->getName()); - $filePath = sprintf('%s/test.php', $tempPath); - - @mkdir($tempPath, 0775, true); - touch($filePath); + $tempPath = Path::join($this->getTemporaryDirectory(), 'sub-dir'); + $this->getTemporaryFile('sub-dir/test.php', 'epiccontentz'); $file = new SolutionFile('test.php', $tempPath); $this->assertSame('php', $file->getExtension());