Skip to content

Commit

Permalink
Merge pull request #1236 from rectorphp/php-spec-create
Browse files Browse the repository at this point in the history
phpspec - turn new of mocks to mocks
  • Loading branch information
TomasVotruba committed Mar 19, 2019
2 parents d28edea + fc19e1e commit de5adc2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Rector\PhpSpecToPHPUnit\Rector\Class_;

use PhpParser\Builder\Method;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Clone_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
Expand Down Expand Up @@ -195,8 +196,23 @@ private function processBeConstructed(ClassMethod $classMethod): void
throw new ShouldNotHappenException();
}

$assign = new Assign($param->var, new New_($param->type));
$assigns[] = new Expression($assign);
$methodCall = new MethodCall(new Variable('this'), 'createMock');
$methodCall->args[] = new Arg(new ClassConstFetch(new FullyQualified($param->type), 'class'));

$varDoc = sprintf(
'/** @var \%s|\%s $%s */',
$this->getName($param->type),
'PHPUnit\Framework\MockObject\MockObject',
$this->getName($param->var)
);

$assign = new Assign($param->var, $methodCall);

// add @var doc comment
$assignExpression = new Expression($assign);
$assignExpression->setDocComment(new Doc($varDoc));

$assigns[] = $assignExpression;
}

$classMethod->params = [];
Expand Down Expand Up @@ -318,6 +334,11 @@ private function processTestMethod(ClassMethod $classMethod): void
}

if ($node->var instanceof Variable && $this->isName($node->var, 'this')) {
// skip "createMock" method
if ($this->isName($node, 'createMock')) {
return $node;
}

// $this->clone() ↓
// clone $this->testedObject
if ($this->isName($node, 'clone')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ class CurrencyTest extends \PHPUnit\Framework\TestCase
private $currency;
protected function setUp()
{
$data = new CurrencyData();
/** @var \spec\Rector\PhpSpecToPHPUnit\Tests\Rector\Class_\PhpSpecClassToPHPUnitClassRector\Fixture\CurrencyData|\PHPUnit\Framework\MockObject\MockObject $data */
$data = $this->createMock(\spec\Rector\PhpSpecToPHPUnit\Tests\Rector\Class_\PhpSpecClassToPHPUnitClassRector\Fixture\CurrencyData::class);
$data->code = 'CZK';
$this->currency = \Rector\PhpSpecToPHPUnit\Tests\Rector\Class_\PhpSpecClassToPHPUnitClassRector\Fixture\Currency::create([$data]);
}

public function testNotBeConstructedWithoutCode()
{
$data = new CurrencyData();
/** @var \spec\Rector\PhpSpecToPHPUnit\Tests\Rector\Class_\PhpSpecClassToPHPUnitClassRector\Fixture\CurrencyData|\PHPUnit\Framework\MockObject\MockObject $data */
$data = $this->createMock(\spec\Rector\PhpSpecToPHPUnit\Tests\Rector\Class_\PhpSpecClassToPHPUnitClassRector\Fixture\CurrencyData::class);
$data->code = '';
$this->expectException(ValidationException::class);
$this->currency = \Rector\PhpSpecToPHPUnit\Tests\Rector\Class_\PhpSpecClassToPHPUnitClassRector\Fixture\Currency::create([$data]);
Expand Down

0 comments on commit de5adc2

Please sign in to comment.