33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \FunctionalTestingFramework \DataGenerator \Api ;
78
89use Magento \FunctionalTestingFramework \DataGenerator \Handlers \DataObjectHandler ;
1213use Magento \FunctionalTestingFramework \DataGenerator \Objects \JsonElement ;
1314use Magento \FunctionalTestingFramework \DataGenerator \Util \JsonObjectExtractor ;
1415use Magento \FunctionalTestingFramework \Util \ApiClientUtil ;
16+ use Magento \Setup \Exception ;
1517
1618/**
1719 * Class ApiExecutor
1820 */
1921class ApiExecutor
2022{
2123 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. " ;
2225
2326 /**
2427 * Describes the operation for the executor ('create','update','delete')
@@ -149,6 +152,7 @@ private function getAuthorizationHeader($authUrl)
149152 * @param EntityDataObject $entityObject
150153 * @param array $jsonArrayMetadata
151154 * @return array
155+ * @throws \Exception
152156 */
153157 private function convertJsonArray ($ entityObject , $ jsonArrayMetadata )
154158 {
@@ -169,20 +173,40 @@ private function convertJsonArray($entityObject, $jsonArrayMetadata)
169173 EntityDataObject::CEST_UNIQUE_VALUE
170174 );
171175
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+ }
179187 }
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+ ));
180197 }
181-
182- $ jsonArray [$ jsonElement ->getKey ()] = $ this ->castValue ($ jsonElementType , $ elementData );
183198 } else {
184199 $ entityNamesOfType = $ entityObject ->getLinkedEntitiesOfType ($ jsonElementType );
185200
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+ }
186210 foreach ($ entityNamesOfType as $ entityName ) {
187211 $ jsonDataSubArray = $ this ->resolveNonPrimitiveElement ($ entityName , $ jsonElement );
188212
@@ -210,7 +234,8 @@ private function resolveNonPrimitiveElement($entityName, $jsonElement)
210234 $ linkedEntityObj = $ this ->resolveLinkedEntityObject ($ entityName );
211235
212236 if (!empty ($ jsonElement ->getNestedJsonElement ($ jsonElement ->getValue ()))
213- && $ jsonElement ->getType () == 'array ' ) {
237+ && $ jsonElement ->getType () == 'array '
238+ ) {
214239 $ jsonSubArray = $ this ->convertJsonArray (
215240 $ linkedEntityObj ,
216241 [$ jsonElement ->getNestedJsonElement ($ jsonElement ->getValue ())]
@@ -285,6 +310,7 @@ private static function getSequence($entityName)
285310 }
286311
287312 // @codingStandardsIgnoreStart
313+
288314 /**
289315 * This function takes a string value and its corresponding type and returns the string cast
290316 * into its the type passed.
@@ -304,6 +330,9 @@ private function castValue($type, $value)
304330 $ newVal = (integer )$ value ;
305331 break ;
306332 case 'boolean ' :
333+ if (strtolower ($ newVal ) === 'false ' ) {
334+ return false ;
335+ }
307336 $ newVal = (boolean )$ value ;
308337 break ;
309338 case 'double ' :
0 commit comments