-
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.
- Loading branch information
Showing
10 changed files
with
276 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Transunit\Pass; | ||
|
||
use PhpParser\Modifiers; | ||
use PhpParser\Node; | ||
use PhpParser\NodeFinder; | ||
use Transunit\Pass; | ||
|
||
/** | ||
* ``` | ||
* - function it_handles_kernel_request_events() | ||
* + public function test_it_handles_kernel_request_events(): void | ||
* { | ||
* ``` | ||
*/ | ||
class AddTestMethodPrefixPass 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->addPrefix($node); | ||
} | ||
|
||
/** | ||
* public function test_it_should_do_something(): void { ... } | ||
*/ | ||
private function addPrefix(Node\Stmt\ClassMethod $node): void | ||
{ | ||
$methodName = $node->name->toString(); | ||
|
||
if (0 !== strpos($methodName, 'it_')) { | ||
return; | ||
} | ||
|
||
$node->name = new Node\Identifier('test_' . $methodName); | ||
$node->flags = Modifiers::PUBLIC; | ||
$node->returnType = new Node\Name('void'); | ||
} | ||
} |
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
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,86 @@ | ||
<?php | ||
|
||
namespace Transunit\Pass; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeFinder; | ||
use Transunit\Pass; | ||
|
||
/** | ||
* ``` | ||
* - protected function setUp(AgentRepository $agentRepository, EventDispatcher $eventDispatcher): void | ||
* + protected function setUp(): void | ||
* { | ||
* + $this->agentRepository = $this->prophesize(AgentRepository::class); | ||
* + $this->eventDispatcher = $this->prophesize(EventDispatcher::class); | ||
* | ||
* // ... | ||
* } | ||
* ``` | ||
*/ | ||
class ProphesizeGlobalCollaboratorsPass 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->prophesizeGlobalCollaborators($node); | ||
} | ||
|
||
/** | ||
* protected function setUp(): void { | ||
* $this->classCollaborator = $this->prophesize(ClassCollaborator::class); | ||
* } | ||
*/ | ||
private function prophesizeGlobalCollaborators(Node\Stmt\ClassMethod $node): void | ||
{ | ||
if (!in_array($node->name->toString(), ['let', 'setUp'], true)) { | ||
return; | ||
} | ||
|
||
$newStmts = []; | ||
|
||
while (count($node->params) > 0) { | ||
$param = array_pop($node->params); | ||
|
||
$newStmts[] = new Node\Stmt\Expression( | ||
new Node\Expr\Assign( | ||
// assign class property | ||
new Node\Expr\PropertyFetch( | ||
new Node\Expr\Variable('this'), | ||
$param->var->name | ||
), | ||
new Node\Expr\MethodCall( | ||
new Node\Expr\Variable('this'), | ||
'prophesize', | ||
[ | ||
new Node\Arg(new Node\Expr\ClassConstFetch( | ||
new Node\Name($param->type->toString()), | ||
'class' | ||
)), | ||
] | ||
) | ||
) | ||
); | ||
|
||
foreach ($node->stmts as $currentStmt) { | ||
$newStmts[] = $currentStmt; | ||
} | ||
} | ||
|
||
$node->stmts = $newStmts; | ||
} | ||
} |
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
Oops, something went wrong.