当我想在handle()方法中模拟某些类时,如何测试Job类?

时间:2018-08-12 22:11:19

标签: php laravel

我有以下任务计划:

$schedule->job(new ProcessTumblrVideos)->hourly();

在ProcessTumblrVideos类中:

class ProcessTumblrVideos implements ShouldQueue
{
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $sites = TumblrSite::all();

        foreach ($sites as $site) {
            $scrapper = new TumblrScrapper($site);
            $posts = $scrapper->scrapVideoPosts();
            $filter = new TumblrFilter($site->last_scrapped_at, $posts);
            $collection = $filter->getFilteredVideos($filter->filterOldPosts($posts));
            if (sizeof($collection) <= 0) {
                continue;
            }
            foreach ($collection as $video) {
                $site->videos()->create([
                    'video_url' => $video->video_url,
                    'thumbnail_url' => $video->thumbnail_url,
                    'thumbnail_width' => $video->thumbnail_width,
                    'thumbnail_height' => $video->thumbnail_height,
                    'duration' => $video->duration,
                ]);
            }

            $site->update([
                'last_scrapped_at' => now()
            ]);
        }
    }
}

TumblrScrapper类将运行第三方API调用,我想模拟该类。由于TumblrScrapper需要构造函数参数($ site),因此我不能在handle()中使用依赖项注入,那么如何模拟此类?

0 个答案:

没有答案