|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: benedikt |
|
6
|
|
|
* Date: 10/1/17 |
|
7
|
|
|
* Time: 2:08 PM |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Tests\Unit\Exceptions; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use Exception; |
|
14
|
|
|
use Illuminate\Http\JsonResponse; |
|
15
|
|
|
use Illuminate\Http\Request; |
|
16
|
|
|
use Illuminate\Support\MessageBag; |
|
17
|
|
|
use Illuminate\Validation\ValidationException; |
|
18
|
|
|
use Illuminate\Validation\Validator; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
20
|
|
|
use Tfboe\FmLib\Exceptions\AuthenticationException; |
|
21
|
|
|
use Tfboe\FmLib\Exceptions\DuplicateException; |
|
22
|
|
|
use Tfboe\FmLib\Exceptions\Handler; |
|
23
|
|
|
use Tfboe\FmLib\Exceptions\PlayerAlreadyExists; |
|
24
|
|
|
use Tfboe\FmLib\Exceptions\ReferenceException; |
|
25
|
|
|
use Tfboe\FmLib\Exceptions\UnorderedPhaseNumberException; |
|
26
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class HandlerTest |
|
30
|
|
|
* @package Tfboe\FmLib\Tests\Unit\Exceptions |
|
31
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
32
|
|
|
*/ |
|
33
|
|
|
class HandlerTest extends UnitTestCase |
|
34
|
|
|
{ |
|
35
|
|
|
//<editor-fold desc="Public Methods"> |
|
36
|
|
|
/** |
|
37
|
|
|
* @covers \Tfboe\FmLib\Exceptions\Handler::render |
|
38
|
|
|
* @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionHTTPStatusCode |
|
39
|
|
|
* @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionName |
|
40
|
|
|
* @covers \Tfboe\FmLib\Exceptions\Handler::getJsonMessage |
|
41
|
|
|
* @uses \Tfboe\FmLib\Exceptions\AuthenticationException |
|
42
|
|
|
* @uses \Tfboe\FmLib\Exceptions\DuplicateException |
|
43
|
|
|
* @uses \Tfboe\FmLib\Exceptions\PlayerAlreadyExists |
|
44
|
|
|
* @uses \Tfboe\FmLib\Exceptions\ReferenceException |
|
45
|
|
|
* @uses \Tfboe\FmLib\Exceptions\UnorderedPhaseNumberException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testRender() |
|
48
|
|
|
{ |
|
49
|
|
|
$handler = $this->handler(); |
|
50
|
|
|
$exception = new Exception("Exception message"); |
|
51
|
|
|
$res = $handler->render($this->request(), $exception); |
|
52
|
|
|
self::assertInstanceOf(JsonResponse::class, $res); |
|
53
|
|
|
/** @var JsonResponse $res */ |
|
54
|
|
|
self::assertEquals(['status' => 500, 'message' => 'Exception message', 'name' => 'InternalException'], |
|
55
|
|
|
$res->getData(true)); |
|
56
|
|
|
|
|
57
|
|
|
$exception = new Exception("Exception message", 402); |
|
58
|
|
|
$res = $handler->render($this->request(), $exception); |
|
59
|
|
|
self::assertInstanceOf(JsonResponse::class, $res); |
|
60
|
|
|
/** @var JsonResponse $res */ |
|
61
|
|
|
self::assertEquals(['status' => 402, 'message' => 'Exception message', 'name' => 'InternalException'], |
|
62
|
|
|
$res->getData(true)); |
|
63
|
|
|
|
|
64
|
|
|
$exception = new AuthenticationException("Authentication Exception message"); |
|
65
|
|
|
$res = $handler->render($this->request(), $exception); |
|
66
|
|
|
self::assertInstanceOf(JsonResponse::class, $res); |
|
67
|
|
|
/** @var JsonResponse $res */ |
|
68
|
|
|
self::assertEquals(['status' => 401, 'message' => 'Authentication Exception message', |
|
69
|
|
|
'name' => 'AuthenticationException'], $res->getData(true)); |
|
70
|
|
|
|
|
71
|
|
|
$exception = new DuplicateException('value', 'name', 'array'); |
|
72
|
|
|
$res = $handler->render($this->request(), $exception); |
|
73
|
|
|
self::assertInstanceOf(JsonResponse::class, $res); |
|
74
|
|
|
/** @var JsonResponse $res */ |
|
75
|
|
|
self::assertEquals(['status' => 409, 'message' => 'Duplicate Exception', |
|
76
|
|
|
'name' => 'DuplicateException', 'duplicateValue' => 'value', 'arrayName' => 'array'], $res->getData(true)); |
|
77
|
|
|
|
|
78
|
|
|
$exception = new ReferenceException('value', 'name'); |
|
79
|
|
|
$res = $handler->render($this->request(), $exception); |
|
80
|
|
|
self::assertInstanceOf(JsonResponse::class, $res); |
|
81
|
|
|
/** @var JsonResponse $res */ |
|
82
|
|
|
self::assertEquals(['status' => 409, 'message' => 'Reference Exception', |
|
83
|
|
|
'name' => 'ReferenceException', 'referenceValue' => 'value', 'referenceName' => 'name'], $res->getData(true)); |
|
84
|
|
|
|
|
85
|
|
|
$exception = new UnorderedPhaseNumberException(2, 1); |
|
86
|
|
|
$res = $handler->render($this->request(), $exception); |
|
87
|
|
|
self::assertInstanceOf(JsonResponse::class, $res); |
|
88
|
|
|
/** @var JsonResponse $res */ |
|
89
|
|
|
self::assertEquals(['status' => 409, 'message' => 'Unordered Phase Number Exception', |
|
90
|
|
|
'name' => 'UnorderedPhaseNumberException', 'previousPhaseNumber' => 2, 'nextPhaseNumber' => 1], |
|
91
|
|
|
$res->getData(true)); |
|
92
|
|
|
|
|
93
|
|
|
$exception = new PlayerAlreadyExists([]); |
|
94
|
|
|
$res = $handler->render($this->request(), $exception); |
|
95
|
|
|
self::assertInstanceOf(JsonResponse::class, $res); |
|
96
|
|
|
/** @var JsonResponse $res */ |
|
97
|
|
|
self::assertEquals(['status' => 409, 'message' => 'Some players do already exist', |
|
98
|
|
|
'name' => 'PlayerAlreadyExistsException', 'players' => []], $res->getData(true)); |
|
99
|
|
|
|
|
100
|
|
|
// test trace in debug mode |
|
101
|
|
|
$exception = new PlayerAlreadyExists([]); |
|
102
|
|
|
$res = $handler->render($this->request(), $exception, true); |
|
103
|
|
|
self::assertInstanceOf(JsonResponse::class, $res); |
|
104
|
|
|
/** @var JsonResponse $res */ |
|
105
|
|
|
$data = $res->getData(true); |
|
106
|
|
|
self::assertArrayIsSubset(['status' => 409, 'message' => 'Some players do already exist', |
|
107
|
|
|
'name' => 'PlayerAlreadyExistsException', 'players' => []], $data); |
|
108
|
|
|
self::assertArrayHasKey('trace', $data); |
|
109
|
|
|
self::assertNotEmpty($data['trace']); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @covers \Tfboe\FmLib\Exceptions\Handler::render |
|
114
|
|
|
* @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionHTTPStatusCode |
|
115
|
|
|
* @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionName |
|
116
|
|
|
* @covers \Tfboe\FmLib\Exceptions\Handler::getJsonMessage |
|
117
|
|
|
*/ |
|
118
|
|
|
public function testRenderValidationErrors() |
|
119
|
|
|
{ |
|
120
|
|
|
$handler = $this->handler(); |
|
121
|
|
|
$validator = $this->createMock(Validator::class); |
|
122
|
|
|
$errors = $this->createMock(MessageBag::class); |
|
123
|
|
|
$errors->method('messages')->willReturn(['username' => ['The username field is required.']]); |
|
124
|
|
|
$validator->method('errors')->willReturn($errors); |
|
125
|
|
|
/** @var Validator $validator */ |
|
126
|
|
|
$exception = new ValidationException($validator, new Response('', 422)); |
|
127
|
|
|
$request = $this->request(); |
|
128
|
|
|
$res = $handler->render($request, $exception); |
|
129
|
|
|
self::assertInstanceOf(JsonResponse::class, $res); |
|
130
|
|
|
/** @var JsonResponse $res */ |
|
131
|
|
|
self::assertEquals(['message' => 'The given data was invalid.', 'errors' => |
|
132
|
|
|
['username' => ['The username field is required.']], |
|
133
|
|
|
'status' => 422, 'name' => 'ValidationException'], $res->getData(true)); |
|
134
|
|
|
} |
|
135
|
|
|
//</editor-fold desc="Public Methods"> |
|
136
|
|
|
|
|
137
|
|
|
//<editor-fold desc="Private Methods"> |
|
138
|
|
|
/** |
|
139
|
|
|
* @return Handler a new handler |
|
140
|
|
|
*/ |
|
141
|
|
|
private function handler() |
|
142
|
|
|
{ |
|
143
|
|
|
return new Handler(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return Request a new request |
|
148
|
|
|
*/ |
|
149
|
|
|
private function request() |
|
150
|
|
|
{ |
|
151
|
|
|
return new Request(); |
|
152
|
|
|
} |
|
153
|
|
|
//</editor-fold desc="Private Methods"> |
|
154
|
|
|
} |