This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#607: Added tests for creating empty cart by customers
- Loading branch information
Harniuk Bohdan
committed
Apr 16, 2019
1 parent
39cabae
commit 4dd5005
Showing
1 changed file
with
105 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ | |
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
use Magento\Quote\Api\GuestCartRepositoryInterface; | ||
use Magento\Framework\Math\Random as RandomDataGenerator; | ||
use Magento\Framework\Exception\AuthenticationException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
/** | ||
* Test for empty cart creation mutation for customer | ||
|
@@ -56,6 +60,11 @@ class CreateEmptyCartTest extends GraphQlAbstract | |
*/ | ||
private $maskedQuoteId; | ||
|
||
/** | ||
* @var RandomDataGenerator | ||
*/ | ||
private $randomDataGenerator; | ||
|
||
protected function setUp() | ||
{ | ||
$objectManager = Bootstrap::getObjectManager(); | ||
|
@@ -65,10 +74,14 @@ protected function setUp() | |
$this->quoteFactory = $objectManager->get(QuoteFactory::class); | ||
$this->maskedQuoteIdToQuoteId = $objectManager->get(MaskedQuoteIdToQuoteIdInterface::class); | ||
$this->quoteIdMaskFactory = $objectManager->get(QuoteIdMaskFactory::class); | ||
$this->randomDataGenerator = $objectManager->get(RandomDataGenerator::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* | ||
* @throws AuthenticationException | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function testCreateEmptyCart() | ||
{ | ||
|
@@ -86,6 +99,29 @@ public function testCreateEmptyCart() | |
self::assertEquals('default', $guestCart->getStore()->getCode()); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* | ||
* @throws LocalizedException | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function testCreateEmptyCartWithCartId() | ||
{ | ||
$uniqueHash = $this->randomDataGenerator->getUniqueHash(); | ||
|
||
$query = $this->getQueryWithCartId('cart_id : "' . $uniqueHash . '"'); | ||
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken()); | ||
|
||
self::assertArrayHasKey('createEmptyCart', $response); | ||
self::assertNotEmpty($response['createEmptyCart']); | ||
|
||
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']); | ||
$this->maskedQuoteId = $response['createEmptyCart']; | ||
|
||
self::assertNotNull($guestCart->getId()); | ||
self::assertEquals(1, $guestCart->getCustomer()->getId()); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Store/_files/second_store.php | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
|
@@ -110,6 +146,24 @@ public function testCreateEmptyCartWithNotDefaultStore() | |
self::assertEquals('fixture_second_store', $guestCart->getStore()->getCode()); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* @dataProvider dataProviderValidateCreateEmptyCartWithSpecifiedCartId | ||
* @param string $input | ||
* @param string $message | ||
* @throws \Exception | ||
*/ | ||
public function testValidateCreateEmptyCartWithSpecifiedCartId(string $input, string $message) | ||
{ | ||
$input = str_replace('provide_non_unique_id', $this->addEmptyCartWithCartId(), $input); | ||
$input = str_replace('provide_hash_with_prefix', $this->randomDataGenerator->getUniqueHash('prefix'), $input); | ||
|
||
$query = $this->getQueryWithCartId($input); | ||
|
||
$this->expectExceptionMessage($message); | ||
$this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken()); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
|
@@ -122,10 +176,28 @@ private function getQuery(): string | |
QUERY; | ||
} | ||
|
||
/** | ||
* @param string $input | ||
* @return string | ||
*/ | ||
private function getQueryWithCartId(string $input): string | ||
{ | ||
return <<<QUERY | ||
mutation { | ||
createEmptyCart( | ||
input : { | ||
{$input} | ||
} | ||
) | ||
} | ||
QUERY; | ||
} | ||
|
||
/** | ||
* @param string $username | ||
* @param string $password | ||
* @return array | ||
* @throws AuthenticationException | ||
*/ | ||
private function getHeaderMapWithCustomerToken( | ||
string $username = '[email protected]', | ||
|
@@ -151,4 +223,37 @@ public function tearDown() | |
} | ||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* Return masked id for created empty cart. | ||
* | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* @return mixed | ||
* @throws LocalizedException | ||
*/ | ||
private function addEmptyCartWithCartId() | ||
{ | ||
$uniqueHash = $this->randomDataGenerator->getUniqueHash(); | ||
$query = $this->getQueryWithCartId('cart_id : "' . $uniqueHash . '"'); | ||
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken()); | ||
|
||
return $response['createEmptyCart']; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function dataProviderValidateCreateEmptyCartWithSpecifiedCartId(): array | ||
{ | ||
return [ | ||
'cart_id_unique_checking' => [ | ||
'cart_id: "provide_non_unique_id"', | ||
'Specified parameter "cart_id" is non unique.' | ||
], | ||
'cart_id_length_checking' => [ | ||
'cart_id: "provide_hash_with_prefix"', | ||
'"cart_id" length have to be less than or equal to 32.' | ||
], | ||
]; | ||
} | ||
} |