Reputation: 190
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&dl_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?
Upvotes: 1
Views: 2596
Reputation: 630
Another way to mock functions one by one:
$mock = Mockery::mock(DownloadController::class)->makePartial();
$mock->shouldReceive('et_download_links_from_download_server')->andReturn('123465');
$this->app->instance(DownloadController::class,$mock);
Upvotes: 1
Reputation: 190
I got the answer.
for mocking a function in different situations it just needs to use $mock = \Mockery::mock
and makePartial();
like this and it let us to make every return we want without execute the function:
public function test_user_notExist_admin()
{
$mock = \Mockery::mock(DownloadController::class, [
'get_download_links_from_download_server' => $this->links,
'post_details_to_log_server' => [200, "new"],
'connect' => [
"Roles" => [
"authenticated",
"subscriber"
]
, "days" => "38"
]
]
)->makePartial();
$this->get('/api/v1/exampledl?uid=1&n_id=400&u_ip=104.58.1.45&dl_id=a81498a9')
->assertStatus(200)
->assertSee('links');
$this->assertDatabaseHas('users', [
'uid' => (int)request('uid'),
'subscription.role' => 'administrator',
]);
}
I've created for each API call a method then I mocked them with Mockery in an Array.
Upvotes: 2