These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Foundation\Testing\WithoutMiddleware; |
||
17 | use Illuminate\Routing\Router; |
||
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 | use WithoutMiddleware; |
||
27 | /** |
||
28 | * Setup DB before each test. |
||
29 | */ |
||
30 | public function setUp() |
||
31 | { |
||
32 | parent::setUp(); |
||
33 | |||
34 | $this->app['config']->set('database.default', 'sqlite'); |
||
35 | $this->app['config']->set('database.connections.sqlite.database', ':memory:'); |
||
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 | $app->register(\Illuminate\Database\DatabaseServiceProvider::class); |
||
75 | $app->register(\NilPortugues\Tests\App\Providers\RouteServiceProvider::class); |
||
76 | $app->register(\NilPortugues\Laravel5\JsonApi\Laravel5JsonApiServiceProvider::class); |
||
77 | |||
78 | return $app; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return Router |
||
83 | */ |
||
84 | protected function getRouter() |
||
85 | { |
||
86 | $router = new Router(new Dispatcher()); |
||
87 | |||
88 | return $router; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param \Illuminate\Foundation\Application $app |
||
93 | */ |
||
94 | private function setUpHttpKernel($app) |
||
95 | { |
||
96 | $app->instance('request', \Illuminate\Http\Request::capture()); |
||
97 | $app->make('Illuminate\Foundation\Http\Kernel', [$app, $this->getRouter()])->bootstrap(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * This is required for \Symfony\Bridge\PsrHttpMessage\Factory to work. |
||
102 | * This comes as a trade-off of building the underlying package as framework-agnostic. |
||
103 | * |
||
104 | * @param string $method |
||
105 | * @param string $uri |
||
106 | * @param array $parameters |
||
107 | * @param array $cookies |
||
108 | * @param array $files |
||
109 | * @param array $server |
||
110 | * @param string $content |
||
111 | * |
||
112 | * @return \Illuminate\Http\Response |
||
113 | */ |
||
114 | public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null) |
||
0 ignored issues
–
show
|
|||
115 | { |
||
116 | $_SERVER['REQUEST_METHOD'] = strtoupper($method); |
||
117 | $_SERVER['SERVER_NAME'] = parse_url($uri, PHP_URL_HOST); |
||
118 | $_SERVER['REQUEST_URI'] = $uri; |
||
119 | $_SERVER['CONTENT_TYPE'] = 'application/json'; |
||
120 | |||
121 | return parent::call($method, $uri, $parameters, $cookies, $files, $server, $content); |
||
122 | } |
||
123 | } |
||
124 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: