diff --git a/config/sets/post-run-set.php b/config/sets/post-run-set.php deleted file mode 100644 index f989c277..00000000 --- a/config/sets/post-run-set.php +++ /dev/null @@ -1,12 +0,0 @@ -rules([CompleteMissingSetUpPropertyRector::class, SplitSetUpMockRector::class]); -}; diff --git a/rules-tests/Rector/Class_/CompleteMissingSetUpPropertyRector/CompleteMissingSetUpPropertyRectorTest.php b/rules-tests/Rector/Class_/CompleteMissingSetUpPropertyRector/CompleteMissingSetUpPropertyRectorTest.php deleted file mode 100644 index b702543c..00000000 --- a/rules-tests/Rector/Class_/CompleteMissingSetUpPropertyRector/CompleteMissingSetUpPropertyRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Rector/Class_/CompleteMissingSetUpPropertyRector/Fixture/create_empty_call.php.inc b/rules-tests/Rector/Class_/CompleteMissingSetUpPropertyRector/Fixture/create_empty_call.php.inc deleted file mode 100644 index 03f57b89..00000000 --- a/rules-tests/Rector/Class_/CompleteMissingSetUpPropertyRector/Fixture/create_empty_call.php.inc +++ /dev/null @@ -1,36 +0,0 @@ -someCall(); - } -} - -?> ------ -someVariable = new \someVariable(); - } - public function testSomething() - { - $this->someVariable->someCall(); - } -} - -?> diff --git a/rules-tests/Rector/Class_/CompleteMissingSetUpPropertyRector/config/configured_rule.php b/rules-tests/Rector/Class_/CompleteMissingSetUpPropertyRector/config/configured_rule.php deleted file mode 100644 index a3aa8d54..00000000 --- a/rules-tests/Rector/Class_/CompleteMissingSetUpPropertyRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rules([CompleteMissingSetUpPropertyRector::class]); -}; diff --git a/rules-tests/Rector/Class_/SplitSetUpMockRector/Fixture/some_setup_mock_calls.php.inc b/rules-tests/Rector/Class_/SplitSetUpMockRector/Fixture/some_setup_mock_calls.php.inc deleted file mode 100644 index 970a97a0..00000000 --- a/rules-tests/Rector/Class_/SplitSetUpMockRector/Fixture/some_setup_mock_calls.php.inc +++ /dev/null @@ -1,59 +0,0 @@ -someClassMock = $this->createMock(SomeClass::class); - $this->someClassMock->expect($this->once())->method('someMethod')->willReturn('someValue'); - } - - public function testSome() - { - $item = 1; - } - - public function testAnother() - { - $item = 2; - $this->someClassMock->expect($this->once())->method('someMethod')->willReturn('differentValue'); - } -} - -?> ------ -someClassMock = $this->createMock(SomeClass::class); - } - - public function testSome() - { - $this->someClassMock->expect($this->once())->method('someMethod')->willReturn('someValue'); - $item = 1; - } - - public function testAnother() - { - $item = 2; - $this->someClassMock->expect($this->once())->method('someMethod')->willReturn('differentValue'); - } -} - -?> diff --git a/rules-tests/Rector/Class_/SplitSetUpMockRector/SplitSetUpMockRectorTest.php b/rules-tests/Rector/Class_/SplitSetUpMockRector/SplitSetUpMockRectorTest.php deleted file mode 100644 index 07d380ad..00000000 --- a/rules-tests/Rector/Class_/SplitSetUpMockRector/SplitSetUpMockRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Rector/Class_/SplitSetUpMockRector/config/configured_rule.php b/rules-tests/Rector/Class_/SplitSetUpMockRector/config/configured_rule.php deleted file mode 100644 index a27853f9..00000000 --- a/rules-tests/Rector/Class_/SplitSetUpMockRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rules([SplitSetUpMockRector::class]); -}; diff --git a/rules/Rector/Class_/CompleteMissingSetUpPropertyRector.php b/rules/Rector/Class_/CompleteMissingSetUpPropertyRector.php deleted file mode 100644 index 7880f075..00000000 --- a/rules/Rector/Class_/CompleteMissingSetUpPropertyRector.php +++ /dev/null @@ -1,169 +0,0 @@ -> - */ - public function getNodeTypes(): array - { - return [Class_::class]; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition( - 'Add missing tested property to setUp() PHPUnit method', - [ - new CodeSample( - <<<'CODE_SAMPLE' -use PHPUnit\Framework\TestCase; - -final class SomeTest extends TestCase -{ - public function testSomething() - { - $some->call(); - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -use PHPUnit\Framework\TestCase; - -final class SomeTest extends TestCase -{ - private Some $some; - - protected function setUp() - { - $this->some = new Some(); - } - - public function testSomething() - { - $this->some->call(); - } -} -CODE_SAMPLE - ), - ] - ); - } - - /** - * @param Class_ $node - */ - public function refactor(Node $node): ?Node - { - $requiredPropertyNames = []; - - // 1. iterate class methods - foreach ($node->getMethods() as $classMethod) { - if (! $classMethod->isPublic()) { - continue; - } - - $this->traverseNodesWithCallable($classMethod, function (Node $node) use ( - &$requiredPropertyNames - ): ?MethodCall { - if (! $node instanceof MethodCall) { - return null; - } - - $callerType = $this->getType($node->var); - if (! $callerType instanceof ErrorType) { - return null; - } - - $variableName = $this->getName($node->var); - if (! is_string($variableName)) { - return null; - } - - $requiredPropertyNames[] = $variableName; - - // replace with property fetch - $node->var = new PropertyFetch(new Variable('this'), $variableName); - - return $node; - }); - } - - if ($requiredPropertyNames === []) { - return null; - } - - $requiredPropertyNames = array_unique($requiredPropertyNames); - - $newProperties = []; - foreach ($requiredPropertyNames as $requiredPropertyName) { - if ($node->getProperty($requiredPropertyName) instanceof Property) { - // already exists - continue; - } - - $newProperties[] = new Property(Modifiers::PRIVATE, [new PropertyItem($requiredPropertyName)]); - } - - if ($newProperties === []) { - return null; - } - - // 3. add setup method, property and replace variable with a property fetch - - $assignExpressions = []; - foreach ($requiredPropertyNames as $requiredPropertyName) { - $assign = new Assign( - new PropertyFetch(new Variable('this'), $requiredPropertyName), - // @todo resolve object type from FQN - new New_(new FullyQualified($requiredPropertyName)) - ); - - $assignExpressions[] = new Expression($assign); - } - - $setUpClassMethod = $node->getMethod('setUp'); - if ($setUpClassMethod instanceof ClassMethod) { - $setUpClassMethod->stmts = array_merge((array) $setUpClassMethod->stmts, $assignExpressions); - } else { - $setUpClassMethod = new Node\Stmt\ClassMethod('setUp'); - $setUpClassMethod->flags = Modifiers::PROTECTED; - $setUpClassMethod->returnType = new Identifier('void'); - $setUpClassMethod->stmts = $assignExpressions; - - $node->stmts = array_merge([$setUpClassMethod], $node->stmts); - } - - $node->stmts = array_merge($newProperties, $node->stmts); - - return $node; - } -} diff --git a/rules/Rector/Class_/SplitSetUpMockRector.php b/rules/Rector/Class_/SplitSetUpMockRector.php deleted file mode 100644 index 7d224a3a..00000000 --- a/rules/Rector/Class_/SplitSetUpMockRector.php +++ /dev/null @@ -1,269 +0,0 @@ -> - */ - public function getNodeTypes(): array - { - return [Class_::class]; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition( - 'Move mock expectations from setUp() to particular methods where not overriden', - [ - new CodeSample( - <<<'CODE_SAMPLE' -use PHPUnit\Framework\TestCase; - -final class SomeTest extends TestCase -{ - private $someClassMock; - - protected function setUp() - { - $this->someClassMock = $this->createMock(SomeClass::class); - $this->someClassMock->expect($this->once())->method('someMethod')->willReturn('someValue'); - } - - public function testSome() - { - } - - public function testAnother() - { - $this->someClassMock->expect($this->once())->method('someMethod')->willReturn('differentValue'); - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -use PHPUnit\Framework\TestCase; - -final class SomeTest extends TestCase -{ - private $someClassMock; - - protected function setUp() - { - $this->someClassMock = $this->createMock(SomeClass::class); - } - - public function testSome() - { - $this->someClassMock->expect($this->once())->method('someMethod')->willReturn('someValue'); - } - - public function testAnother() - { - $this->someClassMock->expect($this->once())->method('someMethod')->willReturn('differentValue'); - } -} -CODE_SAMPLE - ), - ] - ); - } - - /** - * @param Class_ $node - */ - public function refactor(Node $node): ?Node - { - $setUpClassMethod = $node->getMethod('setUp'); - if (! $setUpClassMethod instanceof ClassMethod) { - return null; - } - - $setupMethodCallsStmtKeys = []; - - foreach ((array) $setUpClassMethod->stmts as $key => $classMethodStmt) { - if (! $classMethodStmt instanceof Expression) { - continue; - } - - if (! $classMethodStmt->expr instanceof MethodCall) { - continue; - } - - $methodCall = $classMethodStmt->expr; - - // must be global property fetch, later overridden - $topMostMethodCall = $methodCall; - while ($topMostMethodCall->var instanceof MethodCall) { - $topMostMethodCall = $topMostMethodCall->var; - } - - if (! $topMostMethodCall->var instanceof PropertyFetch) { - continue; - } - - // must be expect() mock method call - if (! MethodCallFinder::hasByName($methodCall, 'expect')) { - continue; - } - - $setupMethodCallsStmtKeys[$key] = $methodCall; - } - - if ($setupMethodCallsStmtKeys === []) { - return null; - } - - $hasChanged = false; - $setupMethodCallsStmtKeysToAddToTestMethods = []; - - foreach ($setupMethodCallsStmtKeys as $key => $mockMethodCall) { - $mockedMethodName = $this->resolveMockedMethodName($mockMethodCall); - if (! is_string($mockedMethodName)) { - continue; - } - - foreach ($node->getMethods() as $classMethod) { - if (! $classMethod->isPublic()) { - continue; - } - - $classMethodName = $this->getName($classMethod); - - // we look only for test methods - if (! str_starts_with($classMethodName, 'test')) { - continue; - } - - foreach ((array) $classMethod->stmts as $stmt) { - if (! $stmt instanceof Expression) { - continue; - } - - if (! $stmt->expr instanceof MethodCall) { - continue; - } - - $methodCall = $stmt->expr; - - // must be global property fetch, later overridden - $topMostMethodCall = $methodCall; - while ($topMostMethodCall->var instanceof MethodCall) { - $topMostMethodCall = $topMostMethodCall->var; - } - - if (! $topMostMethodCall->var instanceof PropertyFetch) { - continue; - } - - // must be expect() mock method call - if (! MethodCallFinder::hasByName($methodCall, 'expect')) { - continue; - } - - $methodMethodCall = MethodCallFinder::findByName($methodCall, 'method'); - if (! $methodMethodCall instanceof MethodCall) { - continue; - } - - $currenMockedMethodName = $this->valueResolver->getValue($methodMethodCall->getArgs()[0]->value); - if (! is_string($currenMockedMethodName)) { - continue; - } - - if ($mockedMethodName === $currenMockedMethodName) { - // we have a match - // we need to move the method call from setUp() to the test method - unset($setUpClassMethod->stmts[$key]); - - $setupMethodCallsStmtKeysToAddToTestMethods[] = $key; - - $hasChanged = true; - } - } - } - } - - foreach ($setupMethodCallsStmtKeysToAddToTestMethods as $setupMethodCallStmtKeyToAddToTestMethod) { - $setUpMockMethodCall = $setupMethodCallsStmtKeys[$setupMethodCallStmtKeyToAddToTestMethod]; - $setUpMockMethodName = $this->resolveMockedMethodName($setUpMockMethodCall); - if (! is_string($setUpMockMethodName)) { - continue; - } - - foreach ($node->getMethods() as $classMethod) { - if (! $classMethod->isPublic()) { - continue; - } - - $classMethodName = $this->getName($classMethod); - - // we look only for test methods - if (! str_starts_with($classMethodName, 'test')) { - continue; - } - - // already set, do not add - if ($this->isMockingMethodName($classMethod, $setUpMockMethodName)) { - continue; - } - - $classMethod->stmts = array_merge([new Expression($setUpMockMethodCall)], (array) $classMethod->stmts); - $hasChanged = true; - } - } - - if ($hasChanged) { - return $node; - } - - return null; - } - - private function resolveMockedMethodName(MethodCall $mockMethodCall): ?string - { - $methodMethodCall = MethodCallFinder::findByName($mockMethodCall, 'method'); - if (! $methodMethodCall instanceof MethodCall) { - return null; - } - - $firstArg = $methodMethodCall->getArgs()[0]; - - return $this->valueResolver->getValue($firstArg->value); - } - - private function isMockingMethodName(ClassMethod $classMethod, string $mockedMethodName): bool - { - $methodMethodCall = MethodCallFinder::findByName($classMethod, 'method'); - if (! $methodMethodCall instanceof MethodCall) { - return false; - } - - $firstArg = $methodMethodCall->getArgs()[0]; - return $this->valueResolver->isValue($firstArg->value, $mockedMethodName); - } -} diff --git a/src/NodeFactory/ExpectExceptionMethodCallFactory.php b/src/NodeFactory/ExpectExceptionMethodCallFactory.php index 6ca0d85a..e5f6da45 100644 --- a/src/NodeFactory/ExpectExceptionMethodCallFactory.php +++ b/src/NodeFactory/ExpectExceptionMethodCallFactory.php @@ -4,7 +4,6 @@ namespace Rector\PhpSpecToPHPUnit\NodeFactory; -use Rector\PhpSpecToPHPUnit\Exception\ShouldNotHappenException; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ClassConstFetch; @@ -16,6 +15,7 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Expression; use Rector\PhpSpecToPHPUnit\Enum\PHPUnitMethodName; +use Rector\PhpSpecToPHPUnit\Exception\ShouldNotHappenException; use Rector\PhpSpecToPHPUnit\ValueObject\DuringAndRelatedMethodCall; use Rector\PhpSpecToPHPUnit\ValueObject\TestedObject; use Rector\ValueObject\MethodName; diff --git a/src/NodeFactory/LetMockNodeFactory.php b/src/NodeFactory/LetMockNodeFactory.php index 1d27f080..9694b463 100644 --- a/src/NodeFactory/LetMockNodeFactory.php +++ b/src/NodeFactory/LetMockNodeFactory.php @@ -4,7 +4,6 @@ namespace Rector\PhpSpecToPHPUnit\NodeFactory; -use Rector\PhpSpecToPHPUnit\Exception\ShouldNotHappenException; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\PropertyFetch; @@ -19,6 +18,7 @@ use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpParser\Node\NodeFactory; use Rector\PhpSpecToPHPUnit\DocFactory; +use Rector\PhpSpecToPHPUnit\Exception\ShouldNotHappenException; use Rector\PhpSpecToPHPUnit\ValueObject\ServiceMock; final class LetMockNodeFactory diff --git a/src/ServiceMockResolver.php b/src/ServiceMockResolver.php index a41dafec..566257d0 100644 --- a/src/ServiceMockResolver.php +++ b/src/ServiceMockResolver.php @@ -4,11 +4,11 @@ namespace Rector\PhpSpecToPHPUnit; -use Rector\PhpSpecToPHPUnit\Exception\ShouldNotHappenException; use PhpParser\Node\Name; use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; use Rector\NodeNameResolver\NodeNameResolver; +use Rector\PhpSpecToPHPUnit\Exception\ShouldNotHappenException; use Rector\PhpSpecToPHPUnit\ValueObject\ServiceMock; final class ServiceMockResolver @@ -46,10 +46,7 @@ private function createServiceMock(Param $param): ServiceMock // this should be always typed if (! $param->type instanceof Name) { - throw new ShouldNotHappenException(sprintf( - 'Param "%s" must be typed', - $variableName - )); + throw new ShouldNotHappenException(sprintf('Param "%s" must be typed', $variableName)); } $mockClassName = $param->type->toString(); diff --git a/src/Set/MigrationSetList.php b/src/Set/MigrationSetList.php index 4177cc85..f66afd5b 100644 --- a/src/Set/MigrationSetList.php +++ b/src/Set/MigrationSetList.php @@ -13,9 +13,4 @@ final class MigrationSetList * @var string */ public const PHPSPEC_TO_PHPUNIT = __DIR__ . '/../../config/sets/phpspec-to-phpunit.php'; - - /** - * @var string - */ - public const POST_RUN = __DIR__ . '/../../config/sets/post-run-set.php'; }