anonymous//tests/AuthenticatedTestCase.php$0   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 10
wmc 3
1
<?php
2
3
namespace Tests;
4
5
use JWTAuth;
6
use App\Models\Employee;
7
use App\Exceptions\Handler;
8
use Illuminate\Contracts\Debug\ExceptionHandler;
9
use Illuminate\Foundation\Testing\RefreshDatabase;
10
11
abstract class AuthenticatedTestCase extends TestCase
12
{
13
    use RefreshDatabase;
0 ignored issues
show
introduced by
The trait Illuminate\Foundation\Testing\RefreshDatabase requires some properties which are not provided by Tests\AuthenticatedTestCase: $connectionsToTransact, $dropViews
Loading history...
14
    use CreatesApplication;
15
16
    protected $loggedUser;
17
    protected $oldExceptionHandler;
18
19
    public function setup()
20
    {
21
        parent::setup();
22
        $this->disableExceptionHandling();
23
        $this->loggedUser = factory(Employee::class)->create();
24
    }
25
26
    private function headers()
27
    {
28
        $token = JWTAuth::fromUser($this->loggedUser);
29
        JWTAuth::setToken($token);
30
31
        return [
32
            'Accept' => 'application/json',
33
            'Authorization' => sprintf('Bearer %s', $token),
34
        ];
35
    }
36
37
    public function json($method, $uri, array $data = [ ], array $headers = [ ])
38
    {
39
        $headers = array_merge($headers, $this->headers());
40
41
        return parent::json($method, $uri, $data, $headers);
42
    }
43
44
    protected function disableExceptionHandling()
45
    {
46
        $this->oldExceptionHandler = $this->app->make(ExceptionHandler::class);
47
        $this->app->instance(ExceptionHandler::class, new class extends Handler {
48
            public function __construct() {}
49
            public function report(\Exception $e) {}
50
            public function render($request, \Exception $e) {
51
                throw $e;
52
            }
53
        });
54
    }
55
    protected function withExceptionHandling()
56
    {
57
        $this->app->instance(ExceptionHandler::class, $this->oldExceptionHandler);
58
59
        return $this;
60
    }
61
}
62