PHP code example of davesweb / mockery-helper

1. Go to this page and download the library: Download davesweb/mockery-helper 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/ */

    

davesweb / mockery-helper example snippets




namespace My\Tests;

use Davesweb\MockeryHelper\UsesMockery;
use PHPUnit\Framework\TestCase;

class UsesMockeryTest extends TestCase
{
    use UsesMockery;
}

public function method(callable $callable): string



namespace My\Tests;

use Davesweb\MockeryHelper\UsesMockery;
use PHPUnit\Framework\TestCase;
use Some\Package\MyDependency;

class UsesMockeryTest extends TestCase
{
    use UsesMockery;
    
    public function setUp()
    {
        $mockedDependency = \Mockery::mock(MyDependency::class);
        
        $mockedDependency
            ->shouldReceive($this->method([MyDependency::class, 'methodItShouldReceive']))
            ->with('param 1', 'param 2')
            ->once()
            ->andReturn('My return value');
        
        //...
    }
}

public function mock(...$args): MockInterface

public function spy(...$args): MockInterface

public function namedMock(...$args): MockInterface

class UsesMockeryTest extends TestCase
{
    use UsesMockery;
    
    /**
     * {@inheritdoc}
     */
    protected $allowNonExistingMethods = true;
    
    public function test_something()
    {
        // This entire test case won't check if methods exist.
    }
}

class UsesMockeryTest extends TestCase
{
    use UsesMockery;
    
    public function test_something()
    {
        $this->allowNonExistingMethods = true;
        
        // Only this test won't check if methods exist.
    }
}

class UsesMockeryTest extends TestCase
{
    use UsesMockery;
    
    public function test_something()
    {        
        $method = $this->method([MyDependency::class, 'someNonExistingMethod'], false);
        
        // Only the above call skips the check if the method exists
    }
}