-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ClassDependencyManipulator to add dependency on right position (#…
- Loading branch information
1 parent
1b1807b
commit 2c67908
Showing
8 changed files
with
196 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
tests/NodeManipulator/ClassDependencyManipulatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\NodeManipulator; | ||
|
||
use PhpParser\Node\Const_; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Property; | ||
use PhpParser\Node\Stmt\PropertyProperty; | ||
use PhpParser\PrettyPrinter\Standard; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\NodeManipulator\ClassDependencyManipulator; | ||
use Rector\PostRector\ValueObject\PropertyMetadata; | ||
use Rector\Testing\PHPUnit\AbstractLazyTestCase; | ||
|
||
final class ClassDependencyManipulatorTest extends AbstractLazyTestCase | ||
{ | ||
private ClassDependencyManipulator $classDependencyManipulator; | ||
|
||
private Standard $printerStandard; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->classDependencyManipulator = $this->make(ClassDependencyManipulator::class); | ||
$this->printerStandard = new Standard(); | ||
} | ||
|
||
public function testEmptyClass(): void | ||
{ | ||
$someClass = new Class_(new Identifier('EmptyClass')); | ||
|
||
$this->addSingleDependency($someClass); | ||
$this->asssertClassEqualsFile($someClass, __DIR__ . '/Fixture/expected_empty_class.php.inc'); | ||
} | ||
|
||
public function testSingleMethod(): void | ||
{ | ||
$someClass = new Class_(new Identifier('SingleMethodClass')); | ||
$someClass->stmts[] = new ClassMethod('firstMethod'); | ||
|
||
$this->addSingleDependency($someClass); | ||
|
||
$this->asssertClassEqualsFile($someClass, __DIR__ . '/Fixture/expected_single_method.php.inc'); | ||
} | ||
|
||
public function testWithProperty(): void | ||
{ | ||
$someClass = new Class_(new Identifier('ClassWithSingleProperty')); | ||
$someClass->stmts[] = new Property(Class_::MODIFIER_PRIVATE, [new PropertyProperty('someProperty')]); | ||
|
||
$this->addSingleDependency($someClass); | ||
|
||
$this->asssertClassEqualsFile($someClass, __DIR__ . '/Fixture/expected_single_property.php.inc'); | ||
|
||
} | ||
|
||
public function testWithMethodAndProperty(): void | ||
{ | ||
$someClass = new Class_(new Identifier('ClassWithMethodAndProperty')); | ||
$someClass->stmts[] = new Property(Class_::MODIFIER_PRIVATE, [new PropertyProperty('someProperty')]); | ||
$someClass->stmts[] = new ClassMethod(new Identifier('someMethod')); | ||
|
||
$this->addSingleDependency($someClass); | ||
|
||
$this->asssertClassEqualsFile($someClass, __DIR__ . '/Fixture/expected_method_and_property.php.inc'); | ||
} | ||
|
||
public function testConstantProperties(): void | ||
{ | ||
$someClass = new Class_(new Identifier('ConstantProperties')); | ||
$someClass->stmts[] = new ClassConst([new Const_('SOME_CONST', new String_('value'))]); | ||
$someClass->stmts[] = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('someProperty')]); | ||
$someClass->stmts[] = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('anotherProperty')]); | ||
|
||
$this->addSingleDependency($someClass); | ||
|
||
$this->asssertClassEqualsFile($someClass, __DIR__ . '/Fixture/expected_class_const_property.php.inc'); | ||
} | ||
|
||
private function addSingleDependency(Class_ $class): void | ||
{ | ||
$this->classDependencyManipulator->addConstructorDependency($class, new PropertyMetadata( | ||
'eventDispatcher', | ||
new ObjectType('EventDispatcherInterface') | ||
)); | ||
} | ||
|
||
private function asssertClassEqualsFile(Class_ $class, string $expectedFilePath): void | ||
{ | ||
$printedClass = $this->printerStandard->prettyPrintFile([$class]); | ||
|
||
// normalize newline in Windows | ||
$printedClass = str_replace("\n", PHP_EOL, $printedClass . "\n"); | ||
|
||
$this->assertStringEqualsFile($expectedFilePath, $printedClass); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/NodeManipulator/Fixture/expected_class_const_property.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
class ConstantProperties | ||
{ | ||
const SOME_CONST = 'value'; | ||
public $someProperty; | ||
public $anotherProperty; | ||
public function __construct(private \EventDispatcherInterface $eventDispatcher) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
class EmptyClass | ||
{ | ||
public function __construct(private \EventDispatcherInterface $eventDispatcher) | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/NodeManipulator/Fixture/expected_method_and_property.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
class ClassWithMethodAndProperty | ||
{ | ||
private $someProperty; | ||
public function __construct(private \EventDispatcherInterface $eventDispatcher) | ||
{ | ||
} | ||
function someMethod() | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/NodeManipulator/Fixture/expected_single_method.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
class SingleMethodClass | ||
{ | ||
public function __construct(private \EventDispatcherInterface $eventDispatcher) | ||
{ | ||
} | ||
function firstMethod() | ||
{ | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/NodeManipulator/Fixture/expected_single_property.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
class ClassWithSingleProperty | ||
{ | ||
private $someProperty; | ||
public function __construct(private \EventDispatcherInterface $eventDispatcher) | ||
{ | ||
} | ||
} |