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