123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\Test\Unit\Helper;
- use Magento\Checkout\Helper\Data;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Store\Model\ScopeInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class DataTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Pricing\PriceCurrencyInterface
- */
- private $priceCurrency;
- /**
- * @var Data
- */
- private $helper;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $transportBuilder;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $translator;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $checkoutSession;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $scopeConfig;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $eventManager;
- /**
- * @inheritdoc
- */
- protected function setUp()
- {
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $className = \Magento\Checkout\Helper\Data::class;
- $arguments = $objectManagerHelper->getConstructArguments($className);
- /** @var \Magento\Framework\App\Helper\Context $context */
- $context = $arguments['context'];
- $this->translator = $arguments['inlineTranslation'];
- $this->eventManager = $context->getEventManager();
- $this->scopeConfig = $context->getScopeConfig();
- $this->scopeConfig->expects($this->any())
- ->method('getValue')
- ->willReturnMap(
- [
- [
- 'checkout/payment_failed/template',
- ScopeInterface::SCOPE_STORE,
- 8,
- 'fixture_email_template_payment_failed',
- ],
- [
- 'checkout/payment_failed/receiver',
- ScopeInterface::SCOPE_STORE,
- 8,
- 'sysadmin',
- ],
- [
- 'trans_email/ident_sysadmin/email',
- ScopeInterface::SCOPE_STORE,
- 8,
- 'sysadmin@example.com',
- ],
- [
- 'trans_email/ident_sysadmin/name',
- ScopeInterface::SCOPE_STORE,
- 8,
- 'System Administrator',
- ],
- [
- 'checkout/payment_failed/identity',
- ScopeInterface::SCOPE_STORE,
- 8,
- 'noreply@example.com',
- ],
- [
- 'carriers/ground/title',
- ScopeInterface::SCOPE_STORE,
- null,
- 'Ground Shipping',
- ],
- [
- 'payment/fixture-payment-method/title',
- ScopeInterface::SCOPE_STORE,
- null,
- 'Check Money Order',
- ],
- [
- 'checkout/options/onepage_checkout_enabled',
- ScopeInterface::SCOPE_STORE,
- null,
- 'One Page Checkout',
- ],
- ]
- );
- $this->checkoutSession = $arguments['checkoutSession'];
- $arguments['localeDate']->expects($this->any())
- ->method('formatDateTime')
- ->willReturn('Oct 02, 2013');
- $this->transportBuilder = $arguments['transportBuilder'];
- $this->priceCurrency = $arguments['priceCurrency'];
- $this->helper = $objectManagerHelper->getObject($className, $arguments);
- }
- /**
- * @return void
- */
- public function testSendPaymentFailedEmail()
- {
- $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->setMethods(['getId'])
- ->disableOriginalConstructor()
- ->getMock();
- $quoteMock->expects($this->any())->method('getId')->willReturn(1);
- $this->assertSame($this->helper, $this->helper->sendPaymentFailedEmail($quoteMock, 'test message'));
- }
- /**
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- public function testGetCheckout()
- {
- $this->assertEquals($this->checkoutSession, $this->helper->getCheckout());
- }
- public function testGetQuote()
- {
- $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
- $this->checkoutSession->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
- $this->assertEquals($quoteMock, $this->helper->getQuote());
- }
- public function testFormatPrice()
- {
- $price = 5.5;
- $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
- $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['formatPrice', '__wakeup']);
- $this->checkoutSession->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
- $quoteMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock));
- $this->priceCurrency->expects($this->once())->method('format')->will($this->returnValue('5.5'));
- $this->assertEquals('5.5', $this->helper->formatPrice($price));
- }
- public function testConvertPrice()
- {
- $price = 5.5;
- $this->priceCurrency->expects($this->once())->method('convertAndFormat')->willReturn($price);
- $this->assertEquals(5.5, $this->helper->convertPrice($price));
- }
- public function testCanOnepageCheckout()
- {
- $this->scopeConfig->expects($this->once())->method('isSetFlag')->with(
- 'checkout/options/onepage_checkout_enabled',
- 'store'
- )->will($this->returnValue(true));
- $this->assertTrue($this->helper->canOnepageCheckout());
- }
- public function testIsContextCheckout()
- {
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $context = $objectManagerHelper->getObject(
- \Magento\Framework\App\Helper\Context::class
- );
- $helper = $objectManagerHelper->getObject(
- \Magento\Checkout\Helper\Data::class,
- ['context' => $context]
- );
- $context->getRequest()->expects($this->once())->method('getParam')->with('context')->will(
- $this->returnValue('checkout')
- );
- $this->assertTrue($helper->isContextCheckout());
- }
- public function testIsCustomerMustBeLogged()
- {
- $this->scopeConfig->expects($this->once())->method('isSetFlag')->with(
- 'checkout/options/customer_must_be_logged',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- )->will($this->returnValue(true));
- $this->assertTrue($this->helper->isCustomerMustBeLogged());
- }
- public function testGetPriceInclTax()
- {
- $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getPriceInclTax']);
- $itemMock->expects($this->exactly(2))->method('getPriceInclTax')->will($this->returnValue(5.5));
- $this->assertEquals(5.5, $this->helper->getPriceInclTax($itemMock));
- }
- public function testGetPriceInclTaxWithoutTax()
- {
- $qty = 1;
- $taxAmount = 1;
- $discountTaxCompensation = 1;
- $rowTotal = 15;
- $roundPrice = 17;
- $expected = 17;
- $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
- $objectManagerHelper = new ObjectManager($this);
- $helper = $objectManagerHelper->getObject(
- \Magento\Checkout\Helper\Data::class,
- [
- 'storeManager' => $storeManager,
- 'priceCurrency' => $this->priceCurrency,
- ]
- );
- $itemMock = $this->createPartialMock(
- \Magento\Framework\DataObject::class,
- ['getPriceInclTax', 'getQty', 'getTaxAmount', 'getDiscountTaxCompensation', 'getRowTotal', 'getQtyOrdered']
- );
- $itemMock->expects($this->once())->method('getPriceInclTax')->will($this->returnValue(false));
- $itemMock->expects($this->exactly(2))->method('getQty')->will($this->returnValue($qty));
- $itemMock->expects($this->never())->method('getQtyOrdered');
- $itemMock->expects($this->once())->method('getTaxAmount')->will($this->returnValue($taxAmount));
- $itemMock->expects($this->once())
- ->method('getDiscountTaxCompensation')->will($this->returnValue($discountTaxCompensation));
- $itemMock->expects($this->once())->method('getRowTotal')->will($this->returnValue($rowTotal));
- $this->priceCurrency->expects($this->once())->method('round')->with($roundPrice)->willReturn($roundPrice);
- $this->assertEquals($expected, $helper->getPriceInclTax($itemMock));
- }
- public function testGetSubtotalInclTax()
- {
- $rowTotalInclTax = 5.5;
- $expected = 5.5;
- $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getRowTotalInclTax']);
- $itemMock->expects($this->exactly(2))->method('getRowTotalInclTax')->will($this->returnValue($rowTotalInclTax));
- $this->assertEquals($expected, $this->helper->getSubtotalInclTax($itemMock));
- }
- public function testGetSubtotalInclTaxNegative()
- {
- $taxAmount = 1;
- $discountTaxCompensation = 1;
- $rowTotal = 15;
- $expected = 17;
- $itemMock = $this->createPartialMock(
- \Magento\Framework\DataObject::class,
- ['getRowTotalInclTax', 'getTaxAmount', 'getDiscountTaxCompensation', 'getRowTotal']
- );
- $itemMock->expects($this->once())->method('getRowTotalInclTax')->will($this->returnValue(false));
- $itemMock->expects($this->once())->method('getTaxAmount')->will($this->returnValue($taxAmount));
- $itemMock->expects($this->once())
- ->method('getDiscountTaxCompensation')->will($this->returnValue($discountTaxCompensation));
- $itemMock->expects($this->once())->method('getRowTotal')->will($this->returnValue($rowTotal));
- $this->assertEquals($expected, $this->helper->getSubtotalInclTax($itemMock));
- }
- public function testGetBasePriceInclTaxWithoutQty()
- {
- $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
- $objectManagerHelper = new ObjectManager($this);
- $helper = $objectManagerHelper->getObject(
- \Magento\Checkout\Helper\Data::class,
- [
- 'storeManager' => $storeManager,
- 'priceCurrency' => $this->priceCurrency,
- ]
- );
- $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getQty']);
- $itemMock->expects($this->once())->method('getQty');
- $this->priceCurrency->expects($this->once())->method('round');
- $helper->getPriceInclTax($itemMock);
- }
- public function testGetBasePriceInclTax()
- {
- $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $helper = $objectManagerHelper->getObject(
- \Magento\Checkout\Helper\Data::class,
- [
- 'storeManager' => $storeManager,
- 'priceCurrency' => $this->priceCurrency,
- ]
- );
- $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getQty', 'getQtyOrdered']);
- $itemMock->expects($this->once())->method('getQty')->will($this->returnValue(false));
- $itemMock->expects($this->exactly(2))->method('getQtyOrdered')->will($this->returnValue(5.5));
- $this->priceCurrency->expects($this->once())->method('round');
- $helper->getBasePriceInclTax($itemMock);
- }
- public function testGetBaseSubtotalInclTax()
- {
- $itemMock = $this->createPartialMock(
- \Magento\Framework\DataObject::class,
- ['getBaseTaxAmount', 'getBaseDiscountTaxCompensation', 'getBaseRowTotal']
- );
- $itemMock->expects($this->once())->method('getBaseTaxAmount');
- $itemMock->expects($this->once())->method('getBaseDiscountTaxCompensation');
- $itemMock->expects($this->once())->method('getBaseRowTotal');
- $this->helper->getBaseSubtotalInclTax($itemMock);
- }
- public function testIsAllowedGuestCheckoutWithoutStore()
- {
- $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
- $store = null;
- $quoteMock->expects($this->once())->method('getStoreId')->will($this->returnValue(1));
- $this->scopeConfig->expects($this->once())
- ->method('isSetFlag')
- ->will($this->returnValue(true));
- $this->assertTrue($this->helper->isAllowedGuestCheckout($quoteMock, $store));
- }
- }
|