Passed
Push — 2.0 ( f5ddb0...24c833 )
by Samuel
02:58 queued 47s
created

JsonHandlerTest::setUpRoutes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMartins\Exceptions\Tests\Feature;
4
5
use Exception;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Route;
8
use SMartins\Exceptions\Tests\TestCase;
9
use Illuminate\Support\Facades\Validator;
10
use Illuminate\Auth\Access\AuthorizationException;
11
use SMartins\Exceptions\Tests\Fixtures\User as Model;
12
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
13
14
class JsonHandlerTest extends TestCase
15
{
16
    public function setUp()
17
    {
18
        parent::setUp();
19
20
        $this->loadLaravelMigrations(['--database' => 'exceptions']);
21
22
        $this->setUpRoutes();
23
    }
24
25
    public function setUpRoutes()
26
    {
27
        Route::get('default_exception', function (Request $request) {
28
            throw new Exception('Test message', 1);
29
        });
30
31
        Route::get('/model_not_found', function (Request $request) {
32
            Model::findOrFail(1);
33
        });
34
35
        Route::middleware('auth')->get('/authentication', function (Request $request) {
36
            //
37
        });
38
39
        Route::get('authorization', function (Request $request) {
40
            throw new AuthorizationException('Forbidden');
41
        });
42
43
        Route::get('validation', function (Request $request) {
44
            Validator::make($request->all(), [
45
                'name' => 'required',
46
                'email' => 'email',
47
            ])->validate();
48
        });
49
50
        Route::get('bad_request', function (Request $request) {
51
            throw new BadRequestHttpException('Bad Request');
52
        });
53
    }
54
55
    public function testThrowsDefaultException()
56
    {
57
        $this->json('GET', 'default_exception')
58
            ->assertStatus(500)
59
            ->assertJsonStructure($this->defaultErrorStructure());
60
    }
61
62
    public function testThrowsModelNotFoundException()
63
    {
64
        $this->json('GET', 'model_not_found')
65
            ->assertStatus(404)
66
            ->assertJsonStructure($this->defaultErrorStructure());
67
    }
68
69
    public function testThrowsAuthenticationException()
70
    {
71
        $this->json('GET', 'authentication')
72
            ->assertStatus(401)
73
            ->assertJsonStructure($this->defaultErrorStructure());
74
    }
75
76
    public function testThrowsAuthorizationException()
77
    {
78
        $this->json('GET', 'authorization')
79
            ->assertStatus(403)
80
            ->assertJsonStructure($this->defaultErrorStructure());
81
    }
82
83
    public function testThrowsValidationExceptions()
84
    {
85
        $params = ['email' => str_repeat('a', 11)];
86
87
        $this->json('GET', 'validation', $params)
88
            ->assertStatus(400)
89
            ->assertJsonStructure($this->defaultErrorStructure());
90
    }
91
92
    public function testThrowsBadRequestHttpException()
93
    {
94
        $this->json('GET', 'bad_request')
95
            ->assertStatus(400)
96
            ->assertJsonStructure($this->defaultErrorStructure());
97
    }
98
99
    public function testThrowsNotFoundHttpException()
100
    {
101
        $this->json('GET', 'not_found_route')
102
            ->assertStatus(404)
103
            ->assertJsonStructure($this->defaultErrorStructure());
104
    }
105
106
    /**
107
     * The default json response error structure.
108
     *
109
     * @return array
110
     */
111
    public function defaultErrorStructure()
112
    {
113
        return [
114
            'errors' => [[
115
                'status',
116
                'code',
117
                'title',
118
                'detail',
119
                'source' => ['pointer']
120
            ]],
121
        ];
122
    }
123
}
124