|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Response\ErrorResponseFactory; |
|
10
|
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException; |
|
12
|
|
|
use KleijnWeb\SwaggerBundle\Response\Error\HttpError; |
|
13
|
|
|
use KleijnWeb\SwaggerBundle\Response\Error\LogRefBuilder; |
|
14
|
|
|
use KleijnWeb\SwaggerBundle\Response\ErrorResponseFactory\SimpleErrorResponseFactory; |
|
15
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author John Kleijn <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class SimpleErrorResponseFactoryTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
const LOGREF = '123456789'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var SimpleErrorResponseFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
private $factory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var LogRefBuilder |
|
33
|
|
|
*/ |
|
34
|
|
|
private $logRefBuilder; |
|
35
|
|
|
|
|
36
|
|
|
protected function setUp() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->factory = new SimpleErrorResponseFactory(); |
|
39
|
|
|
$this->logRefBuilder = $mockObject = $this->getMockForAbstractClass(LogRefBuilder::class); |
|
|
|
|
|
|
40
|
|
|
$mockObject->expects($this->any())->method('create')->willReturn(self::LOGREF); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function willSetResponseWithApplicationJsonHeader() |
|
47
|
|
|
{ |
|
48
|
|
|
$response = $this->factory->create(new HttpError(new Request(), new Exception(), $this->logRefBuilder)); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertContains('application/json', $response->headers->get('Content-Type')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @test |
|
55
|
|
|
*/ |
|
56
|
|
|
public function willSetResponseWithValidJsonContent() |
|
57
|
|
|
{ |
|
58
|
|
|
$response = $this->factory->create(new HttpError(new Request(), new Exception(), $this->logRefBuilder)); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertNotNull(json_decode($response->getContent())); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
public function willSetResponseWithSimpleMessage() |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
foreach ([400 => 'Bad Request', 500 => 'Internal Server Error'] as $code => $message) { |
|
69
|
|
|
$response = $this->factory->create( |
|
70
|
|
|
new HttpError(new Request(), new Exception('Ai caramba!', $code), $this->logRefBuilder) |
|
71
|
|
|
); |
|
72
|
|
|
$this->assertNotNull($body = json_decode($response->getContent())); |
|
73
|
|
|
$this->assertEquals($message, $body->message); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @test |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function willSetResponseWithLogRef() |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
foreach ([400, 500] as $code) { |
|
83
|
|
|
$response = $this->factory->create( |
|
84
|
|
|
new HttpError(new Request(), new Exception('Ai caramba!', $code), $this->logRefBuilder) |
|
85
|
|
|
); |
|
86
|
|
|
$this->assertSame(self::LOGREF, json_decode($response->getContent())->logref); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @test |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function willReturn404ResponsesForNotFoundHttpException() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$response = $this->factory->create( |
|
96
|
|
|
new HttpError(new Request(), new NotFoundHttpException(), $this->logRefBuilder) |
|
97
|
|
|
); |
|
98
|
|
|
$this->assertSame(404, $response->getStatusCode()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @test |
|
103
|
|
|
*/ |
|
104
|
|
|
public function willCreateValidationErrorResponse() |
|
105
|
|
|
{ |
|
106
|
|
|
$validationErrors = ['Wrong.', 'Wrong.', 'Wrong!']; |
|
107
|
|
|
|
|
108
|
|
|
$exception = new InvalidParametersException('Oh noes', $validationErrors); |
|
109
|
|
|
$request = new Request(); |
|
110
|
|
|
|
|
111
|
|
|
$response = $this->factory->create(new HttpError($request, $exception, $this->logRefBuilder)); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertSame($validationErrors, json_decode($response->getContent())->errors); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..