3
3
namespace rjapi \blocks ;
4
4
5
5
use Faker \Factory ;
6
- use rjapi \extension \JSONApiInterface ;
7
6
use rjapi \helpers \Classes ;
8
7
use rjapi \helpers \MethodOptions ;
9
8
use rjapi \types \DefaultInterface ;
@@ -17,6 +16,7 @@ class Tests
17
16
use ContentManager;
18
17
19
18
private $ className ;
19
+ private $ attributesState = [];
20
20
21
21
protected $ sourceCode = '' ;
22
22
protected $ isSoftDelete = false ;
@@ -42,6 +42,7 @@ public function setContent() : void
42
42
$ this ->startMethod ($ methodOpts );
43
43
$ this ->endMethod ();
44
44
// main test methods
45
+ $ this ->collectProps ();
45
46
$ this ->setComment (DefaultInterface::METHOD_START );
46
47
$ this ->setCreateContent ($ methodOpts );
47
48
$ this ->setIndexContent ($ methodOpts );
@@ -64,7 +65,7 @@ private function setIndexContent(MethodOptions $methodOpts) : void
64
65
$ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEND_GET ,
65
66
[PhpInterface::SLASH . $ this ->generator ->version . PhpInterface::SLASH . mb_strtolower ($ this ->generator ->objectName )]);
66
67
$ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_IS_JSON );
67
- $ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_CONTAINS , [$ this ->getJsonApiRequest ()] );
68
+ $ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_CONTAINS_JSON , [$ this ->getJsonApiResponse ( true )], false );
68
69
$ this ->endMethod ();
69
70
}
70
71
@@ -73,7 +74,7 @@ private function setIndexContent(MethodOptions $methodOpts) : void
73
74
*/
74
75
private function setViewContent (MethodOptions $ methodOpts ) : void
75
76
{
76
- $ id = 1 ;
77
+ $ id = $ id = $ this -> getId () ;
77
78
$ methodOpts ->setName (TestsInterface::TRY . $ this ->generator ->objectName . ucfirst (MethodsInterface::VIEW ));
78
79
$ this ->startMethod ($ methodOpts );
79
80
$ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::IM_GOING_TO ,
@@ -83,7 +84,7 @@ private function setViewContent(MethodOptions $methodOpts) : void
83
84
[PhpInterface::SLASH . $ this ->generator ->version . PhpInterface::SLASH . mb_strtolower ($ this ->generator ->objectName )
84
85
. PhpInterface::SLASH . $ id ]);
85
86
$ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_IS_JSON );
86
- $ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_CONTAINS , [$ this ->getJsonApiRequest ()] );
87
+ $ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_CONTAINS_JSON , [$ this ->getJsonApiResponse ( true )], false );
87
88
$ this ->endMethod ();
88
89
}
89
90
@@ -101,7 +102,7 @@ private function setCreateContent(MethodOptions $methodOpts) : void
101
102
[PhpInterface::SLASH . $ this ->generator ->version . PhpInterface::SLASH . mb_strtolower ($ this ->generator ->objectName ),
102
103
$ this ->getJsonApiRequest ()]);
103
104
$ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_IS_JSON );
104
- $ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_CONTAINS , [$ this ->getJsonApiRequest ()] );
105
+ $ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_CONTAINS_JSON , [$ this ->getJsonApiResponse ( true )], false );
105
106
$ this ->endMethod ();
106
107
}
107
108
@@ -110,7 +111,7 @@ private function setCreateContent(MethodOptions $methodOpts) : void
110
111
*/
111
112
private function setUpdateContent (MethodOptions $ methodOpts ) : void
112
113
{
113
- $ id = 1 ;
114
+ $ id = $ id = $ this -> getId () ;
114
115
$ methodOpts ->setName (TestsInterface::TRY . $ this ->generator ->objectName . ucfirst (MethodsInterface::UPDATE ));
115
116
$ this ->startMethod ($ methodOpts );
116
117
$ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::IM_GOING_TO ,
@@ -120,7 +121,7 @@ private function setUpdateContent(MethodOptions $methodOpts) : void
120
121
[PhpInterface::SLASH . $ this ->generator ->version . PhpInterface::SLASH . mb_strtolower ($ this ->generator ->objectName )
121
122
. PhpInterface::SLASH . $ id , $ this ->getJsonApiRequest ()]);
122
123
$ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_IS_JSON );
123
- $ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_CONTAINS , [$ this ->getJsonApiRequest ()] );
124
+ $ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::SEE_RESP_CONTAINS_JSON , [$ this ->getJsonApiResponse ( true )], false );
124
125
$ this ->endMethod ();
125
126
}
126
127
@@ -129,7 +130,10 @@ private function setUpdateContent(MethodOptions $methodOpts) : void
129
130
*/
130
131
private function setDeleteContent (MethodOptions $ methodOpts ) : void
131
132
{
132
- $ id = 1 ;
133
+ $ id = $ this ->getId ();
134
+ if (empty ($ this ->attributesState [RamlInterface::RAML_ID ]) === false ) {
135
+ $ id = $ this ->attributesState [RamlInterface::RAML_ID ];
136
+ }
133
137
$ methodOpts ->setName (TestsInterface::TRY . $ this ->generator ->objectName . ucfirst (MethodsInterface::DELETE ));
134
138
$ this ->startMethod ($ methodOpts );
135
139
$ this ->methodCallOnObject (TestsInterface::PARAM_I , TestsInterface::IM_GOING_TO ,
@@ -140,15 +144,30 @@ private function setDeleteContent(MethodOptions $methodOpts) : void
140
144
$ this ->endMethod ();
141
145
}
142
146
147
+ private function getId ()
148
+ {
149
+ if (empty ($ this ->attributesState [RamlInterface::RAML_ID ]) === false ) {
150
+ return $ this ->attributesState [RamlInterface::RAML_ID ];
151
+ }
152
+ return 1 ;
153
+ }
154
+
143
155
/**
156
+ * @param bool $required
144
157
* @return array
145
158
*/
146
- private function getJsonApiRequest () : array
159
+ private function getJsonApiRequest ($ required = false ) : array
147
160
{
148
161
$ attrs = [];
162
+ // set id if it's string
163
+ if (empty ($ this ->attributesState [RamlInterface::RAML_ID ]) === false ) {
164
+ $ attrs [RamlInterface::RAML_ID ] = $ this ->attributesState [RamlInterface::RAML_ID ];
165
+ }
149
166
$ props = $ this ->generator ->types [$ this ->generator ->objectProps [RamlInterface::RAML_ATTRS ]][RamlInterface::RAML_PROPS ];
150
- foreach ($ props as $ attrKey => $ attrVal ) {
151
- $ attrs [$ attrKey ] = $ this ->getAttributeValue ($ attrVal );
167
+ foreach ($ this ->attributesState as $ attrKey => $ attrVal ) {
168
+ if ($ required === false || ($ required === true && empty ($ props [$ attrKey ][RamlInterface::RAML_KEY_REQUIRED ]) === false )) {
169
+ $ attrs [$ attrKey ] = $ attrVal ;
170
+ }
152
171
}
153
172
return [
154
173
RamlInterface::RAML_DATA => [
@@ -158,6 +177,40 @@ private function getJsonApiRequest() : array
158
177
];
159
178
}
160
179
180
+ /**
181
+ * @param bool $required
182
+ * @return array
183
+ */
184
+ private function getJsonApiResponse ($ required = false ) : array
185
+ {
186
+ $ attrs = [];
187
+ $ props = $ this ->generator ->types [$ this ->generator ->objectProps [RamlInterface::RAML_ATTRS ]][RamlInterface::RAML_PROPS ];
188
+ foreach ($ this ->attributesState as $ attrKey => $ attrVal ) {
189
+ if ($ required === false || ($ required === true && empty ($ props [$ attrKey ][RamlInterface::RAML_KEY_REQUIRED ]) === false )) {
190
+ $ attrs [$ attrKey ] = $ attrVal ;
191
+ }
192
+ }
193
+ return [
194
+ RamlInterface::RAML_DATA => [
195
+ RamlInterface::RAML_TYPE => mb_strtolower ($ this ->generator ->objectName ),
196
+ RamlInterface::RAML_ID => $ this ->getId (),
197
+ RamlInterface::RAML_ATTRS => $ attrs ,
198
+ ],
199
+ ];
200
+ }
201
+
202
+ private function collectProps ()
203
+ {
204
+ $ idObject = $ this ->generator ->types [$ this ->generator ->types [$ this ->generator ->objectName ][RamlInterface::RAML_PROPS ][RamlInterface::RAML_ID ]];
205
+ if ($ idObject [RamlInterface::RAML_TYPE ] === RamlInterface::RAML_TYPE_STRING ) {
206
+ $ this ->attributesState [RamlInterface::RAML_ID ] = uniqid ();
207
+ }
208
+ $ props = $ this ->generator ->types [$ this ->generator ->objectProps [RamlInterface::RAML_ATTRS ]][RamlInterface::RAML_PROPS ];
209
+ foreach ($ props as $ attrKey => $ attrVal ) {
210
+ $ this ->attributesState [$ attrKey ] = $ this ->getAttributeValue ($ attrVal );
211
+ }
212
+ }
213
+
161
214
/**
162
215
* @param array $attrVal
163
216
* @return mixed
0 commit comments