phpvms/tests/TestCase.php

171 lines
4.3 KiB
PHP
Raw Normal View History

2017-06-09 02:28:26 +08:00
<?php
use App\Services\DatabaseService;
2017-06-20 00:30:39 +08:00
/**
* Class TestCase
*/
2017-06-09 02:28:26 +08:00
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
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';
protected $connectionsToTransact = ['testing'];
2017-06-09 02:28:26 +08:00
protected $user;
protected static $auth_headers = [
2017-12-31 03:39:56 +08:00
'x-api-key' => 'testadminapikey'
];
2017-12-13 10:18:02 +08:00
public function apiHeaders()
{
return self::$auth_headers;
}
/**
* @param $user
* @param array $headers
* @return array
*/
public function headers($user=null, array $headers = []): array
{
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;
}
/**
* Return the URL with the URI prefix
* @param $uri
* @return string
*/
public function u($uri) {
return self::$prefix . $uri;
}
2017-06-10 03:47:02 +08:00
public function __construct($name = null, array $data = [], $dataName = '') {
parent::__construct($name, $data, $dataName);
}
2017-07-05 02:09:44 +08:00
protected function reset_db() {
Artisan::call('database:create', ['--reset' => true]);
Artisan::call('migrate:refresh', ['--env' => 'unittest']);
}
public function setUp() {
parent::setUp();
$this->reset_db();
}
2017-06-09 02:28:26 +08:00
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
2017-06-10 03:47:02 +08:00
public function createRepository($repo_name) {
$app = $this->createApplication();
return $app->make('App\Repositories\\' . $repo_name);
2017-06-10 03:47:02 +08:00
}
2017-06-20 00:30:39 +08:00
public function addData($file)
{
$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) {
}
}
public function fillableFields(\Illuminate\Database\Eloquent\Model $model)
{
return $model->fillable;
}
/**
* 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
}
/**
* Override the GET call to inject the user API key
* @param string $uri
* @param array $headers
* @param null $user
* @return \Illuminate\Foundation\Testing\TestResponse
*/
public function get($uri, array $headers=[], $user=null): \Illuminate\Foundation\Testing\TestResponse
{
$req = parent::get($uri, $this->headers($user, $headers));
if($req->isClientError() || $req->isServerError()) {
Log::error('GET Error: ' . $uri, $req->json());
}
return $req;
}
/**
* Override the POST calls to inject the user API key
* @param string $uri
* @param array $data
* @param array $headers
* @param null $user
* @return \Illuminate\Foundation\Testing\TestResponse
*/
public function post($uri, array $data = [], array $headers = [], $user=null)
{
$req = parent::post($uri, $data, $this->headers($user, $headers));
if ($req->isClientError() || $req->isServerError()) {
Log::error('POST Error: ' . $uri, $req->json());
}
return $req;
}
/**
* Override the DELETE calls to inject the user API key
* @param string $uri
* @param array $data
* @param array $headers
* @param null $user
* @return \Illuminate\Foundation\Testing\TestResponse
*/
public function delete($uri, array $data = [], array $headers = [], $user=null)
{
$req = parent::delete($uri, $data, $this->headers($user, $headers));
if ($req->isClientError() || $req->isServerError()) {
Log::error('DELETE Error: ' . $uri, $req->json());
}
return $req;
}
2017-06-09 02:28:26 +08:00
}