Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
#607: Added tests for creating empty cart by customers
Browse files Browse the repository at this point in the history
  • Loading branch information
Harniuk Bohdan committed Apr 16, 2019
1 parent 39cabae commit 4dd5005
Showing 1 changed file with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,6 +60,11 @@ class CreateEmptyCartTest extends GraphQlAbstract
*/
private $maskedQuoteId;

/**
* @var RandomDataGenerator
*/
private $randomDataGenerator;

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
Expand All @@ -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()
{
Expand All @@ -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
Expand All @@ -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
*/
Expand All @@ -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]',
Expand All @@ -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.'
],
];
}
}

0 comments on commit 4dd5005

Please sign in to comment.