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

Commit

Permalink
Test coverage for registered customer
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed Mar 6, 2019
1 parent 52e982b commit 0f0637b
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Customer;

use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test getting cart totals for registered customer
*/
class CartTotalsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var QuoteResource
*/
private $quoteResource;

/**
* @var QuoteFactory
*/
private $quoteFactory;

/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedId;

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->create(QuoteResource::class);
$this->quoteFactory = $objectManager->create(QuoteFactory::class);
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
$this->customerTokenService = $objectManager->create(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_tax.php
*/
public function testGetCartTotalsForCustomerWithTaxApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_tax');

$query = <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
prices {
grand_total {
value,
currency
}
subtotal_including_tax {
value
currency
}
subtotal_excluding_tax {
value
currency
}
subtotal_with_discount_excluding_tax {
value
currency
}
applied_taxes {
label
amount {
value
currency
}
}
}
}
}
QUERY;
$response = $this->sendRequestWithToken($query);

self::assertArrayHasKey('prices', $response['cart']);
$pricesResponse = $response['cart']['prices'];
self::assertEquals(10, $pricesResponse['grand_total']['value']);
self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']);
self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']);
self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']);

$appliedTaxesResponse = $pricesResponse['applied_taxes'];

self::assertEquals('US-CA-*-Rate 1', $appliedTaxesResponse[0]['label']);
self::assertEquals(0.83, $appliedTaxesResponse[0]['amount']['value']);
self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']);
}

/**
* @param string $reversedQuoteId
* @return string
*/
private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');

return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}

/**
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ProductFactory;
use Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory as TaxClassCollectionFactory;
use Magento\Tax\Model\ClassModel as TaxClassModel;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
/** @var ProductFactory $productFactory */
$productFactory = $objectManager->create(ProductFactory::class);
$product = $productFactory->create();

$product
->setTypeId('simple')
->setId(1)
->setAttributeSetId(4)
->setWebsiteIds([1])
->setName('Simple Product')
->setSku('simple')
->setPrice(10)
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 1, 'qty' => 22, 'is_in_stock' => 1])
->setQty(22);


/** @var TaxClassCollectionFactory $taxClassCollectionFactory */
$taxClassCollectionFactory = $objectManager->create(TaxClassCollectionFactory::class);
$taxClassCollection = $taxClassCollectionFactory->create();

/** @var TaxClassModel $taxClass */
$taxClassCollection->addFieldToFilter('class_type', TaxClassModel::TAX_CLASS_TYPE_PRODUCT);
$taxClass = $taxClassCollection->getFirstItem();

$product->setCustomAttribute('tax_class_id', $taxClass->getClassId());
$productRepository->save($product);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

require __DIR__ . '/product_simple_rollback.php';
require __DIR__ . '/../../Tax/_files/tax_classes_rollback.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address;

require __DIR__ . '/../../Customer/_files/customer_with_tax_group.php';
require __DIR__ . '/../../Customer/_files/customer_address.php';
require __DIR__ . '/../../../Magento/Catalog/_files/product_simple_with_tax.php';

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var AddressRepositoryInterface $addressRepository */
$addressRepository = $objectManager->get(AddressRepositoryInterface::class);
$customerAddress = $addressRepository->getById(1);
$customerAddress->setRegionId(12); // Taxable region
$addressRepository->save($customerAddress);
/** @var CustomerRepositoryInterface $customerRepository */
$customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
$customer = $customerRepository->get('customer@example.com');

/** @var Address $quoteAddress */
$quoteAddress = $objectManager->create(Address::class);
$quoteAddress->importCustomerAddressData($customerAddress);

/** @var Quote $quote */
$quote = $objectManager->create(Quote::class);

$quote->setStoreId(
1
)->setIsActive(
true
)->setIsMultiShipping(
false
)->assignCustomer(
$customer
)->setShippingAddress(
$quoteAddress
)->setBillingAddress(
$quoteAddress
)->setCheckoutMethod(
'customer'
)->setReservedOrderId(
'test_order_tax'
)->addProduct(
$product
);

$quote->getShippingAddress()->setRegionId(12);

$quoteRepository = $objectManager->get(
\Magento\Quote\Api\CartRepositoryInterface::class
);

$quoteRepository->save($quote);

/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
->create();
$quoteIdMask->setQuoteId($quote->getId());
$quoteIdMask->setDataChanges(true);
$quoteIdMask->save();

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteIdMask;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;

require __DIR__ . '/../../Customer/_files/customer_with_tax_group_rollback.php';
require __DIR__ . '/../../../Magento/Catalog/_files/product_simple_with_tax_rollback.php';


/** @var $objectManager ObjectManager */
$objectManager = Bootstrap::getObjectManager();
$quote = $objectManager->create(Quote::class);
$quote->load('test_order_tax', 'reserved_order_id')->delete();

/** @var QuoteIdMask $quoteIdMask */
$quoteIdMask = $objectManager->create(QuoteIdMask::class);
$quoteIdMask->delete($quote->getId());
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

require __DIR__ . '/customer.php';

$customer->setGroupId(3); // 3 is a predefined retailer group
$customer->save();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

require __DIR__ . '/customer_rollback.php';

0 comments on commit 0f0637b

Please sign in to comment.