@@ -271,61 +271,105 @@ public function testCreateTwoProductsWithAllCustomOptionsAndRetrieveOrderItems()
271271 self ::assertEquals ('eq ' , $ filters [0 ]['condition_type ' ]);
272272
273273 foreach ($ orderItems ['items ' ] as $ item ) {
274- self ::assertArrayHasKey ('item_id ' , $ item );
275- self ::assertArrayHasKey ('order_id ' , $ item );
276- self ::assertArrayHasKey ('product_id ' , $ item );
277- self ::assertArrayHasKey ('sku ' , $ item );
278- self ::assertArrayHasKey ('name ' , $ item );
279- self ::assertArrayHasKey ('price ' , $ item );
280- self ::assertArrayHasKey ('qty_ordered ' , $ item );
281- self ::assertArrayHasKey ('product_type ' , $ item );
282- self ::assertArrayHasKey ('created_at ' , $ item );
283- self ::assertArrayHasKey ('updated_at ' , $ item );
284- self ::assertArrayHasKey ('product_option ' , $ item );
285- self ::assertArrayHasKey ('extension_attributes ' , $ item ['product_option ' ]);
286- self ::assertArrayHasKey ('custom_options ' , $ item ['product_option ' ]['extension_attributes ' ]);
287-
288- $ customOptions = $ item ['product_option ' ]['extension_attributes ' ]['custom_options ' ];
289- self ::assertIsArray ($ customOptions );
290- self ::assertCount (9 , $ customOptions , sprintf (
291- 'Expected exactly 9 custom options for item "%s", but found %d ' ,
292- $ item ['sku ' ],
293- count ($ customOptions )
294- ));
295-
296- foreach ($ customOptions as $ index => $ option ) {
297- self ::assertArrayHasKey (
298- 'option_id ' ,
299- $ option ,
300- sprintf ('Custom option at index %d for item "%s" is missing option_id ' , $ index , $ item ['sku ' ])
301- );
302- self ::assertArrayHasKey (
303- 'option_value ' ,
304- $ option ,
305- sprintf ('Custom option at index %d for item "%s" is missing option_value ' , $ index , $ item ['sku ' ])
306- );
307- self ::assertNotEmpty (
308- $ option ['option_id ' ],
309- sprintf ('Custom option at index %d for item "%s" has empty option_id ' , $ index , $ item ['sku ' ])
310- );
311- $ optionValue = $ option ['option_value ' ];
312- if (is_array ($ optionValue )) {
313- self ::assertNotEmpty (
314- $ optionValue ,
315- sprintf ('Custom option at index %d for item "%s" has empty option_value ' , $ index , $ item ['sku ' ])
316- );
317- } else {
318- self ::assertNotEmpty (
319- $ optionValue ,
320- sprintf ('Custom option at index %d for item "%s" has empty option_value ' , $ index , $ item ['sku ' ])
321- );
322- }
323- $ this ->validateCustomOptionValue (
324- $ optionValue ,
325- $ index ,
326- $ item ['sku ' ]
327- );
328- }
274+ $ this ->assertOrderItemStructure ($ item );
275+ $ this ->assertOrderItemCustomOptions ($ item );
276+ }
277+ }
278+
279+ /**
280+ * Assert order item has the required structure and fields
281+ *
282+ * @param array $item Order item data
283+ * @return void
284+ */
285+ private function assertOrderItemStructure (array $ item ): void
286+ {
287+ self ::assertArrayHasKey ('item_id ' , $ item );
288+ self ::assertArrayHasKey ('order_id ' , $ item );
289+ self ::assertArrayHasKey ('product_id ' , $ item );
290+ self ::assertArrayHasKey ('sku ' , $ item );
291+ self ::assertArrayHasKey ('name ' , $ item );
292+ self ::assertArrayHasKey ('price ' , $ item );
293+ self ::assertArrayHasKey ('qty_ordered ' , $ item );
294+ self ::assertArrayHasKey ('product_type ' , $ item );
295+ self ::assertArrayHasKey ('created_at ' , $ item );
296+ self ::assertArrayHasKey ('updated_at ' , $ item );
297+ self ::assertArrayHasKey ('product_option ' , $ item );
298+ self ::assertArrayHasKey ('extension_attributes ' , $ item ['product_option ' ]);
299+ self ::assertArrayHasKey ('custom_options ' , $ item ['product_option ' ]['extension_attributes ' ]);
300+ }
301+
302+ /**
303+ * Assert order item custom options are valid
304+ *
305+ * @param array $item Order item data containing custom options
306+ * @return void
307+ */
308+ private function assertOrderItemCustomOptions (array $ item ): void
309+ {
310+ $ customOptions = $ item ['product_option ' ]['extension_attributes ' ]['custom_options ' ];
311+ self ::assertIsArray ($ customOptions );
312+ self ::assertCount (9 , $ customOptions , sprintf (
313+ 'Expected exactly 9 custom options for item "%s", but found %d ' ,
314+ $ item ['sku ' ],
315+ count ($ customOptions )
316+ ));
317+
318+ foreach ($ customOptions as $ index => $ option ) {
319+ $ this ->assertCustomOptionStructure ($ option , $ index , $ item ['sku ' ]);
320+ $ this ->validateCustomOptionValue (
321+ $ option ['option_value ' ],
322+ $ index ,
323+ $ item ['sku ' ]
324+ );
325+ }
326+ }
327+
328+ /**
329+ * Assert custom option has required structure and valid data
330+ *
331+ * @param array $option Custom option data
332+ * @param int $index Option index in the array
333+ * @param string $sku Product SKU for error messages
334+ * @return void
335+ */
336+ private function assertCustomOptionStructure (array $ option , int $ index , string $ sku ): void
337+ {
338+ self ::assertArrayHasKey (
339+ 'option_id ' ,
340+ $ option ,
341+ sprintf ('Custom option at index %d for item "%s" is missing option_id ' , $ index , $ sku )
342+ );
343+ self ::assertArrayHasKey (
344+ 'option_value ' ,
345+ $ option ,
346+ sprintf ('Custom option at index %d for item "%s" is missing option_value ' , $ index , $ sku )
347+ );
348+ self ::assertNotEmpty (
349+ $ option ['option_id ' ],
350+ sprintf ('Custom option at index %d for item "%s" has empty option_id ' , $ index , $ sku )
351+ );
352+ self ::assertIsString (
353+ $ option ['option_id ' ],
354+ sprintf ('Custom option at index %d for item "%s" has non-string option_id ' , $ index , $ sku )
355+ );
356+ self ::assertMatchesRegularExpression (
357+ '/^\d+$/ ' ,
358+ $ option ['option_id ' ],
359+ sprintf ('Custom option at index %d for item "%s" has non-numeric option_id ' , $ index , $ sku )
360+ );
361+
362+ $ optionValue = $ option ['option_value ' ];
363+ if (is_array ($ optionValue )) {
364+ self ::assertNotEmpty (
365+ $ optionValue ,
366+ sprintf ('Custom option at index %d for item "%s" has empty option_value ' , $ index , $ sku )
367+ );
368+ } else {
369+ self ::assertNotEmpty (
370+ $ optionValue ,
371+ sprintf ('Custom option at index %d for item "%s" has empty option_value ' , $ index , $ sku )
372+ );
329373 }
330374 }
331375
0 commit comments