1616use Behat \Behat \Context \Context ;
1717use Behat \Behat \Context \Environment \InitializedContextEnvironment ;
1818use Behat \Behat \Hook \Scope \BeforeScenarioScope ;
19+ use Behat \Gherkin \Node \PyStringNode ;
1920use Behatch \Context \RestContext ;
21+ use Behatch \Json \Json ;
2022use PHPUnit \Framework \Assert ;
2123use PHPUnit \Framework \ExpectationFailedException ;
2224
@@ -42,51 +44,25 @@ public function gatherContexts(BeforeScenarioScope $scope): void
4244 $ this ->restContext = $ restContext ;
4345 }
4446
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-
5747 /**
5848 * @Then the OpenAPI class :class exists
5949 */
6050 public function assertTheOpenApiClassExist (string $ className ): void
6151 {
6252 try {
63- $ this ->getClassInfo ($ className, 3 );
53+ $ this ->getClassInfo ($ className );
6454 } catch (\InvalidArgumentException $ e ) {
6555 throw new ExpectationFailedException (sprintf ('The class "%s" doesn \'t exist. ' , $ className ), null , $ e );
6656 }
6757 }
6858
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-
8359 /**
8460 * @Then the OpenAPI class :class doesn't exist
8561 */
8662 public function assertTheOpenAPIClassNotExist (string $ className ): void
8763 {
8864 try {
89- $ this ->getClassInfo ($ className, 3 );
65+ $ this ->getClassInfo ($ className );
9066 } catch (\InvalidArgumentException ) {
9167 return ;
9268 }
@@ -95,7 +71,6 @@ public function assertTheOpenAPIClassNotExist(string $className): void
9571 }
9672
9773 /**
98- * @Then the Swagger path :arg1 exists
9974 * @Then the OpenAPI path :arg1 exists
10075 */
10176 public function assertThePathExist (string $ path ): void
@@ -105,54 +80,32 @@ public function assertThePathExist(string $path): void
10580 Assert::assertTrue (isset ($ json ->paths ) && isset ($ json ->paths ->{$ path }));
10681 }
10782
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-
12083 /**
12184 * @Then the :prop property exists for the OpenAPI class :class
12285 */
12386 public function assertThePropertyExistForTheOpenApiClass (string $ propertyName , string $ className ): void
12487 {
12588 try {
126- $ this ->getPropertyInfo ($ propertyName , $ className, 3 );
89+ $ this ->getPropertyInfo ($ propertyName , $ className );
12790 } catch (\InvalidArgumentException $ e ) {
12891 throw new ExpectationFailedException (sprintf ('Property "%s" of class "%s" doesn \'t exist. ' , $ propertyName , $ className ), null , $ e );
12992 }
13093 }
13194
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-
14295 /**
14396 * @Then the :prop property is required for the OpenAPI class :class
14497 */
14598 public function assertThePropertyIsRequiredForTheOpenAPIClass (string $ propertyName , string $ className ): void
14699 {
147- if (!\in_array ($ propertyName , $ this ->getClassInfo ($ className, 3 )->required , true )) {
100+ if (!\in_array ($ propertyName , $ this ->getClassInfo ($ className )->required , true )) {
148101 throw new ExpectationFailedException (sprintf ('Property "%s" of class "%s" should be required ' , $ propertyName , $ className ));
149102 }
150103 }
151104
152105 /**
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
154107 */
155- public function assertThePropertyIsNotReadOnlyForTheSwaggerClass (string $ propertyName , string $ className ): void
108+ public function assertThePropertyIsNotReadOnlyForTheOpenAPIClass (string $ propertyName , string $ className ): void
156109 {
157110 $ propertyInfo = $ this ->getPropertyInfo ($ propertyName , $ className );
158111 if (property_exists ($ propertyInfo , 'readOnly ' ) && $ propertyInfo ->readOnly ) {
@@ -161,13 +114,15 @@ public function assertThePropertyIsNotReadOnlyForTheSwaggerClass(string $propert
161114 }
162115
163116 /**
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:
165118 */
166- public function assertThePropertyIsNotReadOnlyForTheOpenAPIClass (string $ propertyName , string $ className ): void
119+ public function assertThePropertyForTheOpenAPIClassShouldBeEqualTo (string $ propertyName , string $ className, PyStringNode $ propertyContent ): void
167120 {
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 ));
171126 }
172127 }
173128
@@ -176,12 +131,10 @@ public function assertThePropertyIsNotReadOnlyForTheOpenAPIClass(string $propert
176131 *
177132 * @throws \InvalidArgumentException
178133 */
179- private function getPropertyInfo (string $ propertyName , string $ className, int $ specVersion = 2 ): \stdClass
134+ private function getPropertyInfo (string $ propertyName , string $ className ): \stdClass
180135 {
181- /**
182- * @var iterable $properties
183- */
184- $ properties = $ this ->getProperties ($ className , $ specVersion );
136+ /** @var iterable $properties */
137+ $ properties = $ this ->getProperties ($ className );
185138 foreach ($ properties as $ classPropertyName => $ property ) {
186139 if ($ classPropertyName === $ propertyName ) {
187140 return $ property ;
@@ -194,19 +147,19 @@ private function getPropertyInfo(string $propertyName, string $className, int $s
194147 /**
195148 * Gets all operations of a given class.
196149 */
197- private function getProperties (string $ className, int $ specVersion = 2 ): \stdClass
150+ private function getProperties (string $ className ): \stdClass
198151 {
199- return $ this ->getClassInfo ($ className, $ specVersion )->{'properties ' } ?? new \stdClass ();
152+ return $ this ->getClassInfo ($ className )->{'properties ' } ?? new \stdClass ();
200153 }
201154
202155 /**
203156 * Gets information about a class.
204157 *
205158 * @throws \InvalidArgumentException
206159 */
207- private function getClassInfo (string $ className, int $ specVersion = 2 ): \stdClass
160+ private function getClassInfo (string $ className ): \stdClass
208161 {
209- $ nodes = 2 === $ specVersion ? $ this -> getLastJsonResponse ()->{ ' definitions ' } : $ this ->getLastJsonResponse ()->{'components ' }->{'schemas ' };
162+ $ nodes = $ this ->getLastJsonResponse ()->{'components ' }->{'schemas ' };
210163 foreach ($ nodes as $ classTitle => $ classData ) {
211164 if ($ classTitle === $ className ) {
212165 return $ classData ;
0 commit comments