PHP code example of rector / mockstan

1. Go to this page and download the library: Download rector/mockstan library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rector / mockstan example snippets


$someMock = $this->createMock(Service::class);
$someMock->expects($this->any())
    ->method('calculate')
    ->willReturn(10);

$someMock = $this->createMock(Service::class);
$someMock->expects($this->once())
    ->method('calculate')
    ->willReturn(10);

$someMock = $this->createMock(Service::class);
$someMock->method('calculate')->willReturn(10);

$someMock = $this->createMock(Service::class);
$someMock->expects($this->once())->method('calculate')->willReturn(10);

$dateTimeMock = $this->createMock(\DateTime::class);

$dateTime = new \DateTime();

$someMock = $this->createMock(Service::class);
$someMock->method('calculate')
    ->with(10)
    ->willReturn(20);

$someMock = $this->createMock(Service::class);
$someMock->expects($this->once())
    ->method('calculate')
    ->with(10)
    ->willReturn(20);

$docMock = $this->createMock(App\Document\User::class);

$user = new App\Document\User();

$someMock = $this->createMock(SomeClass::class);

$someMock->expects($this->once())
    ->method('foo')
    ->willReturnOnConsecutiveCalls(...)
    ->willReturnCallback(...)

$someMock = $this->createMock(SomeClass::class);

$someMock->expects($this->once())
    ->method('foo')
    // handle params in single call
    ->willReturnCallback(...)

$entityMock = $this->createMock(App\Entity\Product::class);

$product = new App\Entity\Product();

$this->service = $this->createMock(Service::class);
$this->service = new Service();

$this->someMock = $this->createMock(AnotherService::class);

$this->realService = new Service();

$someMock = $this->createMock(Service::class);
$someMock->expects($this->atLeast(0))
    ->method('calculate')
    ->willReturn(10);

$someMock = $this->createMock(Service::class);
$someMock->expects($this->atLeast(1))
    ->method('calculate')
    ->willReturn(10);

public function testNothing()
{
    $someMock = $this->createMock(Dependency::class);

    $someMock->expects($this->once())
        ->method('doSomething')
        ->willReturn(true);

    $this->assertSame($someMock->doSomething());
}

public function testSomething()
{
    $someMock = $this->createMock(Dependency::class);
    $realObject = new RealObject($someMock);

    $this->assertTrue($realObject->doSomething());
}