Skip to content
Open
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
@@ -0,0 +1,55 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;

final class IntersectionTypeTest extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject&\stdClass $someMock;

protected function setUp(): void
{
$this->someMock = $this->createMock(\stdClass::class);
}

public function testThis()
{
$this->assertSame('...', $this->someMock);
}

public function testThat()
{
$this->assertSame('...', $this->someMock);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;

final class IntersectionTypeTest extends TestCase
{
private \PHPUnit\Framework\MockObject\Stub&\stdClass $someMock;

protected function setUp(): void
{
$this->someMock = $this->createStub(\stdClass::class);
}

public function testThis()
{
$this->assertSame('...', $this->someMock);
}

public function testThat()
{
$this->assertSame('...', $this->someMock);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipIntersectionTypePropertyUse extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject&\stdClass $someMock;

protected function setUp(): void
{
$this->someMock = $this->createMock(\stdClass::class);
}

public function testThis()
{
$this->someMock->expects($this->atLeastOnce())->method('something');
$this->assertSame('...', $this->someMock);
}

public function testThat()
{
$this->assertSame('...', $this->someMock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -71,7 +72,7 @@ public function refactor(Node $node): ?Class_
// update property type
$property = $node->getProperty($propertyName);
/** @var Property $property */
$property->type = new FullyQualified(PHPUnitClassName::STUB);
$property->type = $this->updatePropertyType($property->type);
}

if (! $hasChanged) {
Expand Down Expand Up @@ -150,4 +151,22 @@ private function shouldSkipClass(Class_ $class): bool
// the setup class method must be here, so we have a place where the createMock() is used
return ! $setUpClassMethod instanceof ClassMethod;
}

private function updatePropertyType(?Node $type): IntersectionType|FullyQualified
{
if ($type instanceof IntersectionType) {
$newTypes = [];
foreach ($type->types as $innerType) {
if ($innerType instanceof FullyQualified && $innerType->toString() === PHPUnitClassName::MOCK_OBJECT) {
$newTypes[] = new FullyQualified(PHPUnitClassName::STUB);
} else {
$newTypes[] = $innerType;
}
}

return new IntersectionType($newTypes);
}

return new FullyQualified(PHPUnitClassName::STUB);
}
}