Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phpspec - turn new of mocks to mocks #1236

Merged
merged 1 commit into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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