Skip to content

Commit

Permalink
[DX] Make use of Laravel container in few tests - step #2 (#4678)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 6, 2023
1 parent 56195b3 commit a82c863
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@
use Rector\BetterPhpDocParser\PhpDoc\StringNode;
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\ArrayParser;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class ArrayParserTest extends AbstractTestCase
final class ArrayParserTest extends AbstractLazyTestCase
{
private ArrayParser $arrayParser;

private TokenIteratorFactory $tokenIteratorFactory;

protected function setUp(): void
{
$this->boot();

$this->arrayParser = $this->getService(ArrayParser::class);
$this->tokenIteratorFactory = $this->getService(TokenIteratorFactory::class);
$this->arrayParser = $this->make(ArrayParser::class);
$this->tokenIteratorFactory = $this->make(TokenIteratorFactory::class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class StaticDoctrineAnnotationParserTest extends AbstractTestCase
final class StaticDoctrineAnnotationParserTest extends AbstractLazyTestCase
{
private StaticDoctrineAnnotationParser $staticDoctrineAnnotationParser;

private TokenIteratorFactory $tokenIteratorFactory;

protected function setUp(): void
{
$this->boot();

$this->tokenIteratorFactory = $this->getService(TokenIteratorFactory::class);
$this->staticDoctrineAnnotationParser = $this->getService(StaticDoctrineAnnotationParser::class);
$this->tokenIteratorFactory = $this->make(TokenIteratorFactory::class);
$this->staticDoctrineAnnotationParser = $this->make(StaticDoctrineAnnotationParser::class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,27 @@
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source\RectorWithChangelog;
use Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source\RectorWithOutChangelog;

final class RectorsChangelogResolverTest extends AbstractTestCase
final class RectorsChangelogResolverTest extends AbstractLazyTestCase
{
private RectorsChangelogResolver $rectorsChangelogResolver;

private FileDiff $fileDiff;

private FilePathHelper $filePathHelper;

protected function setUp(): void
{
$this->boot();
$this->rectorsChangelogResolver = $this->getService(RectorsChangelogResolver::class);
$this->filePathHelper = $this->getService(FilePathHelper::class);

$this->fileDiff = $this->createFileDiff();
$this->rectorsChangelogResolver = $this->make(RectorsChangelogResolver::class);
$this->filePathHelper = $this->make(FilePathHelper::class);
}

public function test(): void
{
$rectorsChangelogs = $this->rectorsChangelogResolver->resolve($this->fileDiff->getRectorClasses());
$fileDiff = $this->createFileDiff();

$rectorsChangelogs = $this->rectorsChangelogResolver->resolve($fileDiff->getRectorClasses());

$expectedRectorsChangelogs = [
RectorWithChangelog::class => 'https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@

use Nette\Utils\FileSystem;
use Rector\NodeTypeResolver\DependencyInjection\BleedingEdgeIncludePurifier;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class BleedingEdgeIncludePurifierTest extends AbstractTestCase
final class BleedingEdgeIncludePurifierTest extends AbstractLazyTestCase
{
private BleedingEdgeIncludePurifier $bleedingEdgeIncludePurifier;

protected function setUp(): void
{
$this->boot();

$this->bleedingEdgeIncludePurifier = $this->getService(BleedingEdgeIncludePurifier::class);
$this->bleedingEdgeIncludePurifier = $this->make(BleedingEdgeIncludePurifier::class);
}

public function testNothing(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
use PHPStan\Type\Type;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\NodeTypeResolver\TypeComparator\ScalarTypeComparator;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class ScalarTypeComparatorTest extends AbstractTestCase
final class ScalarTypeComparatorTest extends AbstractLazyTestCase
{
private ScalarTypeComparator $scalarTypeComparator;

protected function setUp(): void
{
$this->boot();
$this->scalarTypeComparator = $this->getService(ScalarTypeComparator::class);
$this->scalarTypeComparator = $this->make(ScalarTypeComparator::class);
}

#[DataProvider('provideData')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,48 @@

namespace Rector\Tests\PhpAttribute\AnnotationToAttributeMapper;

use Iterator;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class AnnotationToAttributeMapperTest extends AbstractTestCase
final class AnnotationToAttributeMapperTest extends AbstractLazyTestCase
{
private AnnotationToAttributeMapper $annotationToAttributeMapper;

protected function setUp(): void
{
$this->boot();
$this->annotationToAttributeMapper = $this->getService(AnnotationToAttributeMapper::class);
$this->annotationToAttributeMapper = $this->make(AnnotationToAttributeMapper::class);
}

public function test(): void
/**
* @param class-string<Expr> $expectedTypeClass
*/
#[DataProvider('provideData')]
public function test(mixed $input, string $expectedTypeClass): void
{
$mappedExpr = $this->annotationToAttributeMapper->map(false);
$this->assertInstanceOf(ConstFetch::class, $mappedExpr);

$mappedExpr = $this->annotationToAttributeMapper->map('false');
$this->assertInstanceOf(ConstFetch::class, $mappedExpr);

$mappedExpr = $this->annotationToAttributeMapper->map('100');
$this->assertInstanceOf(LNumber::class, $mappedExpr);

$mappedExpr = $this->annotationToAttributeMapper->map('hey');
$this->assertInstanceOf(String_::class, $mappedExpr);

$expr = $this->annotationToAttributeMapper->map(['hey']);
$this->assertInstanceOf(Array_::class, $expr);
$mappedExpr = $this->annotationToAttributeMapper->map($input);
$this->assertInstanceOf($expectedTypeClass, $mappedExpr);

if ($mappedExpr instanceof Array_) {
$arrayItem = $mappedExpr->items[0];
$this->assertInstanceOf(ArrayItem::class, $arrayItem);
$this->assertInstanceOf(String_::class, $arrayItem->value);
}
}

$arrayItem = $expr->items[0];
$this->assertInstanceOf(ArrayItem::class, $arrayItem);
$this->assertInstanceOf(String_::class, $arrayItem->value);
public static function provideData(): Iterator
{
yield [false, ConstFetch::class];
yield ['false', ConstFetch::class];
yield ['100', LNumber::class];
yield ['hey', String_::class];
yield [['hey'], Array_::class];
}
}
8 changes: 6 additions & 2 deletions packages/PhpAttribute/AnnotationToAttributeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ final class AnnotationToAttributeMapper
private array $annotationToAttributeMappers = [];

/**
* @param RewindableGenerator<AnnotationToAttributeMapperInterface> $annotationToAttributeMappers
* @param RewindableGenerator<AnnotationToAttributeMapperInterface>|AnnotationToAttributeMapperInterface[] $annotationToAttributeMappers
*/
public function __construct(iterable $annotationToAttributeMappers)
{
$this->annotationToAttributeMappers = iterator_to_array($annotationToAttributeMappers->getIterator());
if ($annotationToAttributeMappers instanceof RewindableGenerator) {
$this->annotationToAttributeMappers = iterator_to_array($annotationToAttributeMappers->getIterator());
} else {
$this->annotationToAttributeMappers = $annotationToAttributeMappers;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public function __construct(
) {
}

/**
* Avoid circular reference
*/
#[Required]
public function autowire(AnnotationToAttributeMapper $annotationToAttributeMapper): void
{
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,6 @@ parameters:
-
message: '#Parameters should use "Symfony\\Component\\Console\\Application\|string\|callable" types as the only types passed to this method#'
path: src/Util/Reflection/PrivatesAccessor.php

# false positives
- '#Property Rector\\PhpAttribute\\AnnotationToAttributeMapper\:\:\$annotationToAttributeMappers \(array<Rector\\PhpAttribute\\Contract\\AnnotationToAttributeMapperInterface>\) does not accept iterable<Rector\\PhpAttribute\\Contract\\AnnotationToAttributeMapperInterface>#'
7 changes: 3 additions & 4 deletions rules-tests/Naming/Naming/PropertyNamingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Naming\Naming\PropertyNaming;
use Rector\Naming\ValueObject\ExpectedName;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class PropertyNamingTest extends AbstractTestCase
final class PropertyNamingTest extends AbstractLazyTestCase
{
private PropertyNaming $propertyNaming;

protected function setUp(): void
{
$this->boot();
$this->propertyNaming = $this->getService(PropertyNaming::class);
$this->propertyNaming = $this->make(PropertyNaming::class);
}

#[DataProvider('getExpectedNameFromMethodNameDataProvider')]
Expand Down
63 changes: 59 additions & 4 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\ArrayAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\ArrayItemNodeAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\ClassConstFetchAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\ConstExprNodeAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\CurlyListNodeAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\DoctrineAnnotationAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\StringAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\StringNodeAnnotationToAttributeMapper;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
Expand Down Expand Up @@ -63,6 +73,20 @@ final class LazyContainerFactory
VariableNameResolver::class,
];

/**
* @var array<class-string<AnnotationToAttributeMapperInterface>>
*/
private const ANNOTATION_TO_ATTRIBUTE_MAPPER_CLASSES = [
ArrayAnnotationToAttributeMapper::class,
ArrayItemNodeAnnotationToAttributeMapper::class,
ClassConstFetchAnnotationToAttributeMapper::class,
ConstExprNodeAnnotationToAttributeMapper::class,
CurlyListNodeAnnotationToAttributeMapper::class,
DoctrineAnnotationAnnotationToAttributeMapper::class,
StringAnnotationToAttributeMapper::class,
StringNodeAnnotationToAttributeMapper::class,
];

/**
* @api used as next container factory
*/
Expand Down Expand Up @@ -127,10 +151,29 @@ public function create(): Container
->needs('$nodeNameResolvers')
->giveTagged(NodeNameResolverInterface::class);

foreach (self::NODE_NAME_RESOLVER_CLASSES as $nodeNameResolverClass) {
$container->singleton($nodeNameResolverClass);
$container->tag($nodeNameResolverClass, NodeNameResolverInterface::class);
}
$this->registerTagged($container, self::NODE_NAME_RESOLVER_CLASSES, NodeNameResolverInterface::class);

$container->when(AnnotationToAttributeMapper::class)
->needs('$annotationToAttributeMappers')
->giveTagged(AnnotationToAttributeMapperInterface::class);

$this->registerTagged(
$container,
self::ANNOTATION_TO_ATTRIBUTE_MAPPER_CLASSES,
AnnotationToAttributeMapperInterface::class
);

// #[Required]-like setter
$container->afterResolving(
ArrayAnnotationToAttributeMapper::class,
static function (
ArrayAnnotationToAttributeMapper $arrayAnnotationToAttributeMapper,
Container $container
): void {
$annotationToAttributesMapper = $container->make(AnnotationToAttributeMapper::class);
$arrayAnnotationToAttributeMapper->autowire($annotationToAttributesMapper);
}
);

$container->singleton(Parser::class, static function (Container $container) {
$phpstanServiceFactory = $container->make(PHPStanServicesFactory::class);
Expand Down Expand Up @@ -169,4 +212,16 @@ static function (Container $container): ReflectionProvider {

return $container;
}

/**
* @param array<class-string> $classes
* @param class-string $tagInterface
*/
private function registerTagged(Container $container, array $classes, string $tagInterface): void
{
foreach ($classes as $class) {
$container->singleton($class);
$container->tag($class, $tagInterface);
}
}
}
12 changes: 7 additions & 5 deletions tests/FileSystem/FilesFinder/ExcludePaths/ExcludePathsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

namespace Rector\Core\Tests\FileSystem\FilesFinder\ExcludePaths;

use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\FileSystem\FilesFinder;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class ExcludePathsTest extends AbstractTestCase
final class ExcludePathsTest extends AbstractLazyTestCase
{
public function testShouldFail(): void
public function test(): void
{
$this->bootFromConfigFiles([__DIR__ . '/config/config-with-excluded-paths.php']);
SimpleParameterProvider::setParameter(Option::SKIP, ['*/ShouldBeExcluded/*']);

$filesFinder = $this->getService(FilesFinder::class);
$filesFinder = $this->make(FilesFinder::class);

$foundFileInfos = $filesFinder->findInDirectoriesAndFiles([__DIR__ . '/Source'], ['php']);
$this->assertCount(1, $foundFileInfos);
Expand Down

This file was deleted.

8 changes: 3 additions & 5 deletions tests/PhpParser/Node/NodeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@
use PhpParser\Node\Scalar\String_;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class NodeFactoryTest extends AbstractTestCase
final class NodeFactoryTest extends AbstractLazyTestCase
{
private NodeFactory $nodeFactory;

protected function setUp(): void
{
$this->boot();

$this->nodeFactory = $this->getService(NodeFactory::class);
$this->nodeFactory = $this->make(NodeFactory::class);
}

/**
Expand Down

0 comments on commit a82c863

Please sign in to comment.