-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite test subject calls found within loops.
This is achieved by delegating node manipulation to a Visitor which can traverse the AST within the body of a test method.
- Loading branch information
Showing
6 changed files
with
104 additions
and
93 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,42 @@ | ||
<?php | ||
|
||
namespace Transunit\Pass; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeFinder; | ||
use PhpParser\NodeTraverser; | ||
use Transunit\Pass; | ||
use Transunit\Visitor; | ||
|
||
/** | ||
* ``` | ||
* function it_contracts_out_agents(AgentRepository $agentRepository, EventDispatcher $eventDispatcher, Agent $agent47, ContractEvent $event) | ||
* { | ||
* $this->agentRepository->find(47)->willReturn($agent47); | ||
* $this->eventDispatcher->dispatch($event)->shouldBeCalled(); | ||
* | ||
* - $this->contractOut(47)->shouldReturn($agent47); | ||
* + $this->_testSubject->contractOut(47)->shouldReturn($agent47); | ||
* } | ||
* ``` | ||
*/ | ||
class TestSubjectCallPass implements Pass | ||
{ | ||
public function find(NodeFinder $nodeFinder, $ast): array | ||
{ | ||
return $nodeFinder->findInstanceOf($ast, Node\Stmt\ClassMethod::class); | ||
} | ||
|
||
public function rewrite(Node $node): void | ||
{ | ||
if (!$node instanceof Node\Stmt\ClassMethod) { | ||
return; | ||
} | ||
|
||
$subNodeTraverser = new NodeTraverser( | ||
new Visitor\TestSubjectCallVisitor() | ||
); | ||
|
||
$node->stmts = $subNodeTraverser->traverse($node->stmts); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
<?php | ||
|
||
namespace Transunit\Visitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
class TestSubjectCallVisitor extends NodeVisitorAbstract | ||
{ | ||
public function leaveNode(Node $node) | ||
{ | ||
if (! $node instanceof Node\Expr\MethodCall) { | ||
return $node; | ||
} | ||
|
||
if (!$node->var instanceof Node\Expr\Variable) { | ||
return $node; | ||
} | ||
|
||
if ('this' !== $node->var->name) { | ||
return $node; | ||
} | ||
|
||
if ('prophesize' === $node->name->toString()) { | ||
return $node; | ||
} | ||
|
||
if ('beConstructedWith' === $node->name->toString()) { | ||
return $node; | ||
} | ||
|
||
if ('beConstructedThrough' === $node->name->toString()) { | ||
return $node; | ||
} | ||
|
||
$node->var = new Node\Expr\PropertyFetch( | ||
new Node\Expr\Variable('this'), | ||
'_testSubject' | ||
); | ||
|
||
return $node; | ||
} | ||
} |