|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Solution; |
| 6 | + |
| 7 | +use PhpSchool\PhpWorkshop\Solution\InTempSolutionMapper; |
| 8 | +use PhpSchool\PhpWorkshop\Utils\Path; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class InTempSolutionMapperTest extends TestCase |
| 12 | +{ |
| 13 | + public function testFileMapping(): void |
| 14 | + { |
| 15 | + $filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file'); |
| 16 | + touch($filePath); |
| 17 | + |
| 18 | + $mappedFile = InTempSolutionMapper::mapFile($filePath); |
| 19 | + |
| 20 | + self::assertFileExists($mappedFile); |
| 21 | + self::assertNotSame($filePath, $mappedFile); |
| 22 | + self::assertStringContainsString(realpath(sys_get_temp_dir()), $mappedFile); |
| 23 | + } |
| 24 | + |
| 25 | + public function testDirectoryMapping(): void |
| 26 | + { |
| 27 | + $tempDir = Path::join(realpath(sys_get_temp_dir()), bin2hex(random_bytes(10))); |
| 28 | + $file = Path::join($tempDir, 'test.file'); |
| 29 | + $inner = Path::join($tempDir, 'innerDir'); |
| 30 | + $innerFile = Path::join($inner, 'test.file'); |
| 31 | + @mkdir($tempDir); |
| 32 | + touch($file); |
| 33 | + @mkdir($inner); |
| 34 | + touch($innerFile); |
| 35 | + |
| 36 | + $mappedDir = InTempSolutionMapper::mapDirectory($tempDir); |
| 37 | + |
| 38 | + self::assertDirectoryExists($mappedDir); |
| 39 | + self::assertDirectoryExists(Path::join($mappedDir, 'innerDir')); |
| 40 | + self::assertFileExists(Path::join($mappedDir, 'test.file')); |
| 41 | + self::assertFileExists(Path::join($mappedDir, 'innerDir', 'test.file')); |
| 42 | + self::assertNotSame($tempDir, $mappedDir); |
| 43 | + self::assertStringContainsString(realpath(sys_get_temp_dir()), $mappedDir); |
| 44 | + } |
| 45 | + |
| 46 | + public function testMappingIsInFreshTempDir(): void |
| 47 | + { |
| 48 | + $filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file'); |
| 49 | + touch($filePath); |
| 50 | + |
| 51 | + $tempDir = Path::join(realpath(sys_get_temp_dir()), bin2hex(random_bytes(10))); |
| 52 | + @mkdir($tempDir); |
| 53 | + |
| 54 | + self::assertNotSame(InTempSolutionMapper::mapFile($filePath), InTempSolutionMapper::mapFile($filePath)); |
| 55 | + self::assertNotSame(InTempSolutionMapper::mapDirectory($tempDir), InTempSolutionMapper::mapDirectory($tempDir)); |
| 56 | + } |
| 57 | +} |
0 commit comments