Skip to content

Commit ab4e736

Browse files
committed
Covered with api-functional Tests
1 parent 6550d06 commit ab4e736

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class AddSimpleProductWithCustomOptionsToCartTest extends GraphQlAbstract
3131
*/
3232
private $getCustomOptionsValuesForQueryBySku;
3333

34+
/**
35+
* @var GetEmptyOptionsValuesForQueryBySku
36+
*/
37+
private $getEmptyOptionsValuesForQueryBySku;
38+
3439
/**
3540
* @inheritdoc
3641
*/
@@ -40,6 +45,7 @@ protected function setUp()
4045
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
4146
$this->productCustomOptionsRepository = $objectManager->get(ProductCustomOptionRepositoryInterface::class);
4247
$this->getCustomOptionsValuesForQueryBySku = $objectManager->get(GetCustomOptionsValuesForQueryBySku::class);
48+
$this->getEmptyOptionsValuesForQueryBySku = $objectManager->get(GetEmptyOptionsValuesForQueryBySku::class);
4349
}
4450

4551
/**
@@ -99,6 +105,58 @@ public function testAddSimpleProductWithMissedRequiredOptionsSet()
99105
$this->graphQlMutation($query);
100106
}
101107

108+
/**
109+
* Test adding a simple product to the shopping cart with Date customizable option assigned
110+
*
111+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php
112+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
113+
*/
114+
public function testAddSimpleProductWithDateOption()
115+
{
116+
$sku = 'simple-product-1';
117+
$quantity = 1;
118+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
119+
120+
$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
121+
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
122+
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
123+
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
124+
125+
$response = $this->graphQlMutation($query);
126+
127+
self::assertArrayHasKey('items', $response['addSimpleProductsToCart']['cart']);
128+
self::assertCount(1, $response['addSimpleProductsToCart']['cart']);
129+
130+
$customizableOptionOutput = $response['addSimpleProductsToCart']['cart']['items'][0]['customizable_options'][0]['values'][0]['value'];
131+
$expectedValue = date("M d, Y", strtotime($customOptionsValues[0]['value_string']));
132+
133+
self::assertEquals($expectedValue, $customizableOptionOutput);
134+
}
135+
136+
/**
137+
* Test adding a simple product with empty values for date option
138+
*
139+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php
140+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
141+
*/
142+
public function testAddSimpleProductWithMissedDateOptionsSet()
143+
{
144+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
145+
$sku = 'simple-product-1';
146+
$quantity = 1;
147+
148+
$customOptionsValues = $this->getEmptyOptionsValuesForQueryBySku->execute($sku);
149+
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
150+
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
151+
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
152+
153+
self::expectExceptionMessage(
154+
'Invalid format provided. Please use \'Y-m-d H:i:s\' format.'
155+
);
156+
157+
$this->graphQlMutation($query);
158+
}
159+
102160
/**
103161
* @param string $maskedQuoteId
104162
* @param string $sku

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public function execute(string $sku): array
4040

4141
foreach ($customOptions as $customOption) {
4242
$optionType = $customOption->getType();
43-
if ($optionType == 'field' || $optionType == 'area') {
43+
if ($optionType == 'date') {
44+
$customOptionsValues[] = [
45+
'id' => (int)$customOption->getOptionId(),
46+
'value_string' => '2012-12-12 00:00:00'
47+
];
48+
} elseif ($optionType == 'field' || $optionType == 'area') {
4449
$customOptionsValues[] = [
4550
'id' => (int)$customOption->getOptionId(),
4651
'value_string' => 'test'
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote;
9+
10+
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
11+
12+
/**
13+
* Generate an array with test values for customizable options based on the option type
14+
*/
15+
class GetEmptyOptionsValuesForQueryBySku
16+
{
17+
/**
18+
* @var ProductCustomOptionRepositoryInterface
19+
*/
20+
private $productCustomOptionRepository;
21+
22+
/**
23+
* @param ProductCustomOptionRepositoryInterface $productCustomOptionRepository
24+
*/
25+
public function __construct(ProductCustomOptionRepositoryInterface $productCustomOptionRepository)
26+
{
27+
$this->productCustomOptionRepository = $productCustomOptionRepository;
28+
}
29+
30+
/**
31+
* Returns array of empty options for the product
32+
*
33+
* @param string $sku
34+
* @return array
35+
*/
36+
public function execute(string $sku): array
37+
{
38+
$customOptions = $this->productCustomOptionRepository->getList($sku);
39+
$customOptionsValues = [];
40+
41+
foreach ($customOptions as $customOption) {
42+
$optionType = $customOption->getType();
43+
if ($optionType == 'date') {
44+
$customOptionsValues[] = [
45+
'id' => (int)$customOption->getOptionId(),
46+
'value_string' => ''
47+
];
48+
}
49+
}
50+
51+
return $customOptionsValues;
52+
}
53+
}

0 commit comments

Comments
 (0)