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\Functional; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Response\VndValidationErrorFactory; |
12
|
|
|
use KleijnWeb\SwaggerBundle\Test\ApiResponseErrorException; |
13
|
|
|
use KleijnWeb\SwaggerBundle\Test\ApiTestCase; |
14
|
|
|
use Nocarrier\Hal; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author John Kleijn <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class VndParameterValidationErrorTest extends WebTestCase |
21
|
|
|
{ |
22
|
|
|
use ApiTestCase; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Use config_basic.yml |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $env = 'basic'; |
30
|
|
|
|
31
|
|
|
public static function setUpBeforeClass() |
32
|
|
|
{ |
33
|
|
|
static::initSchemaManager(__DIR__ . '/PetStore/app/swagger/petstore.yml'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @test |
38
|
|
|
*/ |
39
|
|
|
public function parameterValidationErrorWillContainDefaultMessageAndLogref() |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
$this->get('/v2/pet/findByStatus', ['status' => 'bogus']); |
43
|
|
|
} catch (ApiResponseErrorException $e) { |
44
|
|
|
$data = Hal::fromJson($e->getJson(), 10)->getData(); |
45
|
|
|
$this->assertSame(VndValidationErrorFactory::DEFAULT_MESSAGE, $data['message']); |
46
|
|
|
$this->assertRegExp('/[0-9a-z]+/', $data['logref']); |
47
|
|
|
|
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
$this->fail("Expected exception"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function parameterValidationErrorWillContainSpecificationPointer() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
try { |
59
|
|
|
$this->get('/v2/pet/findByStatus', ['status' => 'bogus']); |
60
|
|
|
} catch (ApiResponseErrorException $e) { |
61
|
|
|
$error = Hal::fromJson($e->getJson(), 10); |
62
|
|
|
$resource = $error->getFirstResource('errors'); |
63
|
|
|
$specLink = 'http://petstore.swagger.io/swagger/petstore.yml#/paths/~1pet~1findByStatus/get/parameters/0'; |
64
|
|
|
$this->assertSame($specLink, $resource->getUri()); |
65
|
|
|
|
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
$this->fail("Expected exception"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function parameterValidationErrorWillContainSpecificationPointerForBodyParameter() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->markTestIncomplete('Not working with JSON-Schema version'); |
77
|
|
|
|
78
|
|
|
$url = 'http://petstore.swagger.io/swagger/petstore.yml'; |
79
|
|
|
try { |
80
|
|
|
$this->post('/v2/store/order', []); |
81
|
|
|
} catch (ApiResponseErrorException $e) { |
82
|
|
|
$error = Hal::fromJson($e->getJson(), 10); |
83
|
|
|
$resource = $error->getFirstResource('errors'); |
84
|
|
|
$specLink = $url . '#/paths/~1pet~1findByStatus/get/parameters/0/body/properties/quantity'; |
85
|
|
|
$this->assertSame($specLink, $resource->getUri()); |
86
|
|
|
|
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
$this->fail("Expected exception"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function parameterValidationErrorWillContainSchemaPointer() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
try { |
98
|
|
|
$this->get('/v2/pet/findByStatus', ['status' => 'bogus']); |
99
|
|
|
} catch (ApiResponseErrorException $e) { |
100
|
|
|
$error = Hal::fromJson($e->getJson(), 10); |
101
|
|
|
$resource = $error->getFirstResource('errors'); |
102
|
|
|
$data = $resource->getData(); |
103
|
|
|
$this->assertSame('/paths/~1pet~1findByStatus/get/x-request-schema/properties/status', $data['path']); |
104
|
|
|
|
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
$this->fail("Expected exception"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
*/ |
113
|
|
|
public function parameterValidationErrorCanContainMultipleErrors() |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
|
|
$this->get('/v2/user/login'); |
117
|
|
|
} catch (ApiResponseErrorException $e) { |
118
|
|
|
$error = Hal::fromJson($e->getJson(), 10); |
119
|
|
|
$resources = $error->getResources(); |
120
|
|
|
$this->assertArrayHasKey('errors', $resources); |
121
|
|
|
/** |
122
|
|
|
* @var int $i |
123
|
|
|
* @var Hal $resource |
124
|
|
|
*/ |
125
|
|
|
foreach ($resources['errors'] as $i => $resource) { |
126
|
|
|
$data = $resource->getData(); |
127
|
|
|
if ($i == 0) { |
128
|
|
|
$this->assertSame('/paths/~1user~1login/get/x-request-schema/properties/username', $data['path']); |
129
|
|
|
$uri = 'http://petstore.swagger.io/swagger/petstore.yml#/paths/~1user~1login/get/parameters/0'; |
130
|
|
|
$this->assertSame($uri, $resource->getUri()); |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
$this->assertSame('/paths/~1user~1login/get/x-request-schema/properties/password', $data['path']); |
134
|
|
|
$uri = 'http://petstore.swagger.io/swagger/petstore.yml#/paths/~1user~1login/get/parameters/1'; |
135
|
|
|
$this->assertSame($uri, $resource->getUri()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
$this->fail("Expected exception"); |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.