Hi, I'm attempting to write a test and mock the YouTube service. Full disclosure: new to the mockery library.
I'm using the Laravel YouTube library.
I'm attempting to mock the Youtube::searchAdvanced call, not actually to call the Youtube API. Some online resources say to add a 'Youtube' class in the job's constructor. Is that the only way? I find it strange to edit my job's code just for the ability to mock and add a test but perhaps I'm wrong and it's the standard. Any help or tips would be much appreciated!
/**
* Execute the job.
*/
public function handle(): void
{
// use Alaouy\Youtube\Facades\Youtube;
$upcoming = collect(Youtube::searchAdvanced([
'type' => 'video',
'eventType' => 'upcoming',
'part' => 'id, snippet',
'channelId' => config('youtube.channel_id'),
'maxResults' => 50,
]));
if ($upcoming->count() === 0) {
tap($this->notifyOnJobFailure(), fn () => throw new \Exception('No upcoming livestreams found.'));
}
$sunday = $this->firstLivestreamByDate($this->upcomingSunday(), $upcoming);
$wednesday = $this->firstLivestreamByDate($this->upcomingWednesday(), $upcoming);
if ($sunday->count() > 0) {
cache()->put('livestream.sunday', $sunday->first()?->id?->videoId, now()->addWeek());
}
if ($wednesday->count() > 0) {
cache()->put('livestream.wednesday', $wednesday->first()?->id?->videoId, now()->addWeek());
}
if ($sunday->count() === 0 || $wednesday->count() === 0) {
tap($this->notifyOnJobFailure($upcoming), fn () => throw new \Exception('Could not find upcoming livestreams for Sunday and/or Wednesday.'));
}
}
it('updates livestream links from YouTube', function () {
// use Alaouy\Youtube\Facades\Youtube;
Youtube::shouldReceive('searchAdvanced')->andReturn($this->fakeResponse());
$job = new \App\Jobs\Livestream\UpdateUpcomingLivestreamJob();
$job->handle();
// Perform assertions...
});