I am using the spatie laravel calendar package to make api calls to Google Calendar I want to write some Feature tests but I want to mock the Event wrapper class so my tests don't actually make the API calls
I used Laravels Facades to
Controller
class CalendarEventController extends Controller
{
public function show($calendarId, $event_id)
{
return response()->json(\Facades\Event::find($event_id, $calendarId));
}
}
the Event:find
method returns itself. Most of the data is in the googleEvent paramter.
Event Class
class Event
{
/** @var \Google_Service_Calendar_Event */
public $googleEvent;
}
Test
public function test_event(){
\Facades\Event::shouldReceive('find')->once()->andReturn(?);
$response = $this->json('GET','/event/1/1');
$response->assertJson([
...
]);
}
What do I want this Mocked Facade to return in order to get back the response in the expected format.
I figured I could return a new instance of Event but the event class has a bunch of dependencies that I would need to mock up
If I return Self it gets back the mock Object but the googleEvent
is null.
Edit
because i never set the google event. So one possible option is
$mock = \Facades\Event::shouldReceive('find')->once()->andReturnSelf()->getMock();
$mock->googleEvent = {Whatever}
but this still leaves the problem where I need to mock 5 classes just to get the result formatted correctly End Edit
What should I do to mock this class so that I can get the data back in the format it would come in if it were a normal request?
The result format looks like this
+googleEvent: Google_Service_Calendar_Event
+id: "XXX"
+kind: "calendar#event"
...
...
+"creator": Google_Service_Calendar_EventCreator
+"organizer": Google_Service_Calendar_EventOrganizer
+"start": Google_Service_Calendar_EventDateTime
+"end": Google_Service_Calendar_EventDateTime
+"reminders": Google_Service_Calendar_EventReminders
In order to mock this would require Mocking the Google_Service_Calendar_Event which requires the other 5 GOOGLE_SERVICE_CALENDAR_* classes and so on.