Skip to content

Commit

Permalink
Rewrite test subject calls found within loops.
Browse files Browse the repository at this point in the history
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
adamelso committed Jun 14, 2024
1 parent 62978e6 commit f94e342
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 93 deletions.
15 changes: 10 additions & 5 deletions lib/Transunit/Pass/LocalRevealPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use Transunit\Pass;
use Transunit\Visitor\ParentConnectingVisitor;
use Transunit\Visitor\RevealCollaboratorVisitor;

use Transunit\Visitor;

/**
* ```
* - $this->_testSubject->onKernelRequest($event);
* + $this->_testSubject->onKernelRequest($event->reveal());
* ```
*/
class LocalRevealPass implements Pass
{
public function find(NodeFinder $nodeFinder, $ast): array
Expand Down Expand Up @@ -48,8 +53,8 @@ private function reveal(Node\Stmt\ClassMethod $node): void
}

$subNodeTraverser = new NodeTraverser(
new ParentConnectingVisitor(),
new RevealCollaboratorVisitor($collabs)
new Visitor\ParentConnectingVisitor(),
new Visitor\RevealCollaboratorVisitor($collabs)
);

$node->stmts = $subNodeTraverser->traverse($node->stmts);
Expand Down
8 changes: 8 additions & 0 deletions lib/Transunit/Pass/ReplaceCallsToGetWrappedObjectPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
use PhpParser\NodeFinder;
use Transunit\Pass;

/**
* ```
* $finder->getIterator()->willReturn(new \ArrayIterator([
* - $splFileInfo->getWrappedObject(),
* + $splFileInfo->reveal(),
* ]));
* ```
*/
class ReplaceCallsToGetWrappedObjectPass implements Pass
{
public function find(NodeFinder $nodeFinder, $ast): array
Expand Down
42 changes: 42 additions & 0 deletions lib/Transunit/Pass/TestSubjectCallPass.php
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);
}
}
87 changes: 0 additions & 87 deletions lib/Transunit/Pass/TestSubjectCallsPass.php

This file was deleted.

2 changes: 1 addition & 1 deletion lib/Transunit/Transunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static function processFile(string $path): string
new Pass\AddTestMethodPrefixPass(),
new Pass\TestSubjectInstantiationPass(),
new Pass\GlobalRevealPass(),
new Pass\TestSubjectCallsPass(),
new Pass\TestSubjectCallPass(),
new Pass\AssertionPass(), // run after CallTestSubjectPass.
new Pass\ExceptionAssertionPass(),
new Pass\DeclareGlobalCollaboratorPass(),
Expand Down
43 changes: 43 additions & 0 deletions lib/Transunit/Visitor/TestSubjectCallVisitor.php
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;
}
}

0 comments on commit f94e342

Please sign in to comment.