109 lines
3.3 KiB
PHP
109 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\CRM\ApiV2;
|
|
|
|
use App\Enums\ApiStatus;
|
|
use App\Lib\Helper;
|
|
use App\Models\Customer;
|
|
use App\Models\CustomerLogin;
|
|
use App\Models\Service;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
|
use Illuminate\Testing\AssertableJsonString;
|
|
use Tests\CreatesApplication;
|
|
|
|
class ApiBase extends BaseTestCase
|
|
{
|
|
use CreatesApplication;
|
|
use DatabaseTransactions;
|
|
|
|
/**
|
|
* Make assertion that json response has status.
|
|
*
|
|
* @param \Illuminate\Testing\AssertableJsonString $json
|
|
* @param \App\Enums\ApiStatus $status
|
|
* @return void
|
|
*/
|
|
protected function assertApiResponseStatusIs(AssertableJsonString $json, ApiStatus $status): void
|
|
{
|
|
$json->assertFragment(['status' => $status->value()]);
|
|
}
|
|
|
|
/**
|
|
* Raw Function to call api.
|
|
*
|
|
* @param string $method
|
|
* @param string $uri
|
|
* @param array $data
|
|
* @param array $headers
|
|
* @return \Illuminate\Testing\TestResponse
|
|
*/
|
|
protected function callApi(
|
|
string $method,
|
|
string $uri,
|
|
array $data = [],
|
|
array $headers = []
|
|
): \Illuminate\Testing\TestResponse {
|
|
if (!isset($data['timestamp'])) {
|
|
$data['timestamp'] = \time();
|
|
}
|
|
return $this->json($method, $uri, $data, $headers);
|
|
}
|
|
|
|
/**
|
|
* Raw Function to call api with Token.
|
|
*
|
|
* @param \App\Models\Service $service
|
|
* @param string $method
|
|
* @param string $uri
|
|
* @param array $data
|
|
* @param array $headers
|
|
* @return \Illuminate\Testing\TestResponse
|
|
*/
|
|
protected function callApiWithToken(
|
|
Service $service,
|
|
string $method,
|
|
string $uri,
|
|
array $data = [],
|
|
array $headers = []
|
|
): \Illuminate\Testing\TestResponse {
|
|
$data['bearer_token'] = $service->bearer_token;
|
|
$data['timestamp'] = \gmdate('Y-m-d H:i:s');
|
|
|
|
$data['signature'] = Helper::signRequest(
|
|
$service->bearer_pwd,
|
|
$uri,
|
|
$method,
|
|
$data
|
|
);
|
|
|
|
return $this->callApi($method, $uri, $data, $headers);
|
|
}
|
|
|
|
/**
|
|
* Get Customer models with given status (active or not).
|
|
*
|
|
* @param boolean $status The customer status for a brand.
|
|
* @param boolean $random Randomize the customer to get.
|
|
* @return array Data in array: customer, service, login, brand, info.
|
|
*/
|
|
protected function getCustomer(bool $status = true, bool $random = true): array
|
|
{
|
|
$login = CustomerLogin::query()->where('status', $status);
|
|
if ($random) {
|
|
$login = $login->inRandomOrder();
|
|
}
|
|
/** @var \App\Models\CustomerLogin $login */
|
|
$login = $login->firstOrFail();
|
|
/** @var \App\Models\Brand $brand */
|
|
$brand = $login->brand;
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = $login->customer;
|
|
/** @var \App\Models\Service $service */
|
|
$service = $brand->services()->firstOrFail();
|
|
/** @var \App\Models\CustomerInfo $info */
|
|
$info = $customer->info($brand, Customer::FIRST_OR_FAIL);
|
|
|
|
return compact('customer', 'service', 'login', 'brand', 'info');
|
|
}
|
|
}
|