How mock model method in laravel

  laravel, mockery, php, phpunit

I have model method

public static function getDetails(
        array $fields,
        array $where_condition
    ) {
        return Event::select($fields)
            ->where($where_condition)->first();
    }

I am calling this method like this

$logo = Event::getDetails(
[‘logo’],
[‘id’ => $id] )->logo;

so what I tried to mock method in below way but not working

$mock = $this->mock(Event::class, function (MockInterface $mock) {
    $collection = new IlluminateDatabaseEloquentCollection(
        array(
            new Event( array( "logo" => 'test.jpg' ) ),
        )
    );
    $mock->shouldReceive('getDetails')->once()->with(['logo', [ 'id'=> '123']])->andReturns('test.jpg');
});

It is returning null instead of image. It should not go into getDetails method but it is going there

Can anyone help?

Source: Laravel

Leave a Reply