2017-06-09 02:28:26 +08:00
|
|
|
<?php
|
|
|
|
|
2017-06-20 00:30:39 +08:00
|
|
|
use Carbon\Carbon;
|
2017-07-05 02:57:08 +08:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
2017-07-14 04:43:56 +08:00
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
|
|
|
2017-06-20 00:30:39 +08:00
|
|
|
|
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
|
|
|
|
*/
|
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
|
|
|
|
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
|
|
|
];
|
|
|
|
|
2017-12-13 10:18:02 +08:00
|
|
|
public function apiHeaders()
|
|
|
|
{
|
|
|
|
return self::$auth_headers;
|
|
|
|
}
|
|
|
|
|
2018-01-05 11:05:26 +08:00
|
|
|
public function headers($user)
|
2018-01-03 04:31:00 +08:00
|
|
|
{
|
|
|
|
return [
|
2018-01-05 11:05:26 +08:00
|
|
|
#'accept' => 'application/json',
|
|
|
|
#'content-type' => 'application/json',
|
|
|
|
'x-api-key' => $user->api_key,
|
2018-01-03 04:31:00 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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() {
|
2017-07-14 04:43:56 +08:00
|
|
|
Artisan::call('database:create', ['--reset' => true]);
|
|
|
|
Artisan::call('migrate:refresh', ['--env' => 'unittest']);
|
2017-06-10 11:19:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2017-06-10 11:19:17 +08:00
|
|
|
//$app['config']->set('database.default','testing');
|
2017-06-09 02:28:26 +08:00
|
|
|
return $app;
|
|
|
|
}
|
2017-06-10 03:47:02 +08:00
|
|
|
|
|
|
|
public function createRepository($repo_name) {
|
|
|
|
$app = $this->createApplication();
|
2017-06-10 11:19:17 +08:00
|
|
|
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)
|
|
|
|
{
|
2017-07-05 02:57:08 +08:00
|
|
|
$svc = app('\App\Services\DatabaseService');
|
|
|
|
$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)
|
|
|
|
{
|
|
|
|
//$klass = new $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
|
|
|
}
|
2018-01-06 00:23:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for a get call with a user
|
|
|
|
* @param \App\Models\User $user
|
|
|
|
* @param string $uri
|
|
|
|
* @return \Illuminate\Foundation\Testing\TestResponse
|
|
|
|
*/
|
|
|
|
public function user_get($user, $uri)
|
|
|
|
{
|
|
|
|
return $this->withHeaders($this->headers($user))->get($uri);
|
|
|
|
}
|
2017-06-09 02:28:26 +08:00
|
|
|
}
|