-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-4612: Add API Test For Selected Shipping Method functionality #…
- Loading branch information
Showing
18 changed files
with
893 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
...api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Quote\Customer; | ||
|
||
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; | ||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
class GetSelectedShippingMethodTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var CustomerTokenServiceInterface | ||
*/ | ||
private $customerTokenService; | ||
|
||
/** | ||
* @var GetMaskedQuoteIdByReservedOrderId | ||
*/ | ||
private $getMaskedQuoteIdByReservedOrderId; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); | ||
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php | ||
*/ | ||
public function testGetSelectedShippingMethod() | ||
{ | ||
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); | ||
|
||
$query = $this->getQuery($maskedQuoteId); | ||
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); | ||
|
||
self::assertArrayHasKey('cart', $response); | ||
self::assertArrayHasKey('shipping_addresses', $response['cart']); | ||
self::assertCount(1, $response['cart']['shipping_addresses']); | ||
|
||
$shippingAddress = current($response['cart']['shipping_addresses']); | ||
self::assertArrayHasKey('selected_shipping_method', $shippingAddress); | ||
|
||
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); | ||
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']); | ||
|
||
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); | ||
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']); | ||
} | ||
|
||
/** | ||
* _security | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php | ||
*/ | ||
public function testGetSelectedShippingMethodFromGuestCart() | ||
{ | ||
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); | ||
$query = $this->getQuery($maskedQuoteId); | ||
|
||
$this->expectExceptionMessage( | ||
"The current user cannot perform operations on cart \"$maskedQuoteId\"" | ||
); | ||
$this->graphQlQuery($query, [], '', $this->getHeaderMap()); | ||
} | ||
|
||
/** | ||
* _security | ||
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php | ||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php | ||
*/ | ||
public function testGetSelectedShippingMethodFromAnotherCustomerCart() | ||
{ | ||
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); | ||
$query = $this->getQuery($maskedQuoteId); | ||
|
||
$this->expectExceptionMessage( | ||
"The current user cannot perform operations on cart \"$maskedQuoteId\"" | ||
); | ||
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com')); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php | ||
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php | ||
*/ | ||
public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet() | ||
{ | ||
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); | ||
$query = $this->getQuery($maskedQuoteId); | ||
|
||
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); | ||
|
||
self::assertArrayHasKey('cart', $response); | ||
self::assertArrayHasKey('shipping_addresses', $response['cart']); | ||
self::assertCount(1, $response['cart']['shipping_addresses']); | ||
|
||
$shippingAddress = current($response['cart']['shipping_addresses']); | ||
self::assertArrayHasKey('selected_shipping_method', $shippingAddress); | ||
|
||
self::assertNull($shippingAddress['selected_shipping_method']['carrier_code']); | ||
self::assertNull($shippingAddress['selected_shipping_method']['method_code']); | ||
self::assertNull($shippingAddress['selected_shipping_method']['label']); | ||
self::assertNull($shippingAddress['selected_shipping_method']['amount']); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" | ||
*/ | ||
public function testGetGetSelectedShippingMethodOfNonExistentCart() | ||
{ | ||
$maskedQuoteId = 'non_existent_masked_id'; | ||
$query = $this->getQuery($maskedQuoteId); | ||
|
||
$this->graphQlQuery($query, [], '', $this->getHeaderMap()); | ||
} | ||
|
||
/** | ||
* @param string $username | ||
* @param string $password | ||
* @return array | ||
*/ | ||
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array | ||
{ | ||
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); | ||
$headerMap = ['Authorization' => 'Bearer ' . $customerToken]; | ||
return $headerMap; | ||
} | ||
|
||
/** | ||
* @param string $maskedQuoteId | ||
* @return string | ||
*/ | ||
private function getQuery(string $maskedQuoteId): string | ||
{ | ||
return <<<QUERY | ||
{ | ||
cart(cart_id: "$maskedQuoteId") { | ||
shipping_addresses { | ||
selected_shipping_method { | ||
carrier_code | ||
method_code | ||
label | ||
amount | ||
} | ||
} | ||
} | ||
} | ||
QUERY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...functional/testsuite/Magento/GraphQl/Quote/GetQuoteShippingAddressIdByReservedQuoteId.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Quote\Model\ResourceModel\Quote as QuoteResource; | ||
use Magento\Quote\Model\QuoteFactory; | ||
|
||
/** | ||
* Get quote shipping address id by reserved order id | ||
*/ | ||
class GetQuoteShippingAddressIdByReservedQuoteId | ||
{ | ||
/** | ||
* @var QuoteFactory | ||
*/ | ||
private $quoteFactory; | ||
|
||
/** | ||
* @var QuoteResource | ||
*/ | ||
private $quoteResource; | ||
|
||
/** | ||
* @param QuoteFactory $quoteFactory | ||
* @param QuoteResource $quoteResource | ||
*/ | ||
public function __construct( | ||
QuoteFactory $quoteFactory, | ||
QuoteResource $quoteResource | ||
) { | ||
$this->quoteFactory = $quoteFactory; | ||
$this->quoteResource = $quoteResource; | ||
} | ||
|
||
/** | ||
* Get quote shipping address id by reserved order id | ||
* | ||
* @param string $reversedOrderId | ||
* @return int | ||
*/ | ||
public function execute(string $reversedOrderId): int | ||
{ | ||
$quote = $this->quoteFactory->create(); | ||
$this->quoteResource->load($quote, $reversedOrderId, 'reserved_order_id'); | ||
|
||
return (int)$quote->getShippingAddress()->getId(); | ||
} | ||
} |
Oops, something went wrong.