2017-06-09 02:28:26 +08:00
|
|
|
<?php
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
|
|
|
use App\Contracts\Unit;
|
|
|
|
use App\Exceptions\Handler;
|
2019-08-07 05:48:00 +08:00
|
|
|
use App\Repositories\SettingRepository;
|
2018-01-29 01:12:13 +08:00
|
|
|
use App\Services\DatabaseService;
|
2020-05-16 06:23:16 +08:00
|
|
|
use Carbon\Carbon;
|
2020-05-23 23:43:29 +08:00
|
|
|
use DateTimeImmutable;
|
|
|
|
use Exception;
|
2019-08-07 05:48:00 +08:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Handler\MockHandler;
|
|
|
|
use GuzzleHttp\HandlerStack;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2020-05-23 23:43:29 +08:00
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler;
|
2019-08-21 20:17:44 +08:00
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
2019-09-13 20:05:02 +08:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2020-05-16 06:23:16 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-11-20 23:16:01 +08:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
2020-05-16 06:23:16 +08:00
|
|
|
use Illuminate\Testing\TestResponse;
|
2020-05-23 23:43:29 +08:00
|
|
|
use ReflectionClass;
|
2017-06-20 00:30:39 +08:00
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
/**
|
|
|
|
* Test cases should extend this class
|
|
|
|
*/
|
|
|
|
abstract class TestCase extends \Illuminate\Foundation\Testing\TestCase
|
2017-06-09 02:28:26 +08:00
|
|
|
{
|
2018-02-21 02:59:49 +08:00
|
|
|
use TestData;
|
2019-09-13 20:05:02 +08:00
|
|
|
use CreatesApplication;
|
2018-02-21 02:59:49 +08:00
|
|
|
|
2017-06-09 02:28:26 +08:00
|
|
|
/**
|
|
|
|
* The base URL to use while testing the application.
|
|
|
|
*/
|
2018-01-07 05:13:33 +08:00
|
|
|
public static $prefix = '/api';
|
|
|
|
|
2017-06-10 03:47:02 +08:00
|
|
|
protected $app;
|
2017-06-09 02:28:26 +08:00
|
|
|
protected $baseUrl = 'http://localhost';
|
2019-05-12 23:50:38 +08:00
|
|
|
protected $connectionsToTransact = ['test'];
|
2017-06-09 02:28:26 +08:00
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
/** @var \App\Models\User */
|
2018-01-07 02:07:22 +08:00
|
|
|
protected $user;
|
|
|
|
|
2017-12-12 20:48:04 +08:00
|
|
|
protected static $auth_headers = [
|
2018-08-27 00:40:04 +08:00
|
|
|
'x-api-key' => 'testadminapikey',
|
2017-12-12 20:48:04 +08:00
|
|
|
];
|
|
|
|
|
2018-02-09 01:07:21 +08:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2020-01-07 02:44:43 +08:00
|
|
|
public function setUp(): void
|
2018-02-09 01:07:21 +08:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-08-21 20:17:44 +08:00
|
|
|
|
|
|
|
// Don't throttle requests when running the tests
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
|
|
|
|
2019-11-20 23:16:01 +08:00
|
|
|
Notification::fake();
|
2019-09-13 20:05:02 +08:00
|
|
|
|
2018-02-09 01:07:21 +08:00
|
|
|
Artisan::call('database:create', ['--reset' => true]);
|
2019-09-13 20:05:02 +08:00
|
|
|
Artisan::call('migrate:refresh', ['--env' => 'testing']);
|
2020-05-23 23:43:29 +08:00
|
|
|
|
|
|
|
// $this->disableExceptionHandling();
|
2018-02-09 01:07:21 +08:00
|
|
|
}
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
/**
|
|
|
|
* https://stackoverflow.com/a/41945739
|
|
|
|
* https://gist.github.com/adamwathan/c9752f61102dc056d157
|
|
|
|
*/
|
|
|
|
protected function disableExceptionHandling()
|
2019-05-12 23:50:38 +08:00
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->app->instance(ExceptionHandler::class, new class() extends Handler {
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function report(\Throwable $e)
|
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render($request, \Throwable $e)
|
|
|
|
{
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
});
|
2019-05-12 23:50:38 +08:00
|
|
|
}
|
|
|
|
|
2021-03-30 03:49:34 +08:00
|
|
|
/**
|
|
|
|
* @param $mocks
|
|
|
|
*/
|
|
|
|
protected function addMocks($mocks)
|
|
|
|
{
|
|
|
|
$handler = HandlerStack::create($mocks);
|
|
|
|
$client = new Client(['handler' => $handler]);
|
|
|
|
$this->client->httpClient = $client;
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
/**
|
|
|
|
* @param $user
|
|
|
|
* @param array $headers
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-29 01:12:13 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-08-27 00:40:04 +08:00
|
|
|
public function headers($user = null, array $headers = []): array
|
2018-01-03 04:31:00 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($user !== null) {
|
2018-01-29 01:12:13 +08:00
|
|
|
$headers['x-api-key'] = $user->api_key;
|
2018-08-27 02:50:08 +08:00
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->user !== null) {
|
|
|
|
$headers['x-api-key'] = $this->user->api_key;
|
2018-01-29 01:12:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $headers;
|
2018-01-03 04:31:00 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 05:13:33 +08:00
|
|
|
/**
|
2018-02-09 01:07:21 +08:00
|
|
|
* Import data from a YML file
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-09 01:07:21 +08:00
|
|
|
* @param $file
|
2017-06-09 02:28:26 +08:00
|
|
|
*/
|
2017-06-20 00:30:39 +08:00
|
|
|
public function addData($file)
|
|
|
|
{
|
2018-01-29 01:12:13 +08:00
|
|
|
$svc = app(DatabaseService::class);
|
2018-08-27 00:40:04 +08:00
|
|
|
$file_path = base_path('tests/data/'.$file.'.yml');
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
try {
|
|
|
|
$svc->seed_from_yaml_file($file_path);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure an object has the list of keys
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-05 09:33:23 +08:00
|
|
|
* @param $obj
|
|
|
|
* @param array $keys
|
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
public function assertHasKeys($obj, array $keys = []): void
|
2018-01-05 09:33:23 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($keys as $key) {
|
2018-01-05 09:33:23 +08:00
|
|
|
$this->assertArrayHasKey($key, $obj);
|
|
|
|
}
|
2017-06-20 00:30:39 +08:00
|
|
|
}
|
2018-01-06 00:23:26 +08:00
|
|
|
|
2019-08-07 05:48:00 +08:00
|
|
|
/**
|
|
|
|
* Read a file from the data directory
|
|
|
|
*
|
|
|
|
* @param $filename
|
|
|
|
*
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
public function readDataFile($filename)
|
|
|
|
{
|
|
|
|
$paths = [
|
|
|
|
'data/'.$filename,
|
|
|
|
'tests/data/'.$filename,
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($paths as $p) {
|
|
|
|
if (file_exists($p)) {
|
|
|
|
return file_get_contents($p);
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 21:31:35 +08:00
|
|
|
|
|
|
|
return false;
|
2019-08-07 05:48:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a mock Guzzle Client with a response loaded from $mockFile
|
|
|
|
*
|
2020-03-27 23:49:19 +08:00
|
|
|
* @param array|string $files
|
2019-08-07 05:48:00 +08:00
|
|
|
*/
|
2020-03-27 23:49:19 +08:00
|
|
|
public function mockGuzzleClient($files): void
|
2019-08-07 05:48:00 +08:00
|
|
|
{
|
2020-03-27 23:49:19 +08:00
|
|
|
if (!is_array($files)) {
|
|
|
|
$files = [$files];
|
|
|
|
}
|
|
|
|
|
|
|
|
$responses = [];
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$responses[] = new Response(200, [
|
|
|
|
'Content-Type' => 'application/json; charset=utf-8',
|
|
|
|
], $this->readDataFile($file));
|
|
|
|
}
|
|
|
|
|
|
|
|
$mock = new MockHandler($responses);
|
2019-08-07 05:48:00 +08:00
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
$handler = HandlerStack::create($mock);
|
|
|
|
$guzzleClient = new Client(['handler' => $handler]);
|
|
|
|
app()->instance(Client::class, $guzzleClient);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-27 23:49:19 +08:00
|
|
|
* @param array|string $files The filename or files to respond with
|
2020-03-23 21:31:35 +08:00
|
|
|
*/
|
2020-03-27 23:49:19 +08:00
|
|
|
public function mockXmlResponse($files)
|
2020-03-23 21:31:35 +08:00
|
|
|
{
|
2020-03-27 23:49:19 +08:00
|
|
|
if (!is_array($files)) {
|
|
|
|
$files = [$files];
|
|
|
|
}
|
|
|
|
|
|
|
|
$responses = [];
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$responses[] = new Response(200, [
|
|
|
|
'Content-Type' => 'text/xml',
|
|
|
|
], $this->readDataFile($file));
|
|
|
|
}
|
|
|
|
|
|
|
|
$mock = new MockHandler($responses);
|
2020-03-23 21:31:35 +08:00
|
|
|
|
2019-08-07 05:48:00 +08:00
|
|
|
$handler = HandlerStack::create($mock);
|
|
|
|
$guzzleClient = new Client(['handler' => $handler]);
|
|
|
|
app()->instance(Client::class, $guzzleClient);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a setting
|
|
|
|
*
|
|
|
|
* @param $key
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function updateSetting($key, $value)
|
|
|
|
{
|
|
|
|
$settingsRepo = app(SettingRepository::class);
|
|
|
|
$settingsRepo->store($key, $value);
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
* So we can test private/protected methods
|
|
|
|
* http://bit.ly/1mr5hMq
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param $object
|
|
|
|
* @param $methodName
|
|
|
|
* @param array $parameters
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-05-23 23:43:29 +08:00
|
|
|
* @throws \ReflectionException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return mixed
|
2018-03-21 08:17:11 +08:00
|
|
|
*/
|
|
|
|
public function invokeMethod(&$object, $methodName, array $parameters = [])
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$reflection = new ReflectionClass(get_class($object));
|
2018-03-21 08:17:11 +08:00
|
|
|
$method = $reflection->getMethod($methodName);
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
return $method->invokeArgs($object, $parameters);
|
|
|
|
}
|
|
|
|
|
2018-04-08 09:52:12 +08:00
|
|
|
/**
|
|
|
|
* Transform any data that's passed in. E.g, make sure that any mutator
|
|
|
|
* classes (e.g, units) are not passed in as the mutator class
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-08 09:52:12 +08:00
|
|
|
* @param array $data
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-08 09:52:12 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-05-04 05:13:25 +08:00
|
|
|
protected function transformData(&$data)
|
2018-04-08 09:52:12 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($data as $key => &$value) {
|
|
|
|
if (is_array($value)) {
|
2018-05-04 05:13:25 +08:00
|
|
|
$this->transformData($value);
|
2018-05-04 04:36:26 +08:00
|
|
|
}
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
if (is_subclass_of($value, Unit::class)) {
|
2018-04-08 09:52:12 +08:00
|
|
|
$data[$key] = $value->__toString();
|
|
|
|
}
|
2018-05-09 22:15:07 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($value instanceof DateTimeImmutable) {
|
2018-05-09 22:15:07 +08:00
|
|
|
$data[$key] = $value->format(DATE_ATOM);
|
|
|
|
} elseif ($value instanceof Carbon) {
|
|
|
|
$data[$key] = $value->toIso8601ZuluString();
|
|
|
|
}
|
2018-04-08 09:52:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2018-01-06 00:23:26 +08:00
|
|
|
/**
|
2018-01-07 02:07:22 +08:00
|
|
|
* Override the GET call to inject the user API key
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-06 00:23:26 +08:00
|
|
|
* @param string $uri
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param array $headers
|
|
|
|
* @param null $user
|
|
|
|
*
|
2020-05-23 23:43:29 +08:00
|
|
|
* @return \Illuminate\Testing\TestResponse
|
2018-01-06 00:23:26 +08:00
|
|
|
*/
|
2020-05-23 23:43:29 +08:00
|
|
|
public function get($uri, array $headers = [], $user = null): TestResponse
|
2018-01-06 00:23:26 +08:00
|
|
|
{
|
2018-01-29 01:12:13 +08:00
|
|
|
$req = parent::get($uri, $this->headers($user, $headers));
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($req->isClientError() || $req->isServerError()) {
|
|
|
|
Log::error('GET Error: '.$uri, $req->json());
|
2018-01-11 08:49:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $req;
|
2018-01-07 02:07:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the POST calls to inject the user API key
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-07 02:07:22 +08:00
|
|
|
* @param string $uri
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param array $data
|
|
|
|
* @param array $headers
|
|
|
|
* @param null $user
|
|
|
|
*
|
2020-05-16 06:23:16 +08:00
|
|
|
* @return TestResponse
|
2018-01-07 02:07:22 +08:00
|
|
|
*/
|
2020-05-16 06:23:16 +08:00
|
|
|
public function post($uri, array $data = [], array $headers = [], $user = null): TestResponse
|
2018-01-07 02:07:22 +08:00
|
|
|
{
|
2018-04-08 09:52:12 +08:00
|
|
|
$data = $this->transformData($data);
|
2018-01-29 01:12:13 +08:00
|
|
|
$req = parent::post($uri, $data, $this->headers($user, $headers));
|
|
|
|
if ($req->isClientError() || $req->isServerError()) {
|
2018-08-27 00:40:04 +08:00
|
|
|
Log::error('POST Error: '.$uri, $req->json());
|
2018-01-07 02:07:22 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
return $req;
|
2018-01-07 02:07:22 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 00:44:15 +08:00
|
|
|
/**
|
|
|
|
* Override the PUT calls to inject the user API key
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-23 00:44:15 +08:00
|
|
|
* @param string $uri
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param array $data
|
|
|
|
* @param array $headers
|
|
|
|
* @param null $user
|
|
|
|
*
|
2020-05-16 06:23:16 +08:00
|
|
|
* @return TestResponse
|
2018-02-23 00:44:15 +08:00
|
|
|
*/
|
2020-05-16 06:23:16 +08:00
|
|
|
public function put($uri, array $data = [], array $headers = [], $user = null): TestResponse
|
2018-02-23 00:44:15 +08:00
|
|
|
{
|
2018-04-08 09:52:12 +08:00
|
|
|
$req = parent::put($uri, $this->transformData($data), $this->headers($user, $headers));
|
2018-02-23 00:44:15 +08:00
|
|
|
if ($req->isClientError() || $req->isServerError()) {
|
2018-08-27 00:40:04 +08:00
|
|
|
Log::error('PUT Error: '.$uri, $req->json());
|
2018-02-23 00:44:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $req;
|
|
|
|
}
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
/**
|
|
|
|
* Override the DELETE calls to inject the user API key
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-07 02:07:22 +08:00
|
|
|
* @param string $uri
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param array $data
|
|
|
|
* @param array $headers
|
|
|
|
* @param null $user
|
|
|
|
*
|
2020-05-16 06:23:16 +08:00
|
|
|
* @return TestResponse
|
2018-01-07 02:07:22 +08:00
|
|
|
*/
|
2020-05-16 06:23:16 +08:00
|
|
|
public function delete($uri, array $data = [], array $headers = [], $user = null): TestResponse
|
2018-01-07 02:07:22 +08:00
|
|
|
{
|
2018-04-08 09:52:12 +08:00
|
|
|
$req = parent::delete($uri, $this->transformData($data), $this->headers($user, $headers));
|
2018-01-29 01:12:13 +08:00
|
|
|
if ($req->isClientError() || $req->isServerError()) {
|
2018-08-27 00:40:04 +08:00
|
|
|
Log::error('DELETE Error: '.$uri, $req->json());
|
2018-01-07 02:07:22 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
return $req;
|
2018-01-06 00:23:26 +08:00
|
|
|
}
|
2017-06-09 02:28:26 +08:00
|
|
|
}
|