Completed
Push — master ( 48d32a...4da889 )
by Igor
04:45
created

php$0 ➔ withExceptionHandling()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Tests;
4
5
use JWTAuth;
6
use App\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
18
    public function setup()
19
    {
20
        parent::setup();
21
        $this->disableExceptionHandling();
22
        $this->loggedUser = factory(Employee::class)->create();
23
    }
24
25
    private function headers()
26
    {
27
        $token = JWTAuth::fromUser($this->loggedUser);
28
        JWTAuth::setToken($token);
29
30
        return [
31
            'Accept' => 'application/json',
32
            'Authorization' => sprintf('Bearer %s', $token),
33
        ];
34
    }
35
36
    public function json($method, $uri, array $data = [], array $headers = [])
37
    {
38
        $headers = array_merge($headers, $this->headers());
39
40
        return parent::json($method, $uri, $data, $headers);
41
    }
42
43
    protected function disableExceptionHandling()
44
    {
45
        $this->oldExceptionHandler = $this->app->make(ExceptionHandler::class);
46
        $this->app->instance(ExceptionHandler::class, new class extends Handler {
47
            public function __construct() {}
48
            public function report(\Exception $e) {}
49
            public function render($request, \Exception $e) {
50
                throw $e;
51
            }
52
        });
53
    }
54
    protected function withExceptionHandling()
55
    {
56
        $this->app->instance(ExceptionHandler::class, $this->oldExceptionHandler);
0 ignored issues
show
Bug Best Practice introduced by
The property oldExceptionHandler does not exist on Tests\AuthenticatedTestCase. Did you maybe forget to declare it?
Loading history...
57
58
        return $this;
59
    }
60
}
61