1 month ago

How to mock model's static function in phpunit?

Posted 1 month ago by budiantoip

I'm using laravel 4.2 and phpunit 5.7.

[email protected] :

public function index()
    $my_profile = View::shared('my_profile');

    $per_page = Input::get('per_page') ?: Config::get('pagination.custom')[0];

    $event_overviews = Event::getEventsAsOwnerAndTeamMemberByUserId($my_profile->userid)->paginate($per_page);

    return View::make('user/event.overview.index', compact('event_overviews'));

and here's my unit test :

public function loginWithFakeUser()
    $user = new User([
        'userid' => 1,
        'email'  => '[email protected]',


    return $user;

public function testEventIndex()


    $event = $this->getMockBuilder(Event::class)

    $this->app->instance('Event', $event);

    $this->call('GET', action('User\[email protected]'));


running the unit test returns this error message :

1) EventTest::testEventIndex
Trying to configure method "getEventsAsOwnerAndTeamMemberByUserId" which cannot be configured because it does not exist, has not been specified, is final, or is static

it must be because the function is static, but changing the way we call model's function will require lots of efforts, as lots of places are calling the model's function this way.

I wish there's a way, any clue on how to proceed will be much appreciated.

Please sign in or create an account to participate in this conversation.