77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Unit;
|
||
|
|
|
||
|
|
use App\Models\Customer;
|
||
|
|
use Illuminate\Foundation\Testing\TestCase;
|
||
|
|
use Tests\CreatesApplication;
|
||
|
|
|
||
|
|
abstract class CrmClientTester extends TestCase
|
||
|
|
{
|
||
|
|
use CreatesApplication;
|
||
|
|
|
||
|
|
/** @var App\Models\Customer */
|
||
|
|
protected static $customer;
|
||
|
|
|
||
|
|
/** @var App\Models\Brand */
|
||
|
|
protected static $brand;
|
||
|
|
|
||
|
|
/** @var App\Models\Service */
|
||
|
|
protected static $service;
|
||
|
|
|
||
|
|
public static function setUpBeforeClass(): void
|
||
|
|
{
|
||
|
|
fwrite(STDERR, "\n \e[0;30;43m WARNING \e[0m This test cannot be done on another envirronement than local or testing as it modifies the first Service credentials\n");
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setUp(): void
|
||
|
|
{
|
||
|
|
parent::setUp();
|
||
|
|
if (!in_array(config('app.env'), ['local', 'testing'])) {
|
||
|
|
throw new \Exception();
|
||
|
|
}
|
||
|
|
if (!self::$customer) {
|
||
|
|
extract(self::getCustomer());
|
||
|
|
self::$customer = $customer;
|
||
|
|
self::$brand = $brand;
|
||
|
|
self::$service = $service;
|
||
|
|
config(['crm.token' => self::$service->bearer_token]);
|
||
|
|
config(['crm.password' => self::$service->bearer_pwd]);
|
||
|
|
config(['crm.endpoint' => app()->make('url')->to('/')]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
protected static function getCustomer()
|
||
|
|
{
|
||
|
|
/** @var \App\Models\Costumer $customer */
|
||
|
|
$customer = Customer::whereHas('logins', function ($q) {
|
||
|
|
$q->where('status', true);
|
||
|
|
})->inRandomOrder()->firstOrFail();
|
||
|
|
/** @var \App\Models\Brand $brand */
|
||
|
|
$brand = $customer->logins()->where('status', true)->firstOrFail()->brand;
|
||
|
|
/** @var \App\Models\Service $service */
|
||
|
|
$service = $brand->services()->firstOrFail();
|
||
|
|
|
||
|
|
// regen bearer token to get the pwd in clear
|
||
|
|
$service->generateBearerToken();
|
||
|
|
$bearer_pwd = $service->bearer_pwd;
|
||
|
|
$service->saveOrFail();
|
||
|
|
$service->bearer_pwd = $bearer_pwd;
|
||
|
|
|
||
|
|
return compact('customer', 'service', 'brand');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Parse routes.json and return routes list
|
||
|
|
*
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
protected function getClientRoutes(): array
|
||
|
|
{
|
||
|
|
return \json_decode(\file_get_contents(__DIR__ . '/../../clients/V2/src/routes.json'), true);
|
||
|
|
}
|
||
|
|
}
|