当前位置:  开发笔记 > 编程语言 > 正文

需要测试在Laravel 5.1中使用CURL的服务

如何解决《需要测试在Laravel5.1中使用CURL的服务》经验,为你挑选了0个好方法。

我为我的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来测试控制器.现在我需要弄清楚如何测试上面的服务.任何帮助表示赞赏.

推荐阅读
手机用户2402851155
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有