16
16
use Behat \Behat \Context \Context ;
17
17
use Behat \Behat \Context \Environment \InitializedContextEnvironment ;
18
18
use Behat \Behat \Hook \Scope \BeforeScenarioScope ;
19
+ use Behat \Gherkin \Node \PyStringNode ;
19
20
use Behatch \Context \RestContext ;
21
+ use Behatch \Json \Json ;
20
22
use PHPUnit \Framework \Assert ;
21
23
use PHPUnit \Framework \ExpectationFailedException ;
22
24
@@ -42,51 +44,25 @@ public function gatherContexts(BeforeScenarioScope $scope): void
42
44
$ this ->restContext = $ restContext ;
43
45
}
44
46
45
- /**
46
- * @Then the Swagger class :class exists
47
- */
48
- public function assertTheSwaggerClassExist (string $ className ): void
49
- {
50
- try {
51
- $ this ->getClassInfo ($ className );
52
- } catch (\InvalidArgumentException $ e ) {
53
- throw new ExpectationFailedException (sprintf ('The class "%s" doesn \'t exist. ' , $ className ), null , $ e );
54
- }
55
- }
56
-
57
47
/**
58
48
* @Then the OpenAPI class :class exists
59
49
*/
60
50
public function assertTheOpenApiClassExist (string $ className ): void
61
51
{
62
52
try {
63
- $ this ->getClassInfo ($ className, 3 );
53
+ $ this ->getClassInfo ($ className );
64
54
} catch (\InvalidArgumentException $ e ) {
65
55
throw new ExpectationFailedException (sprintf ('The class "%s" doesn \'t exist. ' , $ className ), null , $ e );
66
56
}
67
57
}
68
58
69
- /**
70
- * @Then the Swagger class :class doesn't exist
71
- */
72
- public function assertTheSwaggerClassNotExist (string $ className ): void
73
- {
74
- try {
75
- $ this ->getClassInfo ($ className );
76
- } catch (\InvalidArgumentException ) {
77
- return ;
78
- }
79
-
80
- throw new ExpectationFailedException (sprintf ('The class "%s" exists. ' , $ className ));
81
- }
82
-
83
59
/**
84
60
* @Then the OpenAPI class :class doesn't exist
85
61
*/
86
62
public function assertTheOpenAPIClassNotExist (string $ className ): void
87
63
{
88
64
try {
89
- $ this ->getClassInfo ($ className, 3 );
65
+ $ this ->getClassInfo ($ className );
90
66
} catch (\InvalidArgumentException ) {
91
67
return ;
92
68
}
@@ -95,7 +71,6 @@ public function assertTheOpenAPIClassNotExist(string $className): void
95
71
}
96
72
97
73
/**
98
- * @Then the Swagger path :arg1 exists
99
74
* @Then the OpenAPI path :arg1 exists
100
75
*/
101
76
public function assertThePathExist (string $ path ): void
@@ -105,54 +80,32 @@ public function assertThePathExist(string $path): void
105
80
Assert::assertTrue (isset ($ json ->paths ) && isset ($ json ->paths ->{$ path }));
106
81
}
107
82
108
- /**
109
- * @Then the :prop property exists for the Swagger class :class
110
- */
111
- public function assertThePropertyExistForTheSwaggerClass (string $ propertyName , string $ className ): void
112
- {
113
- try {
114
- $ this ->getPropertyInfo ($ propertyName , $ className );
115
- } catch (\InvalidArgumentException $ e ) {
116
- throw new ExpectationFailedException (sprintf ('Property "%s" of class "%s" doesn \'t exist. ' , $ propertyName , $ className ), null , $ e );
117
- }
118
- }
119
-
120
83
/**
121
84
* @Then the :prop property exists for the OpenAPI class :class
122
85
*/
123
86
public function assertThePropertyExistForTheOpenApiClass (string $ propertyName , string $ className ): void
124
87
{
125
88
try {
126
- $ this ->getPropertyInfo ($ propertyName , $ className, 3 );
89
+ $ this ->getPropertyInfo ($ propertyName , $ className );
127
90
} catch (\InvalidArgumentException $ e ) {
128
91
throw new ExpectationFailedException (sprintf ('Property "%s" of class "%s" doesn \'t exist. ' , $ propertyName , $ className ), null , $ e );
129
92
}
130
93
}
131
94
132
- /**
133
- * @Then the :prop property is required for the Swagger class :class
134
- */
135
- public function assertThePropertyIsRequiredForTheSwaggerClass (string $ propertyName , string $ className ): void
136
- {
137
- if (!\in_array ($ propertyName , $ this ->getClassInfo ($ className )->required , true )) {
138
- throw new ExpectationFailedException (sprintf ('Property "%s" of class "%s" should be required ' , $ propertyName , $ className ));
139
- }
140
- }
141
-
142
95
/**
143
96
* @Then the :prop property is required for the OpenAPI class :class
144
97
*/
145
98
public function assertThePropertyIsRequiredForTheOpenAPIClass (string $ propertyName , string $ className ): void
146
99
{
147
- if (!\in_array ($ propertyName , $ this ->getClassInfo ($ className, 3 )->required , true )) {
100
+ if (!\in_array ($ propertyName , $ this ->getClassInfo ($ className )->required , true )) {
148
101
throw new ExpectationFailedException (sprintf ('Property "%s" of class "%s" should be required ' , $ propertyName , $ className ));
149
102
}
150
103
}
151
104
152
105
/**
153
- * @Then the :prop property is not read only for the Swagger class :class
106
+ * @Then the :prop property is not read only for the OpenAPI class :class
154
107
*/
155
- public function assertThePropertyIsNotReadOnlyForTheSwaggerClass (string $ propertyName , string $ className ): void
108
+ public function assertThePropertyIsNotReadOnlyForTheOpenAPIClass (string $ propertyName , string $ className ): void
156
109
{
157
110
$ propertyInfo = $ this ->getPropertyInfo ($ propertyName , $ className );
158
111
if (property_exists ($ propertyInfo , 'readOnly ' ) && $ propertyInfo ->readOnly ) {
@@ -161,13 +114,15 @@ public function assertThePropertyIsNotReadOnlyForTheSwaggerClass(string $propert
161
114
}
162
115
163
116
/**
164
- * @Then the :prop property is not read only for the OpenAPI class :class
117
+ * @Then the :prop property for the OpenAPI class :class should be equal to:
165
118
*/
166
- public function assertThePropertyIsNotReadOnlyForTheOpenAPIClass (string $ propertyName , string $ className ): void
119
+ public function assertThePropertyForTheOpenAPIClassShouldBeEqualTo (string $ propertyName , string $ className, PyStringNode $ propertyContent ): void
167
120
{
168
- $ propertyInfo = $ this ->getPropertyInfo ($ propertyName , $ className , 3 );
169
- if (property_exists ($ propertyInfo , 'readOnly ' ) && $ propertyInfo ->readOnly ) {
170
- throw new ExpectationFailedException (sprintf ('Property "%s" of class "%s" should not be read only ' , $ propertyName , $ className ));
121
+ $ propertyInfo = $ this ->getPropertyInfo ($ propertyName , $ className );
122
+ $ propertyInfoJson = new Json (json_encode ($ propertyInfo ));
123
+
124
+ if (new Json ($ propertyContent ) != $ propertyInfoJson ) {
125
+ throw new ExpectationFailedException (sprintf ("Property \"%s \" of class \"%s \" is '%s' " , $ propertyName , $ className , $ propertyInfoJson ));
171
126
}
172
127
}
173
128
@@ -176,12 +131,10 @@ public function assertThePropertyIsNotReadOnlyForTheOpenAPIClass(string $propert
176
131
*
177
132
* @throws \InvalidArgumentException
178
133
*/
179
- private function getPropertyInfo (string $ propertyName , string $ className, int $ specVersion = 2 ): \stdClass
134
+ private function getPropertyInfo (string $ propertyName , string $ className ): \stdClass
180
135
{
181
- /**
182
- * @var iterable $properties
183
- */
184
- $ properties = $ this ->getProperties ($ className , $ specVersion );
136
+ /** @var iterable $properties */
137
+ $ properties = $ this ->getProperties ($ className );
185
138
foreach ($ properties as $ classPropertyName => $ property ) {
186
139
if ($ classPropertyName === $ propertyName ) {
187
140
return $ property ;
@@ -194,19 +147,19 @@ private function getPropertyInfo(string $propertyName, string $className, int $s
194
147
/**
195
148
* Gets all operations of a given class.
196
149
*/
197
- private function getProperties (string $ className, int $ specVersion = 2 ): \stdClass
150
+ private function getProperties (string $ className ): \stdClass
198
151
{
199
- return $ this ->getClassInfo ($ className, $ specVersion )->{'properties ' } ?? new \stdClass ();
152
+ return $ this ->getClassInfo ($ className )->{'properties ' } ?? new \stdClass ();
200
153
}
201
154
202
155
/**
203
156
* Gets information about a class.
204
157
*
205
158
* @throws \InvalidArgumentException
206
159
*/
207
- private function getClassInfo (string $ className, int $ specVersion = 2 ): \stdClass
160
+ private function getClassInfo (string $ className ): \stdClass
208
161
{
209
- $ nodes = 2 === $ specVersion ? $ this -> getLastJsonResponse ()->{ ' definitions ' } : $ this ->getLastJsonResponse ()->{'components ' }->{'schemas ' };
162
+ $ nodes = $ this ->getLastJsonResponse ()->{'components ' }->{'schemas ' };
210
163
foreach ($ nodes as $ classTitle => $ classData ) {
211
164
if ($ classTitle === $ className ) {
212
165
return $ classData ;
0 commit comments