我为我的Laravel 5.1 API构建了一个搜索YouTube的服务.我正在尝试为它编写测试,但我很难弄清楚如何模拟功能.以下是该服务.
class Youtube { /** * Youtube API Key * * @var string */ protected $apiKey; /** * Youtube constructor. * * @param $apiKey */ public function __construct($apiKey) { $this->apiKey = $apiKey; } /** * Perform YouTube video search. * * @param $channel * @param $query * @return mixed */ public function searchYoutube($channel, $query) { $url = 'https://www.googleapis.com/youtube/v3/search?order=date' . '&part=snippet' . '&channelId=' . urlencode($channel) . '&type=video' . '&maxResults=25' . '&key=' . urlencode($this->apiKey) . '&q=' . urlencode($query); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); $result = json_decode($result, true); if ( is_array($result) && count($result) ) { return $this->extractVideo($result); } return $result; } /** * Extract the information we want from the YouTube search resutls. * @param $params * @return array */ protected function extractVideo($params) { /* // If successful, YouTube search returns a response body with the following structure: // //{ // "kind": "youtube#searchListResponse", // "etag": etag, // "nextPageToken": string, // "prevPageToken": string, // "pageInfo": { // "totalResults": integer, // "resultsPerPage": integer // }, // "items": [ // { // "kind": "youtube#searchResult", // "etag": etag, // "id": { // "kind": string, // "videoId": string, // "channelId": string, // "playlistId": string // }, // "snippet": { // "publishedAt": datetime, // "channelId": string, // "title": string, // "description": string, // "thumbnails": { // (key): { // "url": string, // "width": unsigned integer, // "height": unsigned integer // } // }, // "channelTitle": string, // "liveBroadcastContent": string // } // ] //} */ $results = []; $items = $params['items']; foreach ($items as $item) { $videoId = $items['id']['videoId']; $title = $items['snippet']['title']; $description = $items['snippet']['description']; $thumbnail = $items['snippet']['thumbnails']['default']['url']; $results[] = [ 'videoId' => $videoId, 'title' => $title, 'description' => $description, 'thumbnail' => $thumbnail ]; } // Return result from YouTube API return ['items' => $results]; } }
我创建了这个服务来从控制器中抽象出功能.然后我用Mockery来测试控制器.现在我需要弄清楚如何测试上面的服务.任何帮助表示赞赏.