-
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.
Call reveal() on local prophesized collaborators.
- Loading branch information
Showing
8 changed files
with
156 additions
and
46 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
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,57 @@ | ||
<?php | ||
|
||
namespace Transunit\Pass; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeFinder; | ||
use PhpParser\NodeTraverser; | ||
use Transunit\Pass; | ||
use Transunit\Visitor\ParentConnectingVisitor; | ||
use Transunit\Visitor\RevealCollaboratorVisitor; | ||
|
||
class LocalRevealPass implements Pass | ||
{ | ||
public function find(NodeFinder $nodeFinder, $ast): array | ||
{ | ||
return $nodeFinder->find($ast, function (Node $node) { | ||
if ($node instanceof Node\Stmt\ClassMethod && !in_array($node->name->toString(), ['setUp', 'let'],true)) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
}); | ||
} | ||
|
||
public function rewrite(Node $node): void | ||
{ | ||
if (!$node instanceof Node\Stmt\ClassMethod) { | ||
return; | ||
} | ||
|
||
$this->reveal($node); | ||
} | ||
|
||
private function reveal(Node\Stmt\ClassMethod $node): void | ||
{ | ||
if (in_array($node->name->toString(), ['let', 'setUp'], true)) { | ||
return; | ||
} | ||
|
||
$collabs = []; | ||
foreach ($node->params as $param) { | ||
$variableName = $param->var->name; | ||
$collabs[] = $variableName; | ||
} | ||
|
||
if (empty($collabs)) { | ||
return; | ||
} | ||
|
||
$subNodeTraverser = new NodeTraverser( | ||
new ParentConnectingVisitor(), | ||
new RevealCollaboratorVisitor($collabs) | ||
); | ||
|
||
$node->stmts = $subNodeTraverser->traverse($node->stmts); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Transunit\Visitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
class ParentConnectingVisitor extends NodeVisitorAbstract | ||
{ | ||
public function enterNode(Node $node): void | ||
{ | ||
foreach ($node->getSubNodeNames() as $name) { | ||
$subNode = $node->{$name}; | ||
|
||
if (is_array($subNode)) { | ||
foreach ($subNode as $childNode) { | ||
if ($childNode instanceof Node) { | ||
$childNode->setAttribute('parent', $node); | ||
} | ||
} | ||
} elseif ($subNode instanceof Node) { | ||
$subNode->setAttribute('parent', $node); | ||
} | ||
} | ||
} | ||
} |
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,62 @@ | ||
<?php | ||
|
||
namespace Transunit\Visitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
/** | ||
* Finds local collaborators within a test method / spec example that should have their | ||
* prophecy revealed. | ||
* | ||
* For example, in PhpSpec you may have the following: | ||
* | ||
* function it_listens_to_request_events(Event $event) | ||
* { | ||
* $this->onKernelRequest($event); | ||
* } | ||
* | ||
* when translated to PHPUnit by the ProphesizeLocalCollaboratorsPass would be: | ||
* | ||
* $event = $this->prophesize(Event::class); | ||
* $this->_testSubject->onKernelRequest($event); | ||
* | ||
* The visitor will modify this statement to call reveal() on the 'event' collaborator: | ||
* | ||
* $this->_testSubject->onKernelRequest($event->reveal()); | ||
* | ||
* @see ProphesizeLocalCollaboratorsPass | ||
*/ | ||
class RevealCollaboratorVisitor extends NodeVisitorAbstract | ||
{ | ||
/** | ||
* @var string[] This is the variable name of the local collaborator that should be revealed. | ||
*/ | ||
private $collaborators; | ||
|
||
public function __construct(array $collaborators) | ||
{ | ||
$this->collaborators = $collaborators; | ||
} | ||
|
||
public function leaveNode(Node $node) | ||
{ | ||
if (! $node instanceof Node\Expr\Variable) { | ||
return $node; | ||
} | ||
|
||
if (! in_array($node->name, $this->collaborators, true)) { | ||
return $node; | ||
} | ||
|
||
/** @see ParentConnectingVisitor which sets this attribute prior to this visitor being invoked by the traverser. */ | ||
if ($node->getAttribute('parent') instanceof Node\Expr\MethodCall) { | ||
return $node; | ||
} | ||
|
||
return new Node\Expr\MethodCall( | ||
$node, | ||
'reveal' | ||
); | ||
} | ||
} |