1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 12/9/15 |
5
|
|
|
* Time: 5:24 PM. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Tests\Laravel5\JsonApi; |
12
|
|
|
|
13
|
|
|
use Illuminate\Events\Dispatcher; |
14
|
|
|
use Illuminate\Filesystem\ClassFinder; |
15
|
|
|
use Illuminate\Filesystem\Filesystem; |
16
|
|
|
use Illuminate\Routing\Router; |
17
|
|
|
use NilPortugues\Laravel5\JsonApi\Laravel5JsonApiServiceProvider; |
18
|
|
|
use NilPortugues\Tests\App\Transformers\EmployeesTransformer; |
19
|
|
|
use NilPortugues\Tests\App\Transformers\OrdersTransformer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class LaravelTestCase. |
23
|
|
|
*/ |
24
|
|
|
class LaravelTestCase extends \Illuminate\Foundation\Testing\TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Setup DB before each test. |
28
|
|
|
*/ |
29
|
|
|
public function setUp() |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
|
33
|
|
|
$this->app['config']->set('database.default', 'sqlite'); |
34
|
|
|
$this->app['config']->set('database.connections.sqlite.database', ':memory:'); |
35
|
|
|
$this->app['config']->set('jsonapi', [EmployeesTransformer::class, OrdersTransformer::class]); |
36
|
|
|
$this->app['config']->set('jsonapi', [EmployeesTransformer::class, OrdersTransformer::class]); |
37
|
|
|
$this->app['config']->set('app.url', 'http://localhost/'); |
38
|
|
|
$this->app['config']->set('app.debug', true); |
39
|
|
|
$this->app['config']->set('app.key', \env('APP_KEY', '1234567890123456')); |
40
|
|
|
$this->app['config']->set('app.cipher', 'AES-128-CBC'); |
41
|
|
|
|
42
|
|
|
$this->app->boot(); |
43
|
|
|
|
44
|
|
|
$this->migrate(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* run package database migrations. |
49
|
|
|
*/ |
50
|
|
|
public function migrate() |
51
|
|
|
{ |
52
|
|
|
$fileSystem = new Filesystem(); |
53
|
|
|
$classFinder = new ClassFinder(); |
54
|
|
|
|
55
|
|
|
foreach ($fileSystem->files(__DIR__.'/../../../../tests/NilPortugues/App/Migrations') as $file) { |
56
|
|
|
$fileSystem->requireOnce($file); |
57
|
|
|
$migrationClass = $classFinder->findClass($file); |
58
|
|
|
(new $migrationClass())->down(); |
59
|
|
|
(new $migrationClass())->up(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Boots the application. |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Foundation\Application |
67
|
|
|
*/ |
68
|
|
|
public function createApplication() |
69
|
|
|
{ |
70
|
|
|
/** @var $app \Illuminate\Foundation\Application */ |
71
|
|
|
$app = require __DIR__.'/../../../../vendor/laravel/laravel/bootstrap/app.php'; |
72
|
|
|
|
73
|
|
|
$this->setUpHttpKernel($app); |
74
|
|
|
|
75
|
|
|
$app->register(\Illuminate\Database\DatabaseServiceProvider::class); |
76
|
|
|
$app->register(\NilPortugues\Tests\App\Providers\RouteServiceProvider::class); |
77
|
|
|
$app->register(Laravel5JsonApiServiceProvider::class); |
78
|
|
|
|
79
|
|
|
return $app; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Router |
84
|
|
|
*/ |
85
|
|
|
protected function getRouter() |
86
|
|
|
{ |
87
|
|
|
$router = new Router(new Dispatcher()); |
88
|
|
|
|
89
|
|
|
$router->group( |
90
|
|
|
['prefix' => 'api/v1/', 'namespace' => 'NilPortugues\Tests\App\Controller'], |
91
|
|
|
function () use ($router) { |
92
|
|
|
$router->post('employees', ['as' => 'employees.post', 'uses' => 'EmployeesController@postAction']); |
93
|
|
|
$router->get('employees', ['as' => 'employees.list', 'uses' => 'EmployeesController@listAction']); |
94
|
|
|
$router->get('employees/{id}', ['as' => 'employees.get', 'uses' => 'EmployeesController@getAction']); |
95
|
|
|
$router->put('employees/{id}', ['as' => 'employees.put', 'uses' => 'EmployeesController@putAction']); |
96
|
|
|
$router->patch( |
97
|
|
|
'employees/{id}', |
98
|
|
|
['as' => 'employees.patch', 'uses' => 'EmployeesController@patchAction'] |
99
|
|
|
); |
100
|
|
|
$router->delete( |
101
|
|
|
'employees/{id}', |
102
|
|
|
['as' => 'employees.delete', 'uses' => 'EmployeesController@deleteAction'] |
103
|
|
|
); |
104
|
|
|
$router->get('orders', ['as' => 'orders.list', 'uses' => 'OrdersController@listAction']); |
105
|
|
|
$router->get('orders/{id}', ['as' => 'orders.get', 'uses' => 'OrdersController@getAction']); |
106
|
|
|
$router->get( |
107
|
|
|
'employees/{employee_id}/orders', |
108
|
|
|
['as' => 'employees.orders', 'uses' => 'OrdersController@getOrdersByEmployee'] |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return $router; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param \Illuminate\Foundation\Application $app |
118
|
|
|
*/ |
119
|
|
|
private function setUpHttpKernel($app) |
120
|
|
|
{ |
121
|
|
|
$app->instance('request', \Illuminate\Http\Request::capture()); |
122
|
|
|
$app->make('Illuminate\Foundation\Http\Kernel', [$app, $this->getRouter()])->bootstrap(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|