3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \FunctionalTestingFramework \DataGenerator \Api ;
7
8
8
9
use Magento \FunctionalTestingFramework \DataGenerator \Handlers \DataObjectHandler ;
12
13
use Magento \FunctionalTestingFramework \DataGenerator \Objects \JsonElement ;
13
14
use Magento \FunctionalTestingFramework \DataGenerator \Util \JsonObjectExtractor ;
14
15
use Magento \FunctionalTestingFramework \Util \ApiClientUtil ;
16
+ use Magento \Setup \Exception ;
15
17
16
18
/**
17
19
* Class ApiExecutor
18
20
*/
19
21
class ApiExecutor
20
22
{
21
23
const PRIMITIVE_TYPES = ['string ' , 'boolean ' , 'integer ' , 'double ' , 'array ' ];
24
+ const EXCEPTION_REQUIRED_DATA = "%s of key \" %s \" in \"%s \" is required by metadata, but was not provided. " ;
22
25
23
26
/**
24
27
* Describes the operation for the executor ('create','update','delete')
@@ -149,6 +152,7 @@ private function getAuthorizationHeader($authUrl)
149
152
* @param EntityDataObject $entityObject
150
153
* @param array $jsonArrayMetadata
151
154
* @return array
155
+ * @throws \Exception
152
156
*/
153
157
private function convertJsonArray ($ entityObject , $ jsonArrayMetadata )
154
158
{
@@ -169,20 +173,40 @@ private function convertJsonArray($entityObject, $jsonArrayMetadata)
169
173
EntityDataObject::CEST_UNIQUE_VALUE
170
174
);
171
175
172
- if (array_key_exists ($ jsonElement ->getKey (), $ entityObject ->getUniquenessData ())) {
173
- $ uniqueData = $ entityObject ->getUniquenessDataByName ($ jsonElement ->getKey ());
174
- if ($ uniqueData === 'suffix ' ) {
175
- $ elementData .= (string )self ::getSequence ($ entityObject ->getName ());
176
- } else {
177
- $ elementData = (string )self ::getSequence ($ entityObject ->getName ())
178
- . $ elementData ;
176
+ // If data was defined at all, attempt to put it into JSON body
177
+ // If data was not defined, and element is required, throw exception
178
+ // If no data is defined, don't input defaults per primitive into JSON for the data
179
+ if ($ elementData != null ) {
180
+ if (array_key_exists ($ jsonElement ->getKey (), $ entityObject ->getUniquenessData ())) {
181
+ $ uniqueData = $ entityObject ->getUniquenessDataByName ($ jsonElement ->getKey ());
182
+ if ($ uniqueData === 'suffix ' ) {
183
+ $ elementData .= (string )self ::getSequence ($ entityObject ->getName ());
184
+ } else {
185
+ $ elementData = (string )self ::getSequence ($ entityObject ->getName ()) . $ elementData ;
186
+ }
179
187
}
188
+ $ jsonArray [$ jsonElement ->getKey ()] = $ this ->castValue ($ jsonElementType , $ elementData );
189
+
190
+ } elseif ($ jsonElement ->getRequired ()) {
191
+ throw new \Exception (sprintf (
192
+ ApiExecutor::EXCEPTION_REQUIRED_DATA ,
193
+ $ jsonElement ->getType (),
194
+ $ jsonElement ->getKey (),
195
+ $ this ->entityObject ->getName ()
196
+ ));
180
197
}
181
-
182
- $ jsonArray [$ jsonElement ->getKey ()] = $ this ->castValue ($ jsonElementType , $ elementData );
183
198
} else {
184
199
$ entityNamesOfType = $ entityObject ->getLinkedEntitiesOfType ($ jsonElementType );
185
200
201
+ // If an element is required by metadata, but was not provided in the entity, throw an exception
202
+ if ($ jsonElement ->getRequired () && $ entityNamesOfType == null ) {
203
+ throw new \Exception (sprintf (
204
+ ApiExecutor::EXCEPTION_REQUIRED_DATA ,
205
+ $ jsonElement ->getType (),
206
+ $ jsonElement ->getKey (),
207
+ $ this ->entityObject ->getName ()
208
+ ));
209
+ }
186
210
foreach ($ entityNamesOfType as $ entityName ) {
187
211
$ jsonDataSubArray = $ this ->resolveNonPrimitiveElement ($ entityName , $ jsonElement );
188
212
@@ -210,7 +234,8 @@ private function resolveNonPrimitiveElement($entityName, $jsonElement)
210
234
$ linkedEntityObj = $ this ->resolveLinkedEntityObject ($ entityName );
211
235
212
236
if (!empty ($ jsonElement ->getNestedJsonElement ($ jsonElement ->getValue ()))
213
- && $ jsonElement ->getType () == 'array ' ) {
237
+ && $ jsonElement ->getType () == 'array '
238
+ ) {
214
239
$ jsonSubArray = $ this ->convertJsonArray (
215
240
$ linkedEntityObj ,
216
241
[$ jsonElement ->getNestedJsonElement ($ jsonElement ->getValue ())]
@@ -285,6 +310,7 @@ private static function getSequence($entityName)
285
310
}
286
311
287
312
// @codingStandardsIgnoreStart
313
+
288
314
/**
289
315
* This function takes a string value and its corresponding type and returns the string cast
290
316
* into its the type passed.
@@ -304,6 +330,9 @@ private function castValue($type, $value)
304
330
$ newVal = (integer )$ value ;
305
331
break ;
306
332
case 'boolean ' :
333
+ if (strtolower ($ newVal ) === 'false ' ) {
334
+ return false ;
335
+ }
307
336
$ newVal = (boolean )$ value ;
308
337
break ;
309
338
case 'double ' :
0 commit comments