mardi 29 janvier 2019

How to test and mock guzzle responses in different situations?

I want to test my API controller that using some guzzle requests from another services. I have one request for making a download link.

  1. this is my API route

    Route::group(['prefix' => '/v1'], function () {
    Route::get('/exampledl', 'DownloadController@downloadChecker');
    });
    
    
  2. DownloadChecker controller checks if user is admin or subscriber makes a guzzle request to one of my services on a different domain, if not do another Guzzle request to another service and for each situations responses are different. This is a part of controller checks admin role.

    $client = new Client();
    try {
        $response = $client->request('GET', 'https://www.example.com/api/user?u=' . $request->uid);
        $json = \GuzzleHttp\json_decode($response->getBody()->getContents(), True);
    
        // if user doesn't exist in CM
    
        //this part has been written to avoid repeating code
        if (array_key_exists('user', $json) && $json['user'] == null) {
            abort(403);
        }
        elseif (in_array("administrator", $json['Roles'])) {
            User::create([
                'uid'               => (int)$request->uid,
                'subscription.role' => 'administrator',
            ]);
            $client = new Client();
    $response = $client->request('GET', "https://vod.example2.com/vod/2.0/videos/{$a_id}?secure_ip={$u_ip}", [
        'headers' => [
            'authorization' => '**********'
        ]
    ]);
    $json = \GuzzleHttp\json_decode($response->getBody()->getContents(), TRUE);
    
    if (isset($json['data']['mp4_videos'])) {
        $links = [];
        foreach ($json['data']['mp4_videos'] as $mp_video) {
            if (stripos($mp_video, "h_144") !== false) {
                $links['144p'] = $mp_video;
            }
            elseif (stripos($mp_video, "h_240") !== false) {
                $links['240p'] = $mp_video;
            }
            elseif (stripos($mp_video, "h_360") !== false) {
                $links['360p'] = $mp_video;
            }
            elseif (stripos($mp_video, "h_480") !== false) {
                $links['480p'] = $mp_video;
            }
            elseif (stripos($mp_video, "h_720") !== false) {
                $links['720p'] = $mp_video;
            }
            elseif (stripos($mp_video, "h_1080") !== false) {
                $links['1080p'] = $mp_video;
            }
        }
        }
    
    
  3. one of my tests.

    public function test_user_notExist_admin()
    {
    $client = new Client();
    $response = $client->request('GET', 'https://www.example.com/api/user_days_and_roles?u=' . request()->uid);
    $json = \GuzzleHttp\json_decode($response->getBody()->getContents(), True);
    
    $this->get('/api/v1/exampledl?uid=1&n_id=400&u_ip=104.58.1.45&arvan_id=a81498a9')
        ->assertStatus(200)
        ->assertSee('links');
    
    $this->assertDatabaseHas('users', [
        'uid'               => (int)request('uid'),
        'subscription.role' => 'administrator',
    ]);
    }
    
    

There are some other conditions check and I'm not sure how to mock these different situations.

Should I make unit test for every situations? Or is there any way to make guzzle in test environment return a custom response? Or any other way?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire