Skip to content

Commit

Permalink
Replace VFS Stream with spatie temp directory
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Mar 6, 2024
1 parent 43260de commit af6eab8
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 337 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/phpmd.xml
/phpunit.xml
/.phpcs-cache
/.phpunit.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
"require-dev": {
"ext-zend-opcache": "*",
"doctrine/coding-standard": "^12.0.0",
"mikey179/vfsstream": "^1.6.11",
"phing/phing": "^2.17.4",
"phpstan/phpstan": "^1.10.59",
"phpunit/phpunit": "^10.5.11",
"roave/infection-static-analysis-plugin": "^1.34.0",
"spatie/temporary-directory": "^2.2",
"vimeo/psalm": "^5.22.2"
},
"config": {
Expand Down
114 changes: 62 additions & 52 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,40 @@
use ArrayObject;
use ComposerRequireChecker\ASTLocator\LocateASTFromFiles;
use ComposerRequireChecker\Exception\FileParseFailed;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PhpParser\ErrorHandler\Collecting;
use PhpParser\Lexer;
use PhpParser\Node\Stmt;
use PhpParser\Parser\Php7;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Spatie\TemporaryDirectory\TemporaryDirectory;

use function file_put_contents;
use function iterator_to_array;

/** @covers \ComposerRequireChecker\ASTLocator\LocateASTFromFiles */
final class LocateASTFromFilesTest extends TestCase
{
private LocateASTFromFiles $locator;
private vfsStreamDirectory $root;
private TemporaryDirectory $root;

protected function setUp(): void
{
parent::setUp();

$this->locator = new LocateASTFromFiles(new Php7(new Lexer()), null);
$this->root = vfsStream::setup();
$this->root = (new TemporaryDirectory())
->deleteWhenDestroyed()
->create();
}

public function testLocate(): void
{
$files = [
__DIR__ . '/.',
__DIR__ . '/..',
$this->createFile('MyClassA', '<?php class MyClassA {}'),
$this->createFile('MyClassB', '<?php class MyClassB {}'),
$this->createFile('MyClassA.php', '<?php class MyClassA {}'),
$this->createFile('MyClassB.php', '<?php class MyClassB {}'),
];

$roots = $this->locate($files);
Expand All @@ -48,12 +50,15 @@ public function testLocate(): void

public function testFailOnParseError(): void
{
$this->expectException(FileParseFailed::class);
$this->expectExceptionMessageMatches('/\[vfs:\/\/root\/MyBadCode\]/');
$files = [
$this->createFile('MyBadCode', '<?php this causes a parse error'),
$this->createFile('MyBadCode.php', '<?php this causes a parse error'),
];

$filePath = $files[0];

$this->expectException(FileParseFailed::class);
$this->expectExceptionMessage('[' . $filePath . ']');

$this->locate($files);
}

Expand All @@ -62,7 +67,7 @@ public function testDoNotFailOnParseErrorWithErrorHandler(): void
$collectingErrorHandler = new Collecting();
$this->locator = new LocateASTFromFiles(new Php7(new Lexer()), $collectingErrorHandler);
$files = [
$this->createFile('MyBadCode', '<?php this causes a parse error'),
$this->createFile('MyBadCode.php', '<?php this causes a parse error'),
];

$roots = $this->locate($files);
Expand All @@ -81,7 +86,7 @@ public function testFailOnParseErrorWithNullReturn(): void
$this->locator = new LocateASTFromFiles($parserMock, null);
$files = [
$this->createFile(
'MyBadCode',
'MyBadCode.php',
'this content is not relevant because the parser is mocked and always returns null',
),
];
Expand All @@ -91,7 +96,10 @@ public function testFailOnParseErrorWithNullReturn(): void

private function createFile(string $path, string|null $content = null): string
{
return vfsStream::newFile($path)->at($this->root)->setContent($content)->url();
$fullPath = $this->root->path($path);
file_put_contents($fullPath, $content);

return $fullPath;
}

/**
Expand Down
39 changes: 25 additions & 14 deletions test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use ComposerRequireChecker\Cli\Application;
use InvalidArgumentException;
use LogicException;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use Spatie\TemporaryDirectory\TemporaryDirectory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
Expand Down Expand Up @@ -175,20 +175,25 @@ public function testVerboseSelfCheckShowsCounts(): void

public function testWithAdditionalSourceFiles(): void
{
$root = vfsStream::setup();
vfsStream::create([
'config.json' => <<<'JSON'
$root = (new TemporaryDirectory())
->deleteWhenDestroyed()
->create();

$configFilePath = $root->path('config.json');
file_put_contents(
$configFilePath,
<<<'JSON'
{
"scan-files": ["src/ComposerRequireChecker/Cli/CheckCommand.php"]
}
JSON
,
]);
);

$this->commandTester->execute([
// that's our own composer.json
'composer-json' => dirname(__DIR__, 3) . '/composer.json',
'--config-file' => $root->getChild('config.json')->url(),
'--config-file' => $configFilePath,
]);

$this->assertMatchesRegularExpression(
Expand All @@ -199,15 +204,17 @@ public function testWithAdditionalSourceFiles(): void

public function testSourceFileThatUsesDevDependency(): void
{
$root = vfsStream::setup();
vfsStream::create(
['config.json' => '{"scan-files":["test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php"]}'],
);
$root = (new TemporaryDirectory())
->deleteWhenDestroyed()
->create();

$configFilePath = $root->path('config.json');
file_put_contents($configFilePath, '{"scan-files":["test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php"]}');

$exitCode = $this->commandTester->execute([
// that's our own composer.json
'composer-json' => dirname(__DIR__, 3) . '/composer.json',
'--config-file' => $root->getChild('config.json')->url(),
'--config-file' => $configFilePath,
]);

$this->assertNotEquals(0, $exitCode);
Expand Down Expand Up @@ -298,13 +305,17 @@ public function testDefaultConfigPath(): void
public function testOverrideDefaultConfigPath(): void
{
$baseDirectory = dirname(__DIR__, 2) . '/fixtures/defaultConfigPath/';
$root = vfsStream::setup();
vfsStream::create(['config.json' => '{"scan-files": []}']);
$root = (new TemporaryDirectory())
->deleteWhenDestroyed()
->create();

$configFilePath = $root->path('config.json');
file_put_contents($configFilePath, '{"scan-files":[]}');

chdir($baseDirectory);
$exitCode = $this->commandTester->execute([
'composer-json' => 'composer.json',
'--config-file' => $root->getChild('config.json')->url(),
'--config-file' => $configFilePath,
]);

$output = $this->commandTester->getDisplay();
Expand Down
Loading

0 comments on commit af6eab8

Please sign in to comment.