Skip to content

Commit

Permalink
Check if argument to exception assertion is an array first.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamelso committed Jun 27, 2024
1 parent ca11355 commit 859b756
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Transunit
===

Convert PhpSpec tests to PHPUnit using the Prophecy library
Convert PhpSpec tests to PHPUnit using the Prophecy library.

This tool takes a different approach to [Rector's implementation][1].
Whilst Rector will convert to PHPUnit's internal mocking library, this
tool will instead rewrite the test to use PhpSpec's Prophecy library
instead within PHPUnit. This tool also has a more linear pipeline.

Dependencies
---
Expand All @@ -16,3 +21,5 @@ Usage
```bash
php ./transunit.php spec tests/unit
```

[1]: https://github.com/rectorphp/custom-phpspec-to-phpunit/tree/main
18 changes: 10 additions & 8 deletions lib/Transunit/Pass/ExceptionAssertionPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* ```
* public function test_it_throws_an_exception_if_country_name_cannot_be_converted_to_code(): void
* {
* - $this->shouldThrow(\InvalidArgumentException::class)->during('convertToCode', ['Atlantis']);
* + static::expectException(\InvalidArgumentException::class);
* + $this->_testSubject->convertToCode('Atlantis');
* - $this->shouldThrow(\InvalidArgumentException::class)->during('convertToCode', ['Atlantis']);
* + static::expectException(\InvalidArgumentException::class);
* + $this->_testSubject->convertToCode('Atlantis');
* }
* ```
*/
Expand All @@ -21,7 +21,7 @@ class ExceptionAssertionPass 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)) {
if ($node instanceof Node\Stmt\ClassMethod && !in_array($node->name->toString(), ['setUp', 'let'], true)) {
return $node;
}

Expand Down Expand Up @@ -51,7 +51,7 @@ private function expectException(Node\Stmt\ClassMethod $node): void

$expression = $stmt;

if (!$expression->expr instanceof Node\Expr\MethodCall) {
if (! $expression->expr instanceof Node\Expr\MethodCall) {
continue;
}

Expand Down Expand Up @@ -80,9 +80,11 @@ private function expectException(Node\Stmt\ClassMethod $node): void
$subjectMethodName = $expression->expr->args[0]->value->value;
$subjectMethodArgs = [];

/** @var Node\ArrayItem $item */
foreach ($expression->expr->args[1]->value->items as $item) {
$subjectMethodArgs[] = new Node\Arg($item->value);
if ($expression->expr->args[1]->value instanceof Node\Expr\Array_) {
/** @var Node\ArrayItem $item */
foreach ($expression->expr->args[1]->value->items as $item) {
$subjectMethodArgs[] = new Node\Arg($item->value);
}
}

$newStmts[] = new Node\Stmt\Expression(
Expand Down

0 comments on commit 859b756

Please sign in to comment.