Skip to content
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
6 changes: 5 additions & 1 deletion composer-dependency-analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
->ignoreErrorsOnPackage('symfony/config', [ErrorType::DEV_DEPENDENCY_IN_PROD])
->ignoreErrorsOnPackage('symfony/dependency-injection', [ErrorType::DEV_DEPENDENCY_IN_PROD])

// test fixture
// test fixtures
->ignoreErrorsOnPath(
__DIR__ . '/tests/EntityClassResolver/Fixture/Anything/SomeAttributeDocument.php',
[ErrorType::UNKNOWN_CLASS]
)
->ignoreErrorsOnPath(
__DIR__ . '/tests/PhpParser/NodeVisitor/MockedClassNameCollectingNodeVisitor/Fixture',
[ErrorType::UNKNOWN_CLASS]
);
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"symfony/console": "^6.4.24",
"symfony/finder": "^7.4",
"symfony/yaml": "^7.4",
"webmozart/assert": "^1.12"
"webmozart/assert": "^2.1"
},
"require-dev": {
"symplify/easy-coding-standard": "^13.0",
Expand Down
6 changes: 4 additions & 2 deletions src/Command/GenerateSymfonyConfigBuildersCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Command;

use Entropy\Console\Contract\CommandInterface;
Expand All @@ -17,7 +19,7 @@
/**
* @see https://github.com/nelmio/alice/blob/v2.3.0/doc/complete-reference.md#php
*/
final class GenerateSymfonyConfigBuildersCommand implements CommandInterface
final readonly class GenerateSymfonyConfigBuildersCommand implements CommandInterface
{
/**
* @var string[]
Expand All @@ -34,7 +36,7 @@ final class GenerateSymfonyConfigBuildersCommand implements CommandInterface
];

public function __construct(
private readonly SymfonyStyle $symfonyStyle,
private SymfonyStyle $symfonyStyle,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function enterNode(Node $node): ?Node
'mock', // https://github.com/mockery/mockery/blob/73a9714716f87510a7c2add9931884188e657541/library/Mockery.php#L475, https://github.com/laravel/framework/blob/4ca4a16772b2e89233b3606badefae34003e1538/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php#L69
'partialMock', // https://github.com/laravel/framework/blob/4ca4a16772b2e89233b3606badefae34003e1538/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php#L81
'spy', // https://github.com/mockery/mockery/blob/73a9714716f87510a7c2add9931884188e657541/library/Mockery.php#L675, https://github.com/laravel/framework/blob/4ca4a16772b2e89233b3606badefae34003e1538/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php#L93
'createStub', // https://github.com/sebastianbergmann/phpunit/blob/d72b735d34bbff2065cef80653cafbe31cb45ba0/src/Framework/TestCase.php#L2262
'createStubForIntersectionOfInterfaces', // https://github.com/sebastianbergmann/phpunit/blob/d72b735d34bbff2065cef80653cafbe31cb45ba0/src/Framework/TestCase.php#L2285
'createConfiguredStub', // https://github.com/sebastianbergmann/phpunit/blob/d72b735d34bbff2065cef80653cafbe31cb45ba0/src/Framework/TestCase.php#2312
];
if (! in_array($methodName, $mockMethodNames, true)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\PhpParser\NodeVisitor\MockedClassNameCollectingNodeVisitor\Fixture;

use PHPUnit\Framework\TestCase;

final class CreateConfiguredStubCall extends TestCase
{
public function test(): void
{
$stub = $this->createConfiguredStub(\SomeNamespace\SomeConfiguredStubClass::class, []);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\PhpParser\NodeVisitor\MockedClassNameCollectingNodeVisitor\Fixture;

use PHPUnit\Framework\TestCase;

final class CreateMockCall extends TestCase
{
public function test(): void
{
$mock = $this->createMock(\SomeNamespace\SomeMockedClass::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\PhpParser\NodeVisitor\MockedClassNameCollectingNodeVisitor\Fixture;

use PHPUnit\Framework\TestCase;

final class CreateStubCall extends TestCase
{
public function test(): void
{
$stub = $this->createStub(\SomeNamespace\SomeStubClass::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\PhpParser\NodeVisitor\MockedClassNameCollectingNodeVisitor\Fixture;

use PHPUnit\Framework\TestCase;

final class CreateStubForIntersectionCall extends TestCase
{
public function test(): void
{
$stub = $this->createStubForIntersectionOfInterfaces(\SomeNamespace\SomeIntersectionClass::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\PhpParser\NodeVisitor\MockedClassNameCollectingNodeVisitor\Fixture;

use PHPUnit\Framework\TestCase;

final class UnrelatedMethodCall extends TestCase
{
public function test(): void
{
$this->assertTrue(true);
$this->someRandomMethod(\SomeNamespace\ShouldNotBeDetected::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\PhpParser\NodeVisitor\MockedClassNameCollectingNodeVisitor;

use Iterator;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rector\SwissKnife\PhpParser\NodeVisitor\MockedClassNameCollectingNodeVisitor;

final class MockedClassNameCollectingNodeVisitorTest extends TestCase
{
/**
* @param string[] $expectedClassNames
*/
#[DataProvider('provideData')]
public function test(string $filePath, array $expectedClassNames): void
{
$mockedClassNameCollectingNodeVisitor = new MockedClassNameCollectingNodeVisitor();

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($mockedClassNameCollectingNodeVisitor);

$parser = (new ParserFactory())->createForNewestSupportedVersion();
$stmts = $parser->parse((string) file_get_contents($filePath));
$this->assertNotNull($stmts);

$nodeTraverser->traverse($stmts);

$this->assertSame($expectedClassNames, $mockedClassNameCollectingNodeVisitor->getMockedClassNames());
}

/**
* @return Iterator<string, array{string, string[]}>
*/
public static function provideData(): Iterator
{
yield 'createMock' => [__DIR__ . '/Fixture/CreateMockCall.php', ['SomeNamespace\SomeMockedClass']];

yield 'createStub' => [__DIR__ . '/Fixture/CreateStubCall.php', ['SomeNamespace\SomeStubClass']];

yield 'createStubForIntersectionOfInterfaces' => [
__DIR__ . '/Fixture/CreateStubForIntersectionCall.php',
['SomeNamespace\SomeIntersectionClass'],
];

yield 'createConfiguredStub' => [
__DIR__ . '/Fixture/CreateConfiguredStubCall.php',
['SomeNamespace\SomeConfiguredStubClass'],
];

yield 'unrelated method is not detected' => [__DIR__ . '/Fixture/UnrelatedMethodCall.php', []];
}
}
Loading