2017-06-09 02:28:26 +08:00
|
|
|
<?php
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
use App\Services\DatabaseService;
|
2017-07-14 04:43:56 +08:00
|
|
|
|
2017-06-20 00:30:39 +08:00
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
/**
|
|
|
|
* Class TestCase
|
|
|
|
*/
|
2018-02-09 01:07:21 +08:00
|
|
|
class TestCase extends Illuminate\Foundation\Testing\TestCase
|
2017-06-09 02:28:26 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The base URL to use while testing the application.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
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';
|
2017-06-10 11:19:17 +08:00
|
|
|
protected $connectionsToTransact = ['testing'];
|
2017-06-09 02:28:26 +08:00
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
protected $user;
|
|
|
|
|
2017-12-12 20:48:04 +08:00
|
|
|
protected static $auth_headers = [
|
2017-12-31 03:39:56 +08:00
|
|
|
'x-api-key' => 'testadminapikey'
|
2017-12-12 20:48:04 +08:00
|
|
|
];
|
|
|
|
|
2018-02-09 01:07:21 +08:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Artisan::call('database:create', ['--reset' => true]);
|
|
|
|
Artisan::call('migrate:refresh', ['--env' => 'unittest']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the application. Required to be implemented
|
|
|
|
* @return \Illuminate\Foundation\Application
|
|
|
|
*/
|
|
|
|
public function createApplication()
|
2017-12-13 10:18:02 +08:00
|
|
|
{
|
2018-02-09 01:07:21 +08:00
|
|
|
$app = require __DIR__ . '/../bootstrap/app.php';
|
|
|
|
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
|
|
|
|
return $app;
|
2017-12-13 10:18:02 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
/**
|
|
|
|
* @param $user
|
|
|
|
* @param array $headers
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function headers($user=null, array $headers = []): array
|
2018-01-03 04:31:00 +08:00
|
|
|
{
|
2018-01-29 01:12:13 +08:00
|
|
|
if($user !== null) {
|
|
|
|
$headers['x-api-key'] = $user->api_key;
|
|
|
|
} else {
|
|
|
|
if($this->user !== null) {
|
|
|
|
$headers['x-api-key'] = $this->user->api_key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
* @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);
|
2017-07-05 02:57:08 +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
|
|
|
|
* @param $obj
|
|
|
|
* @param array $keys
|
|
|
|
*/
|
|
|
|
public function assertHasKeys($obj, $keys=[])
|
|
|
|
{
|
|
|
|
foreach($keys as $key) {
|
|
|
|
$this->assertArrayHasKey($key, $obj);
|
|
|
|
}
|
2017-06-20 00:30:39 +08:00
|
|
|
}
|
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-01-06 00:23:26 +08:00
|
|
|
* @param string $uri
|
2018-01-07 02:07:22 +08:00
|
|
|
* @param array $headers
|
2018-01-11 02:39:13 +08:00
|
|
|
* @param null $user
|
2018-01-06 00:23:26 +08:00
|
|
|
* @return \Illuminate\Foundation\Testing\TestResponse
|
|
|
|
*/
|
2018-01-11 02:39:13 +08:00
|
|
|
public function get($uri, array $headers=[], $user=null): \Illuminate\Foundation\Testing\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-01-11 08:49:42 +08:00
|
|
|
if($req->isClientError() || $req->isServerError()) {
|
2018-01-29 01:12:13 +08:00
|
|
|
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
|
|
|
|
* @param string $uri
|
|
|
|
* @param array $data
|
|
|
|
* @param array $headers
|
2018-01-29 01:12:13 +08:00
|
|
|
* @param null $user
|
2018-01-07 02:07:22 +08:00
|
|
|
* @return \Illuminate\Foundation\Testing\TestResponse
|
|
|
|
*/
|
2018-01-29 01:12:13 +08:00
|
|
|
public function post($uri, array $data = [], array $headers = [], $user=null)
|
2018-01-07 02:07:22 +08:00
|
|
|
{
|
2018-01-29 01:12:13 +08:00
|
|
|
$req = parent::post($uri, $data, $this->headers($user, $headers));
|
|
|
|
if ($req->isClientError() || $req->isServerError()) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the DELETE calls to inject the user API key
|
|
|
|
* @param string $uri
|
|
|
|
* @param array $data
|
|
|
|
* @param array $headers
|
2018-01-29 01:12:13 +08:00
|
|
|
* @param null $user
|
2018-01-07 02:07:22 +08:00
|
|
|
* @return \Illuminate\Foundation\Testing\TestResponse
|
|
|
|
*/
|
2018-01-29 01:12:13 +08:00
|
|
|
public function delete($uri, array $data = [], array $headers = [], $user=null)
|
2018-01-07 02:07:22 +08:00
|
|
|
{
|
2018-01-29 01:12:13 +08:00
|
|
|
$req = parent::delete($uri, $data, $this->headers($user, $headers));
|
|
|
|
if ($req->isClientError() || $req->isServerError()) {
|
|
|
|
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
|
|
|
}
|