-
-
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.
- Loading branch information
1 parent
2d862a7
commit dbd1baf
Showing
6 changed files
with
149 additions
and
49 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
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\NodeManipulator; | ||
|
||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Class_; | ||
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 testSingleMethod(): void | ||
{ | ||
$someClass = new Class_(new Name('SingleMethodClass')); | ||
$someClass->stmts[] = new ClassMethod('firstMethod'); | ||
|
||
$this->addSingleDependency($someClass); | ||
|
||
$printedClass = $this->printerStandard->prettyPrintFile([$someClass]) . PHP_EOL; | ||
|
||
$this->assertStringEqualsFile(__DIR__ . '/Fixture/expected_single_method.php.inc', $printedClass); | ||
} | ||
|
||
public function testWithProperty(): void | ||
{ | ||
$someClass = new Class_(new Name('ClassWithSingleProperty')); | ||
$someClass->stmts[] = new Property(Class_::MODIFIER_PRIVATE, [new PropertyProperty('someProperty')]); | ||
|
||
$this->addSingleDependency($someClass); | ||
|
||
$printedClass = $this->printerStandard->prettyPrintFile([$someClass]) . PHP_EOL; | ||
|
||
$this->assertStringEqualsFile(__DIR__ . '/Fixture/expected_single_property.php.inc', $printedClass); | ||
} | ||
|
||
public function testWithMethodAndProperty(): void | ||
{ | ||
$someClass = new Class_(new Name('ClassWithMethodAndProperty')); | ||
$someClass->stmts[] = new Property(Class_::MODIFIER_PRIVATE, [new PropertyProperty('someProperty')]); | ||
$someClass->stmts[] = new ClassMethod(new Identifier('someMethod')); | ||
|
||
$this->addSingleDependency($someClass); | ||
|
||
$printedClass = $this->printerStandard->prettyPrintFile([$someClass]) . PHP_EOL; | ||
$this->assertStringEqualsFile(__DIR__ . '/Fixture/expected_method_and_property.php.inc', $printedClass); | ||
} | ||
|
||
private function addSingleDependency(Class_ $someClass): void | ||
{ | ||
$this->classDependencyManipulator->addConstructorDependency($someClass, new PropertyMetadata( | ||
'eventDispatcher', | ||
new ObjectType('EventDispatcherInterface') | ||
)); | ||
} | ||
} |
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) | ||
{ | ||
} | ||
} |