PHP code example of gyro / phake-attributes

1. Go to this page and download the library: Download gyro/phake-attributes 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/ */

    

gyro / phake-attributes example snippets


use Gyro\PhakeAttributes\Mock;
use Gyro\PhakeAttributes\PhakeAttributes;
use PHPUnit\Framework\TestCase;

class MyServiceTestCase extends TestCase
{
    use PhakeAttributes;

    #[Mock] // during setup phase in PHPUnit will call Phake::mock(MyDependencyService::class)
    private MyDependencyService $myDependency;

    public function testMyServiceMethod() : void
    {
        \Phake::when($this->myDependency)->foo()->thenReturn('bar');

        // this uses named arguments and array expansion to pass ctor arguments
        // reflection is used to find the types needed for each argument and the
        // properties and mocks in current test are searched for matches based
        //  on variable name or type.
        $service = new MyService(...$this->mockArgumentsFor(Myservice::class));

        // if you dont want to type that much:
        $service = $this->newInstanceWithMockedArgumentsFor(MyService::class);
    }
}