Skip to content

Commit 264efe0

Browse files
committed
add setUp method adding
1 parent c2bc8f9 commit 264efe0

File tree

10 files changed

+130
-40
lines changed

10 files changed

+130
-40
lines changed

rules-tests/Rector/Class_/PromisesToAssertsRector/Fixture/phpspec_promises.php.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ use PhpSpec\ObjectBehavior;
2222

2323
final class PhpSpecPromises extends ObjectBehavior
2424
{
25+
private \Rector\PhpSpecToPHPUnit\Tests\Rector\Class_\PromisesToAssertsRector\Fixture\PhpSpecPromises $phpSpecPromises;
26+
protected function setUp(): void
27+
{
28+
$this->phpSpecPromises = new \Rector\PhpSpecToPHPUnit\Tests\Rector\Class_\PromisesToAssertsRector\Fixture\PhpSpecPromises();
29+
}
2530
public function it_returns_id()
2631
{
2732
$this->assertSame(5, $this->phpSpecPromises->id());

rules-tests/Rector/Class_/PromisesToAssertsRector/Fixture/should_be_instance_of.php.inc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace spec\Rector\PhpSpecToPHPUnit;
44

55
use PhpSpec\ObjectBehavior;
66

7-
class TestClassMethod extends ObjectBehavior
7+
class ShouldBeInstanceofMethod extends ObjectBehavior
88
{
99
public function let_1()
1010
{
@@ -35,26 +35,31 @@ namespace spec\Rector\PhpSpecToPHPUnit;
3535

3636
use PhpSpec\ObjectBehavior;
3737

38-
class TestClassMethod extends ObjectBehavior
38+
class ShouldBeInstanceofMethod extends ObjectBehavior
3939
{
40+
private \Rector\PhpSpecToPHPUnit\ShouldBeInstanceofMethod $shouldBeInstanceofMethod;
41+
protected function setUp(): void
42+
{
43+
$this->shouldBeInstanceofMethod = new \Rector\PhpSpecToPHPUnit\ShouldBeInstanceofMethod();
44+
}
4045
public function let_1()
4146
{
42-
$this->assertInstanceOf(\stdClass::class, $this->testClassMethod);
47+
$this->assertInstanceOf(\stdClass::class, $this->shouldBeInstanceofMethod);
4348
}
4449

4550
public function let_2()
4651
{
47-
$this->assertInstanceOf(\stdClass::class, $this->testClassMethod);
52+
$this->assertInstanceOf(\stdClass::class, $this->shouldBeInstanceofMethod);
4853
}
4954

5055
public function let_3()
5156
{
52-
$this->assertInstanceOf(\stdClass::class, $this->testClassMethod);
57+
$this->assertInstanceOf(\stdClass::class, $this->shouldBeInstanceofMethod);
5358
}
5459

5560
public function let_4()
5661
{
57-
$this->assertInstanceOf(\stdClass::class, $this->testClassMethod);
62+
$this->assertInstanceOf(\stdClass::class, $this->shouldBeInstanceofMethod);
5863
}
5964
}
6065

rules/Rector/Class_/ImplicitLetInitializationRector.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,18 @@
44

55
namespace Rector\PhpSpecToPHPUnit\Rector\Class_;
66

7-
use PhpParser\Modifiers;
87
use PhpParser\Node;
9-
use PhpParser\Node\Expr\Assign;
10-
use PhpParser\Node\Expr\New_;
11-
use PhpParser\Node\Expr\PropertyFetch;
12-
use PhpParser\Node\Expr\Variable;
13-
use PhpParser\Node\Identifier;
14-
use PhpParser\Node\Name\FullyQualified;
158
use PhpParser\Node\Param;
169
use PhpParser\Node\Stmt\Class_;
1710
use PhpParser\Node\Stmt\ClassMethod;
18-
use PhpParser\Node\Stmt\Expression;
1911
use PhpParser\Node\Stmt\Property;
2012
use Rector\NodeTypeResolver\Node\AttributeKey;
2113
use Rector\PhpSpecToPHPUnit\Enum\PhpSpecMethodName;
2214
use Rector\PhpSpecToPHPUnit\Naming\PhpSpecRenaming;
15+
use Rector\PhpSpecToPHPUnit\NodeFactory\SetUpInstanceFactory;
2316
use Rector\PhpSpecToPHPUnit\NodeFinder\MethodCallFinder;
2417
use Rector\PhpSpecToPHPUnit\ValueObject\TestedObject;
2518
use Rector\Rector\AbstractRector;
26-
use Rector\ValueObject\MethodName;
2719
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2820
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2921

@@ -34,6 +26,7 @@ final class ImplicitLetInitializationRector extends AbstractRector
3426
{
3527
public function __construct(
3628
private readonly PhpSpecRenaming $phpSpecRenaming,
29+
private readonly SetUpInstanceFactory $setUpInstanceFactory,
3730
) {
3831
}
3932

@@ -108,26 +101,12 @@ public function refactor(Node $node): ?Node
108101

109102
$testedObjectProperty = $this->createTestedObjectProperty($testedObject);
110103

111-
$setUpClassMethod = $this->createSetUpClassMethod($testedObject);
104+
$setUpClassMethod = $this->setUpInstanceFactory->createSetUpClassMethod($testedObject);
112105
$node->stmts = [$testedObjectProperty, $setUpClassMethod, ...(array) $node->stmts];
113106

114107
return $node;
115108
}
116109

117-
private function createSetUpClassMethod(TestedObject $testedObject): ClassMethod
118-
{
119-
$classMethod = new ClassMethod(MethodName::SET_UP);
120-
$classMethod->returnType = new Identifier('void');
121-
$classMethod->flags |= Modifiers::PROTECTED;
122-
123-
$propertyFetch = new PropertyFetch(new Variable('this'), $testedObject->getPropertyName());
124-
$new = new New_(new FullyQualified($testedObject->getClassName()));
125-
126-
$classMethod->stmts = [new Expression(new Assign($propertyFetch, $new))];
127-
128-
return $classMethod;
129-
}
130-
131110
private function createTestedObjectProperty(TestedObject $testedObject): Property
132111
{
133112
return $this->nodeFactory->createPrivatePropertyFromNameAndType(

rules/Rector/Class_/PromisesToAssertsRector.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Rector\PhpSpecToPHPUnit\Rector\Class_;
66

7+
use PhpParser\Node\Stmt\ClassMethod;
8+
use PhpParser\Node\Identifier;
79
use PhpParser\Node;
810
use PhpParser\Node\Expr;
911
use PhpParser\Node\Expr\Assign;
@@ -12,14 +14,19 @@
1214
use PhpParser\Node\Expr\PropertyFetch;
1315
use PhpParser\Node\Expr\Variable;
1416
use PhpParser\Node\Stmt\Class_;
17+
use PhpParser\Node\Stmt\Property;
18+
use PhpParser\NodeFinder;
1519
use Rector\PhpSpecToPHPUnit\Enum\PhpSpecMethodName;
1620
use Rector\PhpSpecToPHPUnit\Enum\PHPUnitMethodName;
1721
use Rector\PhpSpecToPHPUnit\Enum\ProphecyPromisesToPHPUnitAssertMap;
1822
use Rector\PhpSpecToPHPUnit\Naming\PhpSpecRenaming;
1923
use Rector\PhpSpecToPHPUnit\Naming\SystemMethodDetector;
2024
use Rector\PhpSpecToPHPUnit\NodeFactory\AssertMethodCallFactory;
2125
use Rector\PhpSpecToPHPUnit\NodeFactory\BeConstructedWithAssignFactory;
26+
use Rector\PhpSpecToPHPUnit\NodeFactory\SetUpInstanceFactory;
27+
use Rector\PhpSpecToPHPUnit\ValueObject\TestedObject;
2228
use Rector\Rector\AbstractRector;
29+
use Rector\ValueObject\MethodName;
2330
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2431
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2532

@@ -28,11 +35,15 @@
2835
*/
2936
final class PromisesToAssertsRector extends AbstractRector
3037
{
38+
private NodeFinder $nodeFinder;
39+
3140
public function __construct(
3241
private readonly PhpSpecRenaming $phpSpecRenaming,
3342
private readonly AssertMethodCallFactory $assertMethodCallFactory,
3443
private readonly BeConstructedWithAssignFactory $beConstructedWithAssignFactory,
44+
private readonly SetUpInstanceFactory $setUpInstanceFactory,
3545
) {
46+
$this->nodeFinder = new NodeFinder();
3647
}
3748

3849
/**
@@ -57,6 +68,8 @@ public function refactor(Node $node): Node|null
5768

5869
$localMethodNames = $this->getLocalMethodNames($node);
5970

71+
$needsSetUp = true;
72+
6073
foreach ($node->getMethods() as $classMethod) {
6174
if (! $classMethod->isPublic()) {
6275
continue;
@@ -67,9 +80,15 @@ public function refactor(Node $node): Node|null
6780
$classMethod,
6881
[PhpSpecMethodName::LET, PhpSpecMethodName::LET_GO, PhpSpecMethodName::GET_MATCHERS]
6982
)) {
83+
$needsSetUp = false;
7084
continue;
7185
}
7286

87+
// if all methods have this call, no need to add setUp()
88+
if ($this->hasBeConstructedMethodCall($classMethod)) {
89+
$needsSetUp = false;
90+
}
91+
7392
$this->traverseNodesWithCallable($classMethod, function (Node $node) use (
7493
$class,
7594
$testedObjectPropertyFetch,
@@ -158,7 +177,7 @@ public function refactor(Node $node): Node|null
158177
}
159178

160179
// it's a local method call, skip
161-
if ($class->getMethod($methodName) instanceof Node\Stmt\ClassMethod) {
180+
if ($class->getMethod($methodName) instanceof ClassMethod) {
162181
return null;
163182
}
164183

@@ -178,6 +197,16 @@ public function refactor(Node $node): Node|null
178197
return null;
179198
}
180199

200+
// add setUp() method with the property
201+
if ($needsSetUp && ! $class->getMethod(MethodName::SET_UP)) {
202+
$testedObject = $this->phpSpecRenaming->resolveTestedObject($node);
203+
$setUpClassMethod = $this->setUpInstanceFactory->createSetUpClassMethod($testedObject);
204+
205+
$testedObjectProperty = $this->createTestedObjectProperty($testedObject);
206+
207+
$class->stmts = array_merge([$testedObjectProperty, $setUpClassMethod], $class->stmts);
208+
}
209+
181210
return $node;
182211
}
183212

@@ -247,4 +276,29 @@ private function getLocalMethodNames(Class_ $class): array
247276
/** @var string[] $localMethodNames */
248277
return $localMethodNames;
249278
}
279+
280+
private function createTestedObjectProperty(TestedObject $testedObject): Property
281+
{
282+
return $this->nodeFactory->createPrivatePropertyFromNameAndType(
283+
$testedObject->getPropertyName(),
284+
$testedObject->getTestedObjectType()
285+
);
286+
}
287+
288+
private function hasBeConstructedMethodCall(ClassMethod $classMethod): bool
289+
{
290+
$methodCall = $this->nodeFinder->findFirst((array) $classMethod->stmts, function (Node $node): bool {
291+
if (! $node instanceof MethodCall) {
292+
return false;
293+
}
294+
295+
if (! $node->name instanceof Identifier) {
296+
return false;
297+
}
298+
299+
return $node->name->toString() === 'beConstructedWith';
300+
});
301+
302+
return $methodCall instanceof MethodCall;
303+
}
250304
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PhpSpecToPHPUnit\NodeFactory;
6+
7+
use PhpParser\Modifiers;
8+
use PhpParser\Node\Expr\Assign;
9+
use PhpParser\Node\Expr\New_;
10+
use PhpParser\Node\Expr\PropertyFetch;
11+
use PhpParser\Node\Expr\Variable;
12+
use PhpParser\Node\Identifier;
13+
use PhpParser\Node\Name\FullyQualified;
14+
use PhpParser\Node\Stmt\ClassMethod;
15+
use PhpParser\Node\Stmt\Expression;
16+
use Rector\PhpSpecToPHPUnit\ValueObject\TestedObject;
17+
use Rector\ValueObject\MethodName;
18+
19+
final class SetUpInstanceFactory
20+
{
21+
public function createSetUpClassMethod(TestedObject $testedObject): ClassMethod
22+
{
23+
$classMethod = new ClassMethod(MethodName::SET_UP);
24+
$classMethod->returnType = new Identifier('void');
25+
$classMethod->flags |= Modifiers::PROTECTED;
26+
27+
$propertyFetch = new PropertyFetch(new Variable('this'), $testedObject->getPropertyName());
28+
$new = new New_(new FullyQualified($testedObject->getClassName()));
29+
30+
$classMethod->stmts = [new Expression(new Assign($propertyFetch, $new))];
31+
32+
return $classMethod;
33+
}
34+
}

tests/Sets/Fixture/Sylius/add_setup.php.inc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ use PhpSpec\ObjectBehavior;
2727

2828
final class SomeTestedClassTest extends \PHPUnit\Framework\TestCase
2929
{
30-
private SomeTestedClass $someTestedClass;
31-
30+
private \Set\Sylius\SomeTestedClass $someTestedClass;
3231
protected function setUp(): void
3332
{
34-
$this->someTestedClass = new SomeTestedClass();
33+
$this->someTestedClass = new \Set\Sylius\SomeTestedClass();
3534
}
36-
3735
public function testImplements(): void
3836
{
3937
$this->assertInstanceOf(RandomInterface::class, $this->someTestedClass);

tests/Sets/Fixture/is_array_type.php.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ use Sets\Source\Cart;
2626

2727
final class IsArrayTypeTest extends \PHPUnit\Framework\TestCase
2828
{
29+
private \Rector\PhpSpecToPHPUnit\Tests\Sets\Fixture\IsArrayType $isArrayType;
30+
protected function setUp(): void
31+
{
32+
$this->isArrayType = new \Rector\PhpSpecToPHPUnit\Tests\Sets\Fixture\IsArrayType();
33+
}
2934
public function testArrayType(): void
3035
{
3136
$this->assertIsIterable($this->isArrayType->shippingAddresses());

tests/Sets/Fixture/property_fetch_call.php.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ use Sets\Source\Address;
2424

2525
final class PropertyFetchCallTest extends \PHPUnit\Framework\TestCase
2626
{
27+
private \Rector\PhpSpecToPHPUnit\Tests\Sets\Fixture\PropertyFetchCall $propertyFetchCall;
28+
protected function setUp(): void
29+
{
30+
$this->propertyFetchCall = new \Rector\PhpSpecToPHPUnit\Tests\Sets\Fixture\PropertyFetchCall();
31+
}
2732
public function testThrowsAnExceptionIfTheCardIsNotInTheUsersWallet(): void
2833
{
2934
/** @var \Sets\Source\Address|\PHPUnit\Framework\MockObject\MockObject $addressMock */

tests/Sets/Fixture/should_return.php.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ use Sets\Source\Cart;
2626

2727
final class ShouldReturnTest extends \PHPUnit\Framework\TestCase
2828
{
29+
private \PhpSpecToPHPUnit\Fixture\ShouldReturn $shouldReturn;
30+
protected function setUp(): void
31+
{
32+
$this->shouldReturn = new \PhpSpecToPHPUnit\Fixture\ShouldReturn();
33+
}
2934
public function testReturnsId(): void
3035
{
3136
$this->assertSame(5, $this->shouldReturn->id());

tests/Sets/Fixture/should_return_value.php.inc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Sets\Fixture;
44

55
use PhpSpec\ObjectBehavior;
66

7-
class ShouldReturnSpec extends ObjectBehavior
7+
class ShouldReturnValueSpec extends ObjectBehavior
88
{
99
public function let()
1010
{
@@ -30,17 +30,17 @@ namespace Sets\Fixture;
3030

3131
use PhpSpec\ObjectBehavior;
3232

33-
final class ShouldReturnTest extends \PHPUnit\Framework\TestCase
33+
final class ShouldReturnValueTest extends \PHPUnit\Framework\TestCase
3434
{
35-
private \Sets\Fixture\ShouldReturn $shouldReturn;
35+
private \Sets\Fixture\ShouldReturnValue $shouldReturnValue;
3636
protected function setUp(): void
3737
{
38-
$this->shouldReturn = new \Sets\Fixture\ShouldReturn('some');
38+
$this->shouldReturnValue = new \Sets\Fixture\ShouldReturnValue('some');
3939
}
4040

4141
public function testCalculateZeroPriceWhenCartIsEmpty(): void
4242
{
43-
$price = $this->shouldReturn->price();
43+
$price = $this->shouldReturnValue->price();
4444

4545
$this->assertInstanceOf('someType', $price);
4646
$this->assertSame(0.0, $price->withVat());

0 commit comments

Comments
 (0)