Skip to content

Commit

Permalink
Covered with api-functional Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sedonik committed Sep 22, 2019
1 parent 6550d06 commit ab4e736
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class AddSimpleProductWithCustomOptionsToCartTest extends GraphQlAbstract
*/
private $getCustomOptionsValuesForQueryBySku;

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

/**
* @inheritdoc
*/
Expand All @@ -40,6 +45,7 @@ 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 Down Expand Up @@ -99,6 +105,58 @@ public function testAddSimpleProductWithMissedRequiredOptionsSet()
$this->graphQlMutation($query);
}

/**
* Test adding a simple product to the shopping cart with Date customizable option assigned
*
* @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']);

$customizableOptionOutput = $response['addSimpleProductsToCart']['cart']['items'][0]['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/Checkout/_files/active_quote.php
*/
public function testAddSimpleProductWithMissedDateOptionsSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = 'simple-product-1';
$quantity = 1;

$customOptionsValues = $this->getEmptyOptionsValuesForQueryBySku->execute($sku);
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($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->graphQlMutation($query);
}

/**
* @param string $maskedQuoteId
* @param string $sku
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public function execute(string $sku): array

foreach ($customOptions as $customOption) {
$optionType = $customOption->getType();
if ($optionType == 'field' || $optionType == 'area') {
if ($optionType == 'date') {
$customOptionsValues[] = [
'id' => (int)$customOption->getOptionId(),
'value_string' => '2012-12-12 00:00:00'
];
} elseif ($optionType == 'field' || $optionType == 'area') {
$customOptionsValues[] = [
'id' => (int)$customOption->getOptionId(),
'value_string' => 'test'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote;

use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;

/**
* Generate an array with test values for customizable options based on the option type
*/
class GetEmptyOptionsValuesForQueryBySku
{
/**
* @var ProductCustomOptionRepositoryInterface
*/
private $productCustomOptionRepository;

/**
* @param ProductCustomOptionRepositoryInterface $productCustomOptionRepository
*/
public function __construct(ProductCustomOptionRepositoryInterface $productCustomOptionRepository)
{
$this->productCustomOptionRepository = $productCustomOptionRepository;
}

/**
* Returns array of empty options for the product
*
* @param string $sku
* @return array
*/
public function execute(string $sku): array
{
$customOptions = $this->productCustomOptionRepository->getList($sku);
$customOptionsValues = [];

foreach ($customOptions as $customOption) {
$optionType = $customOption->getType();
if ($optionType == 'date') {
$customOptionsValues[] = [
'id' => (int)$customOption->getOptionId(),
'value_string' => ''
];
}
}

return $customOptionsValues;
}
}

0 comments on commit ab4e736

Please sign in to comment.