ldc-crm/tests/CRM/ApiV2/ApiBase.php
Alexis Gousseau 303834bbca
Crm V4
- Make all tests pass Client (V4),CRMV2,CRMV4
- Simplify client submodule path
- Move App_url to .env.testing
2023-10-06 16:37:29 +02:00

106 lines
3.2 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 Illuminate\Testing\TestResponse;
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)
{
$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 = []): 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 = []
): 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
*/
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');
}
}