I'm still working on it but I'm closer. I'm going to try and figure this out as if I am not trying to get the venue from the route first. This way I can just test that it calls the update method off of the VenuePolicy.
I almost had something working. Gonna settle my kid to bed and I'll take another look :)
No worries. If you find something that solves this issue, reply back.
Ok, so, not sure if this is the best solution, but it got the tests working, you will have to bear with me.
This is my test:
/** @test */
public function testTheAuthorizeFunction() {
$this->signIn(); //user with permissions
$mock = $this->mock(LessonPolicy::class)->makePartial()->shouldReceive('update')->andReturn(true)->getMock();
app()->offsetSet(LessonPolicy::class, $mock);
$requestMock = Mockery::mock(Request::class)
->makePartial()
->shouldReceive('path')
->once()
->andReturn('venue/1');
app()->instance('request', $requestMock->getMock());
$request = request();
$request->setRouteResolver(function () use ($request) {
return (new Route('GET', 'venue/{venue}', []))->bind($request);
});
$storeRequest = new StoreRequest;
$this->assertTrue($storeRequest->authorize());
}
Not 100% on this method, its a copy paste from stack overflow, but it seems to do the trick
My authorize method now looks like:
public function authorize() {
$route = request()->route('venue');
$lesson = Lesson::find($route);
return Auth::user()->can('update', $lesson);
}
I have to change the $this->route to request()->route() to get this working properly. Must be something to do with how Laravel resolves stuff out of the container.
Obviously here I have used the model of lesson, rather than Venue as I would assume you would have to do. Guess you would probably want a find or fail too?
Not saying its the best solution, but hopefully it will get your test to pass and give you some more ideas about how to tackle this :)
Please or to participate in this conversation.