Merge consecutive mock expectations to single ->willReturnMap()
call
use PhpSpec\ObjectBehavior;
class DuringMethodSpec extends ObjectBehavior
{
public function is_should(MockedType $mockedType)
{
- $mockedType->set('first_key')->shouldReturn(100);
- $mockedType->set('second_key')->shouldReturn(200);
+ $mockedType->expects($this->exactly(2))->method('set')
+ ->willReturnMap([
+ ['first_key', 100],
+ ['second_key', 200],
+ ]);
}
}
Split shouldThrow()
and during()
method to expected exception and method call
use PhpSpec\ObjectBehavior;
class DuringMethodSpec extends ObjectBehavior
{
public function is_should()
{
- $this->shouldThrow(ValidationException::class)->during('someMethod');
+ $this->expectException(ValidationException::class);
+ $this->someMethod();
}
}
From PhpSpec mock expectations to PHPUnit mock expectations
use PhpSpec\ObjectBehavior;
class ResultSpec extends ObjectBehavior
{
public function it_returns()
{
- $this->run()->shouldReturn(1000);
+ $this->expects($this->once())->method('run')->willReturn(1000);
}
}
Add implicit object property to setUp()
PHPUnit method
use PhpSpec\ObjectBehavior;
final class SomeTypeSpec extends ObjectBehavior
{
+ private SomeType $someType;
+
+ protected function setUp(): void
+ {
+ $this->someType = new SomeType();
+ }
+
public function let()
{
- $this->run();
+ $this->someType->run();
}
}
Change letGo()
method to tearDown()
PHPUnit method
use PhpSpec\ObjectBehavior;
final class LetGoLetMethods extends ObjectBehavior
{
- public function letGo()
+ protected function tearDown(): void
{
}
}
Change let()
method to setUp()
PHPUnit method, including property mock initialization
use PhpSpec\ObjectBehavior;
+use PHPUnit\Framework\MockObject\MockObject;
final class SomeTypeSpec extends ObjectBehavior
{
- public function let(SomeDependency $someDependency)
+ private SomeType $someType;
+
+ /**
+ * @var MockObject<SomeDependency>
+ */
+ private MockObject $someDependencyMock;
+
+ protected function setUp(): void
{
- $this->beConstructedWith($someDependency);
+ $this->someDependencyMock = $this->createMock(SomeDependency::class);
+ $this->someType = new SomeType($this->someDependencyMock);
}
}
Move parameter mocks to local mocks
use PhpSpec\ObjectBehavior;
final class AddMockProperty extends ObjectBehavior
{
- public function it_should_handle_stuff(SomeType $someType)
+ public function it_should_handle_stuff()
{
+ $someTypeMock = $this->createMock(SomeType::class);
}
}
Rename spec class name and its parent class to PHPUnit format
-use PhpSpec\ObjectBehavior;
+use PHPUnit\Framework\TestCase;
-class DefaultClassWithSetupProperty extends ObjectBehavior
+class DefaultClassWithSetupPropertyTest extends TestCase
{
}
Convert promises and object construction to new instances
use PhpSpec\ObjectBehavior;
class TestClassMethod extends ObjectBehavior
{
public function it_shoud_do()
{
- $this->beConstructedWith(5);
+ $testClassMethod = new \Rector\PhpSpecToPHPUnit\TestClassMethod(5);
}
}
Remove shouldBeCalled()
as implicit in PHPUnit, also empty willReturn()
as no return is implicit in PHPUnit
use PhpSpec\ObjectBehavior;
class ResultSpec extends ObjectBehavior
{
public function it_is_initializable()
{
- $this->run()->shouldBeCalled();
+ $this->run();
- $this->go()->willReturn();
+ $this->go();
}
}
Remove shouldHaveType()
check as pointless in time of PHP 7.0 types
use PhpSpec\ObjectBehavior;
class RenameMethodTest extends ObjectBehavior
{
- public function is_shoud_have_type()
- {
- $this->shouldHaveType(SomeType::class);
- }
}
Rename spec\ to Tests\ namespace
-namespace spec\SomeNamespace;
+namespace Tests\SomeNamespace;
use PhpSpec\ObjectBehavior;
class SomeTest extends ObjectBehavior
{
}
Rename test method from underscore PhpSpec syntax to test* PHPUnit syntax
use PhpSpec\ObjectBehavior;
class RenameMethodTest extends ObjectBehavior
{
- public function is_shoud_be_valid()
+ public function testShouldBeValid(): void
{
}
}
Change shouldNotBeCalled()
to $this->expects($this->never())
check
use PhpSpec\ObjectBehavior;
class ResultSpec extends ObjectBehavior
{
public function it_is_initializable()
{
- $this->run()->shouldNotBeCalled();
+ $this->expects($this->never())->run();
}
}
Handle shouldNotThrow()
expectations
use PhpSpec\ObjectBehavior;
class ResultSpec extends ObjectBehavior
{
public function it_is_initializable()
{
- $this->shouldNotThrow(Exception::class)->during(
- 'someMethodCall',
- ['someArguments']
- );
+ // should not throw an exception
+ $this->someMethodCall('someArguments');
}
}
Reorder and rename shouldThrow()
method to mark before instantiation
use PhpSpec\ObjectBehavior;
class RenameMethodTest extends ObjectBehavior
{
public function is_should()
{
+ $this->expectException(ValidationException::class);
$this->beConstructedThrough('create', [$data]);
- $this->shouldThrow(ValidationException::class)->duringInstantiation();
}
}
Migrate ->with(Arguments::*()) call to PHPUnit
use PhpSpec\ObjectBehavior;
-use Prophecy\Argument;
class ResultSpec extends ObjectBehavior
{
public function it_is_initializable()
{
- $this->run()->with(Arguments::cetera());
+ $this->run()->with($this->any());
}
}