-
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-3474: Added API-functional tests coverage for setting shipping…
… method for cart #240 - Merge Pull Request magento/graphql-ce#240 from magento/graphql-ce:shipping-methods-tests - Merged commits: 1. 936a223 2. c3b4a80 3. c713b67 4. bae69d6 5. 5cadbd0
- Loading branch information
Showing
1 changed file
with
252 additions
and
0 deletions.
There are no files selected for viewing
252 changes: 252 additions & 0 deletions
252
dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingMethodOnCartTest.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,252 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Quote; | ||
|
||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; | ||
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
* Test for setting shipping methods on cart | ||
*/ | ||
class SetShippingMethodOnCartTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var CustomerTokenServiceInterface | ||
*/ | ||
private $customerTokenService; | ||
|
||
/** | ||
* @var QuoteResource | ||
*/ | ||
private $quoteResource; | ||
|
||
/** | ||
* @var Quote | ||
*/ | ||
private $quote; | ||
|
||
/** | ||
* @var QuoteIdToMaskedQuoteIdInterface | ||
*/ | ||
private $quoteIdToMaskedId; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$this->quoteResource = $objectManager->create(QuoteResource::class); | ||
$this->quote = $objectManager->create(Quote::class); | ||
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); | ||
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php | ||
*/ | ||
public function testSetShippingMethodOnCart() | ||
{ | ||
$shippingCarrierCode = 'flatrate'; | ||
$shippingMethodCode = 'flatrate'; | ||
$this->quoteResource->load( | ||
$this->quote, | ||
'test_order_1', | ||
'reserved_order_id' | ||
); | ||
$shippingAddress = $this->quote->getShippingAddress(); | ||
$shippingAddressId = $shippingAddress->getId(); | ||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); | ||
|
||
$query = $this->prepareMutationQuery( | ||
$maskedQuoteId, | ||
$shippingMethodCode, | ||
$shippingCarrierCode, | ||
$shippingAddressId | ||
); | ||
|
||
$response = $this->sendRequestWithToken($query); | ||
|
||
self::assertArrayHasKey('setShippingMethodsOnCart', $response); | ||
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); | ||
self::assertEquals($maskedQuoteId, $response['setShippingMethodsOnCart']['cart']['cart_id']); | ||
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['addresses']; | ||
self::assertCount(2, $addressesInformation); | ||
self::assertEquals( | ||
$addressesInformation[0]['selected_shipping_method']['code'], | ||
$shippingCarrierCode . '_' . $shippingMethodCode | ||
); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php | ||
*/ | ||
public function testSetShippingMethodWithWrongCartId() | ||
{ | ||
$shippingCarrierCode = 'flatrate'; | ||
$shippingMethodCode = 'flatrate'; | ||
$shippingAddressId = '1'; | ||
$maskedQuoteId = 'invalid'; | ||
|
||
$query = $this->prepareMutationQuery( | ||
$maskedQuoteId, | ||
$shippingMethodCode, | ||
$shippingCarrierCode, | ||
$shippingAddressId | ||
); | ||
|
||
self::expectExceptionMessage("Could not find a cart with ID \"$maskedQuoteId\""); | ||
$this->sendRequestWithToken($query); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php | ||
*/ | ||
public function testSetNonExistingShippingMethod() | ||
{ | ||
$shippingCarrierCode = 'non'; | ||
$shippingMethodCode = 'existing'; | ||
$this->quoteResource->load( | ||
$this->quote, | ||
'test_order_1', | ||
'reserved_order_id' | ||
); | ||
$shippingAddress = $this->quote->getShippingAddress(); | ||
$shippingAddressId = $shippingAddress->getId(); | ||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); | ||
|
||
$query = $this->prepareMutationQuery( | ||
$maskedQuoteId, | ||
$shippingMethodCode, | ||
$shippingCarrierCode, | ||
$shippingAddressId | ||
); | ||
|
||
self::expectExceptionMessage("Carrier with such method not found: $shippingCarrierCode, $shippingMethodCode"); | ||
$this->sendRequestWithToken($query); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php | ||
*/ | ||
public function testSetShippingMethodWithNonExistingAddress() | ||
{ | ||
$shippingCarrierCode = 'flatrate'; | ||
$shippingMethodCode = 'flatrate'; | ||
$this->quoteResource->load( | ||
$this->quote, | ||
'test_order_1', | ||
'reserved_order_id' | ||
); | ||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); | ||
$shippingAddressId = '-20'; | ||
|
||
$query = $this->prepareMutationQuery( | ||
$maskedQuoteId, | ||
$shippingMethodCode, | ||
$shippingCarrierCode, | ||
$shippingAddressId | ||
); | ||
|
||
self::expectExceptionMessage('The shipping address is missing. Set the address and try again.'); | ||
$this->sendRequestWithToken($query); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php | ||
*/ | ||
public function testSetShippingMethodByGuestToCustomerCart() | ||
{ | ||
$shippingCarrierCode = 'flatrate'; | ||
$shippingMethodCode = 'flatrate'; | ||
$this->quoteResource->load( | ||
$this->quote, | ||
'test_order_1', | ||
'reserved_order_id' | ||
); | ||
$shippingAddress = $this->quote->getShippingAddress(); | ||
$shippingAddressId = $shippingAddress->getId(); | ||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); | ||
|
||
$query = $this->prepareMutationQuery( | ||
$maskedQuoteId, | ||
$shippingMethodCode, | ||
$shippingCarrierCode, | ||
$shippingAddressId | ||
); | ||
|
||
self::expectExceptionMessage( | ||
"The current user cannot perform operations on cart \"$maskedQuoteId\"" | ||
); | ||
|
||
$this->graphQlQuery($query); | ||
} | ||
|
||
/** | ||
* Generates query for setting the specified shipping method on cart | ||
* | ||
* @param string $maskedQuoteId | ||
* @param string $shippingMethodCode | ||
* @param string $shippingCarrierCode | ||
* @param string $shippingAddressId | ||
* @return string | ||
*/ | ||
private function prepareMutationQuery( | ||
string $maskedQuoteId, | ||
string $shippingMethodCode, | ||
string $shippingCarrierCode, | ||
string $shippingAddressId | ||
) : string { | ||
return <<<QUERY | ||
mutation { | ||
setShippingMethodsOnCart(input: | ||
{ | ||
cart_id: "$maskedQuoteId", | ||
shipping_methods: [ | ||
{ | ||
shipping_method_code: "$shippingMethodCode" | ||
shipping_carrier_code: "$shippingCarrierCode" | ||
cart_address_id: $shippingAddressId | ||
} | ||
]}) { | ||
cart { | ||
cart_id, | ||
addresses { | ||
selected_shipping_method { | ||
code | ||
label | ||
} | ||
} | ||
} | ||
} | ||
} | ||
QUERY; | ||
} | ||
|
||
/** | ||
* Sends a GraphQL request with using a bearer token | ||
* | ||
* @param string $query | ||
* @return array | ||
* @throws \Magento\Framework\Exception\AuthenticationException | ||
*/ | ||
private function sendRequestWithToken(string $query): array | ||
{ | ||
|
||
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); | ||
$headerMap = ['Authorization' => 'Bearer ' . $customerToken]; | ||
|
||
return $this->graphQlQuery($query, [], '', $headerMap); | ||
} | ||
} |