NVM I found the solution.
I needed to create an alias mock to call a static method (http://docs.mockery.io/en/latest/reference/public_static_properties.html)
i just changed the one line to:
$user = Mockery::mock('alias:App\User');
and it works.
I've been struggling with this for awhile now and hoping someone can enlighten me.
Basically I have a PayloadService that help me manage JWT auth stuff I'm doing. It provides some simple functions that all me to do things like get the userId from the payload or other claims. I also have a toUser function that returns the user based off the ids in the claims... but I can't figure out how to UNIT test it.
Here is the function
/**
* @return mixed
*/
public function toUser()
{
// if it is already loaded.. don't reload.
if ($this->user)
{
return $this->user;
}
$user_class = $this->user_class;
$this->user = $user_class::find($this->getModelUserIdentifier());
return $this->user;
}
My problem is that I don't want it to actually query the database. How can I use mockery to stub this out so I can return a dummy user?
this is how I'm trying to run the test right now.
/** @test */
public function it_gets_the_user_from_the_payload()
{
$user = Mockery::mock('App\User');
$user->shouldReceive('find')
->andReturn($this->getDummyUser());
$user = $this->payload->toUser();
$this->assertInstanceOf('App\User', $user);
}
but it doesn't work. it just performs the query on the database anyway. As you can tell I'm pretty new to testing and can't figure it out! any help would be appreciated.
NVM I found the solution.
I needed to create an alias mock to call a static method (http://docs.mockery.io/en/latest/reference/public_static_properties.html)
i just changed the one line to:
$user = Mockery::mock('alias:App\User');
and it works.
Please or to participate in this conversation.