Skip to content

Commit

Permalink
[Testing] Use own FixtureFileFinder to keep dependency low (#2858)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 30, 2022
1 parent b01ea04 commit 86ccf3f
Show file tree
Hide file tree
Showing 23 changed files with 243 additions and 109 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"symplify/autowire-array-parameter": "^11.1",
"symplify/composer-json-manipulator": "^11.1",
"symplify/easy-parallel": "^11.1",
"symplify/easy-testing": "^11.1",
"symplify/package-builder": "^11.1",
"symplify/rule-doc-generator-contracts": "^11.1",
"symplify/skipper": "^11.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;

abstract class AbstractPhpDocInfoPrinterTest extends AbstractTestCase
{
Expand All @@ -39,6 +39,6 @@ protected function createPhpDocInfoFromDocCommentAndNode(string $docComment, Nod

protected function yieldFilesFromDirectory(string $directory, string $suffix = '*.php'): Iterator
{
return StaticFixtureFinder::yieldDirectory($directory, $suffix);
return FixtureFileFinder::yieldDirectory($directory, $suffix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ public function testClass(string $docFilePath, Node $node): void
$docComment = FileSystem::read($docFilePath);
$phpDocInfo = $this->createPhpDocInfoFromDocCommentAndNode($docComment, $node);

$relativeFilePathFromCwd = $this->filePathHelper->relativePath($docFilePath);

$printedPhpDocInfo = $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);
$this->assertSame($docComment, $printedPhpDocInfo, $relativeFilePathFromCwd);
$this->assertSame($docComment, $printedPhpDocInfo);
}

public function provideDataClass(): Iterator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\TestingParser\TestingParser;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ResolveTagToKnownFullyQualifiedNameTest extends AbstractTestCase
Expand Down Expand Up @@ -75,6 +75,6 @@ public function testResolvesClass(SmartFileInfo $file): void
*/
public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Iterator;
use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
Expand All @@ -14,10 +15,8 @@
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\FileSystemRector\Parser\FileInfoParser;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\EasyTesting\FixtureSplitter\TrioFixtureSplitter;
use Symplify\EasyTesting\ValueObject\FixtureSplit\TrioContent;
use Symplify\SmartFileSystem\SmartFileInfo;

final class TagValueNodeReprintTest extends AbstractTestCase
Expand Down Expand Up @@ -49,13 +48,15 @@ protected function setUp(): void
*/
public function test(SmartFileInfo $fixtureFileInfo): void
{
$trioFixtureSplitter = new TrioFixtureSplitter();
$trioContent = $trioFixtureSplitter->splitFileInfo($fixtureFileInfo);
[$fileContents, $nodeClass, $tagValueNodeClasses] = Strings::split(
$fixtureFileInfo->getContents(),
"#-----\n#"
);
$nodeClass = trim((string) $nodeClass);
$tagValueNodeClasses = $this->splitListByEOL($tagValueNodeClasses);

$nodeClass = trim($trioContent->getSecondValue());
$tagValueNodeClasses = $this->splitListByEOL($trioContent->getExpectedResult());
$fixtureFileInfo = $this->createFixtureFileInfo($fileContents, $fixtureFileInfo);

$fixtureFileInfo = $this->createFixtureFileInfo($trioContent, $fixtureFileInfo);
foreach ($tagValueNodeClasses as $tagValueNodeClass) {
$this->doTestPrintedPhpDocInfo($fixtureFileInfo, $tagValueNodeClass, $nodeClass);
}
Expand All @@ -66,15 +67,15 @@ public function test(SmartFileInfo $fixtureFileInfo): void
*/
public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture');
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideDataNested(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/FixtureNested');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/FixtureNested');
}

/**
Expand Down Expand Up @@ -112,12 +113,10 @@ private function splitListByEOL(string $content): array
return explode(PHP_EOL, $trimmedContent);
}

private function createFixtureFileInfo(TrioContent $trioContent, SmartFileInfo $fixturefileInfo): SmartFileInfo
private function createFixtureFileInfo(string $fileContents, SmartFileInfo $fixturefileInfo): SmartFileInfo
{
$temporaryFileName = sys_get_temp_dir() . '/rector/tests/' . $fixturefileInfo->getRelativePathname();
$firstValue = $trioContent->getFirstValue();

FileSystem::write($temporaryFileName, $firstValue);
FileSystem::write($temporaryFileName, $fileContents);

return new SmartFileInfo($temporaryFileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\FileSystemRector\Parser\FileInfoParser;
use Rector\Testing\Fixture\FixtureSplitter;
use Rector\Testing\Fixture\FixtureTempFileDumper;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\EasyTesting\StaticFixtureSplitter;
use Symplify\SmartFileSystem\SmartFileInfo;

final class TestModifyReprintTest extends AbstractTestCase
{
Expand All @@ -43,12 +43,11 @@ protected function setUp(): void

public function test(): void
{
$fixtureFileInfo = new SmartFileInfo(__DIR__ . '/FixtureModify/route_with_extra_methods.php.inc');

$inputFileInfoAndExpected = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpected($fixtureFileInfo);
$inputFileInfo = $inputFileInfoAndExpected->getInputFileInfo();
[$inputContent, $expectedContent] = FixtureSplitter::loadFileAndSplitInputAndExpected(
__DIR__ . '/FixtureModify/route_with_extra_methods.php.inc'
);

$phpDocInfo = $this->parseFileAndGetFirstNodeOfType($inputFileInfo, ClassMethod::class);
$phpDocInfo = $this->parseFileAndGetFirstNodeOfType($inputContent, ClassMethod::class);

/** @var DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode */
$doctrineAnnotationTagValueNode = $phpDocInfo->findOneByAnnotationClass(
Expand All @@ -62,7 +61,7 @@ public function test(): void
]);
$doctrineAnnotationTagValueNode->values[] = new ArrayItemNode($methodsCurlyListNode, 'methods');

$expectedDocContent = trim((string) $inputFileInfoAndExpected->getExpected());
$expectedDocContent = trim((string) $expectedContent);

$printedPhpDocInfo = $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);
$this->assertSame($expectedDocContent, $printedPhpDocInfo);
Expand All @@ -71,13 +70,14 @@ public function test(): void
/**
* @param class-string<Node> $nodeType
*/
private function parseFileAndGetFirstNodeOfType(SmartFileInfo $smartFileInfo, string $nodeType): PhpDocInfo
private function parseFileAndGetFirstNodeOfType(string $fileContents, string $nodeType): PhpDocInfo
{
$nodes = $this->fileInfoParser->parseFileInfoToNodesAndDecorate($smartFileInfo);
$fixtureFileInfo = FixtureTempFileDumper::dump($fileContents);
$nodes = $this->fileInfoParser->parseFileInfoToNodesAndDecorate($fixtureFileInfo);

$node = $this->betterNodeFinder->findFirstInstanceOf($nodes, $nodeType);
if (! $node instanceof Node) {
throw new ShouldNotHappenException($smartFileInfo->getRealPath());
throw new ShouldNotHappenException($fileContents);
}

return $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
Expand Down
16 changes: 8 additions & 8 deletions packages-tests/Comments/CommentRemover/CommentRemoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\FileSystemRector\Parser\FileInfoParser;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\Fixture\FixtureSplitter;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\EasyTesting\StaticFixtureSplitter;
use Symplify\SmartFileSystem\SmartFileInfo;

final class CommentRemoverTest extends AbstractTestCase
Expand All @@ -25,6 +25,7 @@ final class CommentRemoverTest extends AbstractTestCase
protected function setUp(): void
{
$this->boot();

$this->commentRemover = $this->getService(CommentRemover::class);
$this->fileInfoParser = $this->getService(FileInfoParser::class);
$this->nodePrinter = $this->getService(BetterStandardPrinter::class);
Expand All @@ -35,18 +36,17 @@ protected function setUp(): void
*/
public function test(SmartFileInfo $smartFileInfo): void
{
$fileInfoToLocalInputAndExpected = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpected($smartFileInfo);

$nodes = $this->fileInfoParser->parseFileInfoToNodesAndDecorate(
$fileInfoToLocalInputAndExpected->getInputFileInfo()
[$inputContents, $expectedOutputContents] = FixtureSplitter::loadFileAndSplitInputAndExpected(
$smartFileInfo->getRealPath()
);

$nodes = $this->fileInfoParser->parseFileInfoToNodesAndDecorate($inputContents);
$nodesWithoutComments = $this->commentRemover->removeFromNode($nodes);

$fileContent = $this->nodePrinter->print($nodesWithoutComments);
$fileContent = trim($fileContent);

$expectedContent = trim((string) $fileInfoToLocalInputAndExpected->getExpected());
$expectedContent = trim((string) $expectedOutputContents);
$this->assertSame($fileContent, $expectedContent);

// original nodes are not touched
Expand All @@ -56,6 +56,6 @@ public function test(SmartFileInfo $smartFileInfo): void

public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.php.inc');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use PhpParser\Node\Expr\PropertyFetch;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\Fixture\FixtureSplitter;
use Rector\Testing\Fixture\FixtureTempFileDumper;
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\AbstractNodeTypeResolverTest;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\EasyTesting\StaticFixtureSplitter;
use Symplify\SmartFileSystem\SmartFileInfo;

final class PropertyFetchTypeResolverTest extends AbstractNodeTypeResolverTest
Expand All @@ -28,21 +29,23 @@ public function test(SmartFileInfo $smartFileInfo): void
*/
public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectoryExclusively(__DIR__ . '/Fixture');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture');
}

private function doTestFileInfo(SmartFileInfo $smartFileInfo): void
{
$inputFileInfoAndExpectedFileInfo = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos(
$smartFileInfo
[$inputFileContents, $expectedType] = FixtureSplitter::loadFileAndSplitInputAndExpected(
$smartFileInfo->getRealPath()
);
$inputFileInfo = $inputFileInfoAndExpectedFileInfo->getInputFileInfo();
$expectedFileInfo = $inputFileInfoAndExpectedFileInfo->getExpectedFileInfo();

$inputFileInfo = FixtureTempFileDumper::dump($inputFileContents);

$propertyFetchNodes = $this->getNodesForFileOfType($inputFileInfo->getRealPath(), PropertyFetch::class);
$resolvedType = $this->nodeTypeResolver->getType($propertyFetchNodes[0]);

$expectedType = include $expectedFileInfo->getRealPath();
// this file actually containts PHP for type
$typeFileInfo = FixtureTempFileDumper::dump($expectedType);
$expectedType = include $typeFileInfo->getRealPath();

$expectedTypeAsString = $this->getStringFromType($expectedType);
$resolvedTypeAsString = $this->getStringFromType($resolvedType);
Expand Down
10 changes: 9 additions & 1 deletion packages/FileSystemRector/Parser/FileInfoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Rector\Testing\Fixture\FixtureTempFileDumper;
use Symplify\SmartFileSystem\SmartFileInfo;

/**
* Only for testing, @todo move to testing
*/
final class FileInfoParser
{
public function __construct(
Expand All @@ -23,8 +27,12 @@ public function __construct(
/**
* @return Stmt[]
*/
public function parseFileInfoToNodesAndDecorate(SmartFileInfo $smartFileInfo): array
public function parseFileInfoToNodesAndDecorate(SmartFileInfo|string $smartFileInfo): array
{
if (is_string($smartFileInfo)) {
$smartFileInfo = FixtureTempFileDumper::dump($smartFileInfo);
}

$stmts = $this->rectorParser->parseFile($smartFileInfo);
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);

Expand Down
41 changes: 41 additions & 0 deletions packages/Testing/Fixture/FixtureFileFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Rector\Testing\Fixture;

use Iterator;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symplify\SmartFileSystem\SmartFileInfo;

final class FixtureFileFinder
{
/**
* @return Iterator<array<int, SmartFileInfo>>
*/
public static function yieldDirectory(string $directory, string $suffix = '*.php.inc'): Iterator
{
$fileInfos = self::findFilesInDirectory($directory, $suffix);

foreach ($fileInfos as $fileInfo) {
// @todo is this one needed? maybe symfony is good enough :)
$smartFileInfo = new SmartFileInfo($fileInfo->getRealPath());
yield [$smartFileInfo];
}
}

/**
* @return SplFileInfo[]
*/
private static function findFilesInDirectory(string $directory, string $suffix): array
{
$finder = (new Finder())
->in($directory)
->files()
->name($suffix);

$fileInfos = iterator_to_array($finder);
return array_values($fileInfos);
}
}
28 changes: 28 additions & 0 deletions packages/Testing/Fixture/FixtureSplitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Rector\Testing\Fixture;

use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use Webmozart\Assert\Assert;

final class FixtureSplitter
{
/**
* @var string
* @see https://regex101.com/r/zZDoyy/1
*/
public const SPLIT_LINE_REGEX = '#\-\-\-\-\-\r?\n#';

/**
* @return array<string, string>
*/
public static function loadFileAndSplitInputAndExpected(string $filePath): array
{
Assert::fileExists($filePath);
$fixtureFileContents = FileSystem::read($filePath);

return Strings::split($fixtureFileContents, self::SPLIT_LINE_REGEX);
}
}
Loading

0 comments on commit 86ccf3f

Please sign in to comment.