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; |
10
|
|
|
|
11
|
|
|
use JsonSchema\Validator; |
12
|
|
|
use KleijnWeb\SwaggerBundle\Document\ParameterRefBuilder; |
13
|
|
|
use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException; |
14
|
|
|
use KleijnWeb\SwaggerBundle\Response\VndValidationErrorFactory; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author John Kleijn <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class VndValidationErrorFactoryTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var VndValidationErrorFactory |
24
|
|
|
*/ |
25
|
|
|
private $factory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ParameterRefBuilder |
29
|
|
|
*/ |
30
|
|
|
private $refBuilder; |
31
|
|
|
|
32
|
|
|
protected function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->refBuilder = $this |
35
|
|
|
->getMockBuilder('KleijnWeb\SwaggerBundle\Document\ParameterRefBuilder') |
36
|
|
|
->disableOriginalConstructor() |
37
|
|
|
->getMock(); |
38
|
|
|
$this->factory = new VndValidationErrorFactory($this->refBuilder); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
*/ |
44
|
|
|
public function createdErrorCanHaveLogRef() |
45
|
|
|
{ |
46
|
|
|
$vndError = $this->factory->create( |
47
|
|
|
new Request(), |
48
|
|
|
new InvalidParametersException('Yikes', []), |
49
|
|
|
'123456789' |
50
|
|
|
); |
51
|
|
|
$this->assertInstanceOf('Ramsey\VndError\VndError', $vndError); |
52
|
|
|
$this->assertSame('123456789', $vndError->getLogref()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @test |
57
|
|
|
*/ |
58
|
|
|
public function createdErrorCanHaveNoLogRef() |
59
|
|
|
{ |
60
|
|
|
$this->assertNull( |
61
|
|
|
$this->factory->create( |
62
|
|
|
new Request(), |
63
|
|
|
new InvalidParametersException('Yikes', []) |
64
|
|
|
)->getLogref() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
*/ |
71
|
|
|
public function resultIncludesErrorMessagesCreatedByJsonSchema() |
72
|
|
|
{ |
73
|
|
|
$value = (object)[ |
74
|
|
|
'foo' => (object)[ |
75
|
|
|
'blah' => 'one' |
76
|
|
|
], |
77
|
|
|
'bar' => (object)[] |
78
|
|
|
]; |
79
|
|
|
$validator = new Validator(); |
80
|
|
|
$schema = (object)[ |
81
|
|
|
'type' => 'object', |
82
|
|
|
'required' => ['foo', 'bar'], |
83
|
|
|
'properties' => (object)[ |
84
|
|
|
'foo' => (object)[ |
85
|
|
|
'type' => 'object', |
86
|
|
|
'properties' => (object)[ |
87
|
|
|
'blah' => (object)[ |
88
|
|
|
'type' => 'integer' |
89
|
|
|
] |
90
|
|
|
] |
91
|
|
|
], |
92
|
|
|
'bar' => (object)[ |
93
|
|
|
'type' => 'object', |
94
|
|
|
'required' => ['blah'], |
95
|
|
|
'properties' => (object)[ |
96
|
|
|
'blah' => (object)[ |
97
|
|
|
'type' => 'string' |
98
|
|
|
] |
99
|
|
|
] |
100
|
|
|
] |
101
|
|
|
] |
102
|
|
|
]; |
103
|
|
|
$validator->check($value, $schema); |
104
|
|
|
$errors = $validator->getErrors(); |
105
|
|
|
|
106
|
|
|
$exception = new InvalidParametersException('Nope', $errors); |
107
|
|
|
|
108
|
|
|
$mock = $this->refBuilder; |
109
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject $mock */ |
110
|
|
|
$mock |
111
|
|
|
->expects($this->exactly(2)) |
112
|
|
|
->method('buildSpecificationLink') |
113
|
|
|
->willReturnOnConsecutiveCalls('http://1.net/1', 'http://2.net/2'); |
114
|
|
|
|
115
|
|
|
$vndError = $this->factory->create( |
116
|
|
|
new Request(), |
117
|
|
|
$exception |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$resources = $vndError->getResources(); |
121
|
|
|
$this->assertArrayHasKey('errors', $resources); |
122
|
|
|
$errorResources = $resources['errors']; |
123
|
|
|
$this->assertSame(count($errors), count($errorResources)); |
124
|
|
|
|
125
|
|
|
$resources = array_values($errorResources); |
126
|
|
|
|
127
|
|
|
foreach ($errors as $i => $spec) { |
128
|
|
|
$data = $resources[$i]->getData(); |
129
|
|
|
$this->assertContains($spec['message'], $data['message']); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|