Skip to content

Use directory utils #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions test/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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;
}
Expand Down
89 changes: 35 additions & 54 deletions test/Solution/DirectorySolutionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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()));
Expand All @@ -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()));
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
63 changes: 32 additions & 31 deletions test/Solution/InTempSolutionMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 3 additions & 9 deletions test/Solution/SingleFileSolutionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@
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);

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);
}
}
Loading