I want to test my API controller that using some guzzle requests from another services. I have one request for making a download link.
-
this is my API route
Route::group(['prefix' => '/v1'], function () { Route::get('/exampledl', 'DownloadController@downloadChecker'); });
-
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; } } }
-
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?
from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2B97GcG
via IFTTT
Aucun commentaire:
Enregistrer un commentaire