Skip to content

Commit 0153d51

Browse files
committed
Added test coverage
1 parent bafb07d commit 0153d51

File tree

4 files changed

+118
-9
lines changed

4 files changed

+118
-9
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class SpecialPrice implements ResolverInterface
2424
*/
2525
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
2626
{
27-
/** @var ProductInterface $product */
28-
$product = $value['model'];
29-
3027
if (!isset($value['model'])) {
3128
throw new LocalizedException(__('"model" value should be specified'));
3229
}
3330

31+
/** @var ProductInterface $product */
32+
$product = $value['model'];
33+
3434
/** @var PricingSpecialPrice $specialPrice */
3535
$specialPrice = $product->getPriceInfo()->getPrice(PricingSpecialPrice::PRICE_CODE);
3636

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,73 @@ public function testProductWithCatalogDiscount()
832832
}
833833
}
834834

835+
/**
836+
* Check if the special price visible if the current date is in the date range set
837+
* for the special price
838+
*
839+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
840+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_special_price.php
841+
*/
842+
public function testSpecialPriceVisibleIfInDateRange()
843+
{
844+
$query = <<<QUERY
845+
{
846+
products(filter: {sku: {eq: "simple_product"}}) {
847+
items {
848+
price_range {
849+
minimum_price {
850+
regular_price {
851+
value
852+
}
853+
}
854+
}
855+
special_price
856+
}
857+
}
858+
}
859+
QUERY;
860+
$result = $this->graphQlQuery($query);
861+
$productInformation = $result['products']['items'][0];
862+
$productRegularPrice = $productInformation['price_range']['minimum_price']['regular_price']['value'];
863+
864+
self::assertEquals('10', $productRegularPrice);
865+
self::assertEquals('5.99', $productInformation['special_price']);
866+
}
867+
868+
869+
/**
870+
* Check if the special price is not visible if the current date is not in the date range set
871+
* for the special price
872+
*
873+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
874+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_special_price_future_date.php
875+
*/
876+
public function testSpecialPriceNotVisibleIfNotInDateRange()
877+
{
878+
$query = <<<QUERY
879+
{
880+
products(filter: {sku: {eq: "simple_product"}}) {
881+
items {
882+
price_range {
883+
minimum_price {
884+
regular_price {
885+
value
886+
}
887+
}
888+
}
889+
special_price
890+
}
891+
}
892+
}
893+
QUERY;
894+
$result = $this->graphQlQuery($query);
895+
$productInformation = $result['products']['items'][0];
896+
$productRegularPrice = $productInformation['price_range']['minimum_price']['regular_price']['value'];
897+
898+
self::assertEquals('10', $productRegularPrice);
899+
self::assertEquals(null, $productInformation['special_price']);
900+
}
901+
835902
/**
836903
* Get GraphQl query to fetch products by sku
837904
*
@@ -909,7 +976,7 @@ private function getQueryConfigurableProductAndVariants(array $sku): string
909976
name
910977
sku
911978
price_range {
912-
minimum_price {regular_price
979+
minimum_price {regular_price
913980
{
914981
value
915982
currency
@@ -949,13 +1016,13 @@ private function getQueryConfigurableProductAndVariants(array $sku): string
9491016
... on ConfigurableProduct{
9501017
variants{
9511018
product{
952-
1019+
9531020
sku
9541021
price_range {
9551022
minimum_price {regular_price {value}
9561023
final_price {
9571024
value
958-
1025+
9591026
}
9601027
discount {
9611028
amount_off
@@ -965,11 +1032,11 @@ private function getQueryConfigurableProductAndVariants(array $sku): string
9651032
maximum_price {
9661033
regular_price {
9671034
value
968-
1035+
9691036
}
9701037
final_price {
9711038
value
972-
1039+
9731040
}
9741041
discount {
9751042
amount_off
@@ -985,7 +1052,7 @@ private function getQueryConfigurableProductAndVariants(array $sku): string
9851052
final_price{value}
9861053
quantity
9871054
}
988-
1055+
9891056
}
9901057
}
9911058
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
$objectManager = Bootstrap::getObjectManager();
12+
/** @var ProductRepositoryInterface $productRepository */
13+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
14+
15+
$product = $productRepository->get('simple_product');
16+
$product->setSpecialPrice('5.99');
17+
18+
$product->setSpecialFromDate(date('Y-m-d', strtotime('-1 day')));
19+
$product->setSpecialToDate(date('Y-m-d', strtotime('+1 day')));
20+
21+
$productRepository->save($product);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
$objectManager = Bootstrap::getObjectManager();
12+
/** @var ProductRepositoryInterface $productRepository */
13+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
14+
15+
$product = $productRepository->get('simple_product');
16+
$product->setSpecialPrice('5.99');
17+
18+
$product->setSpecialFromDate(date('Y-m-d', strtotime('+3 day')));
19+
$product->setSpecialToDate(date('Y-m-d', strtotime('+5 day')));
20+
21+
$productRepository->save($product);

0 commit comments

Comments
 (0)