rector/rules/PhpSpecToPHPUnit/Rector/AbstractPhpSpecToPHPUnitRector.php
2021-08-23 00:20:32 +00:00

66 lines
2.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\PhpSpecToPHPUnit\Rector;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://gnugat.github.io/2015/09/23/phpunit-with-phpspec.html
* @changelog http://www.phpspec.net/en/stable/cookbook/construction.html
*/
abstract class AbstractPhpSpecToPHPUnitRector extends \Rector\Core\Rector\AbstractRector
{
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Migrate PhpSpec behavior to PHPUnit test', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
namespace spec\SomeNamespaceForThisTest;
use PhpSpec\ObjectBehavior;
class OrderSpec extends ObjectBehavior
{
public function let(OrderFactory $factory, ShippingMethod $shippingMethod): void
{
$factory->createShippingMethodFor(Argument::any())->shouldBeCalled()->willReturn($shippingMethod);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
namespace spec\SomeNamespaceForThisTest;
class OrderSpec extends ObjectBehavior
{
/**
* @var \SomeNamespaceForThisTest\Order
*/
private $order;
protected function setUp()
{
/** @var OrderFactory|\PHPUnit\Framework\MockObject\MockObject $factory */
$factory = $this->createMock(OrderFactory::class);
/** @var ShippingMethod|\PHPUnit\Framework\MockObject\MockObject $shippingMethod */
$shippingMethod = $this->createMock(ShippingMethod::class);
$factory->expects($this->once())->method('createShippingMethodFor')->willReturn($shippingMethod);
}
}
CODE_SAMPLE
)]);
}
public function isInPhpSpecBehavior(\PhpParser\Node $node) : bool
{
$classLike = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
if (!$classLike instanceof \PhpParser\Node\Stmt\ClassLike) {
return \false;
}
return $this->isObjectType($classLike, new \PHPStan\Type\ObjectType('PhpSpec\\ObjectBehavior'));
}
}