Skip to content

Commit

Permalink
magento/graphql-ce#761: [Customizable Options] Call to a member funct…
Browse files Browse the repository at this point in the history
…ion format() on boolean
  • Loading branch information
lenaorobei committed Oct 14, 2019
1 parent 063950f commit 10d20bf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private function formatValues($values)
$value = $values[$this->getOption()->getId()];
$dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value);

if (!$dateTime) {
if ($dateTime === false) {
throw new GraphQlInputException(
__('Invalid format provided. Please use \'Y-m-d H:i:s\' format.')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class AddSimpleProductWithCustomOptionsToCartTest extends GraphQlAbstract
*/
private $getCustomOptionsValuesForQueryBySku;

/**
* @var GetEmptyOptionsValuesForQueryBySku
*/
private $getEmptyOptionsValuesForQueryBySku;

/**
* @inheritdoc
*/
Expand All @@ -45,7 +40,6 @@ protected function setUp()
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->productCustomOptionsRepository = $objectManager->get(ProductCustomOptionRepositoryInterface::class);
$this->getCustomOptionsValuesForQueryBySku = $objectManager->get(GetCustomOptionsValuesForQueryBySku::class);
$this->getEmptyOptionsValuesForQueryBySku = $objectManager->get(GetEmptyOptionsValuesForQueryBySku::class);
}

/**
Expand All @@ -63,7 +57,7 @@ public function testAddSimpleProductWithOptions()

$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
/* Generate customizable options fragment for GraphQl request */
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode(array_values($customOptionsValues)));

$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
Expand All @@ -74,13 +68,14 @@ public function testAddSimpleProductWithOptions()
self::assertCount(1, $response['addSimpleProductsToCart']['cart']);

$customizableOptionsOutput = $response['addSimpleProductsToCart']['cart']['items'][0]['customizable_options'];
$assignedOptionsCount = count($customOptionsValues);
for ($counter = 0; $counter < $assignedOptionsCount; $counter++) {
$expectedValues = $this->buildExpectedValuesArray($customOptionsValues[$counter]['value_string']);
$count = 0;
foreach ($customOptionsValues as $type => $value) {
$expectedValues = $this->buildExpectedValuesArray($value['value_string'], $type);
self::assertEquals(
$expectedValues,
$customizableOptionsOutput[$counter]['values']
$customizableOptionsOutput[$count]['values']
);
$count++;
}
}

Expand All @@ -106,54 +101,24 @@ public function testAddSimpleProductWithMissedRequiredOptionsSet()
}

/**
* Test adding a simple product to the shopping cart with Date customizable option assigned
* Test adding a simple product with wrong format value for date option
*
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
public function testAddSimpleProductWithDateOption()
{
$sku = 'simple-product-1';
$quantity = 1;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');

$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);

$response = $this->graphQlMutation($query);

self::assertArrayHasKey('items', $response['addSimpleProductsToCart']['cart']);
self::assertCount(1, $response['addSimpleProductsToCart']['cart']);

$cartItem = $response['addSimpleProductsToCart']['cart']['items'][0];
$customizableOptionOutput = $cartItem['customizable_options'][0]['values'][0]['value'];
$expectedValue = date("M d, Y", strtotime($customOptionsValues[0]['value_string']));

self::assertEquals($expectedValue, $customizableOptionOutput);
}

/**
* Test adding a simple product with empty values for date option
*
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
public function testAddSimpleProductWithMissedDateOptionsSet()
public function testAddSimpleProductWithWrongDateOptionFormat()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = 'simple-product-1';
$sku = 'simple';
$quantity = 1;

$customOptionsValues = $this->getEmptyOptionsValuesForQueryBySku->execute($sku);
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
$customOptionsValues['date']['value_string'] = '12-12-12';
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode(array_values($customOptionsValues)));
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);

self::expectExceptionMessage(
'Invalid format provided. Please use \'Y-m-d H:i:s\' format.'
);
$this->expectExceptionMessage('Invalid format provided. Please use \'Y-m-d H:i:s\' format.');

$this->graphQlMutation($query);
}
Expand Down Expand Up @@ -204,10 +169,14 @@ private function getQuery(string $maskedQuoteId, string $sku, float $quantity, s
* Build the part of expected response.
*
* @param string $assignedValue
* @param string $type option type
* @return array
*/
private function buildExpectedValuesArray(string $assignedValue) : array
private function buildExpectedValuesArray(string $assignedValue, string $type) : array
{
if ($type === 'date'){
return [['value' => date('M d, Y', strtotime($assignedValue))]];
}
$assignedOptionsArray = explode(',', trim($assignedValue, '[]'));
$expectedArray = [];
foreach ($assignedOptionsArray as $assignedOption) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,26 @@ public function execute(string $sku): array
foreach ($customOptions as $customOption) {
$optionType = $customOption->getType();
if ($optionType == 'date') {
$customOptionsValues[] = [
$customOptionsValues[$optionType] = [
'id' => (int)$customOption->getOptionId(),
'value_string' => '2012-12-12 00:00:00'
'value_string' => '2012-12-12 00:00:00',
];
} elseif ($optionType == 'field' || $optionType == 'area') {
$customOptionsValues[] = [
$customOptionsValues[$optionType] = [
'id' => (int)$customOption->getOptionId(),
'value_string' => 'test'
'value_string' => 'test',
];
} elseif ($optionType == 'drop_down') {
$optionSelectValues = $customOption->getValues();
$customOptionsValues[] = [
$customOptionsValues[$optionType] = [
'id' => (int)$customOption->getOptionId(),
'value_string' => reset($optionSelectValues)->getOptionTypeId()
'value_string' => reset($optionSelectValues)->getOptionTypeId(),
];
} elseif ($optionType == 'multiple') {
$customOptionsValues[] = [
$customOptionsValues[$optionType] = [
'id' => (int)$customOption->getOptionId(),
'value_string' => '[' . implode(',', array_keys($customOption->getValues())) . ']'
'value_string' => '[' . implode(',', array_keys($customOption->getValues())) . ']',

];
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@
'sort_order' => 2,
],
],
],
[
'title' => 'date option',
'type' => 'date',
'price' => 80.0,
'price_type' => 'fixed',
'sku' => 'date option sku',
'is_require' => false,
'sort_order' => 6
]
];

Expand Down

0 comments on commit 10d20bf

Please sign in to comment.