AddPaymentWeeeItemTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Weee\Test\Unit\Observer;
  8. use Magento\Framework\Event;
  9. use Magento\Framework\Event\Observer;
  10. use Magento\Payment\Model\Cart;
  11. use Magento\Payment\Model\Cart\SalesModel\SalesModelInterface;
  12. use Magento\Quote\Model\Quote\Item;
  13. use Magento\Store\Api\Data\StoreInterface;
  14. use Magento\Store\Model\Store;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. use Magento\Weee\Helper\Data;
  17. use Magento\Weee\Observer\AddPaymentWeeeItem;
  18. use PHPUnit\Framework\TestCase;
  19. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  20. /**
  21. * Class AddPaymentWeeeItemTest
  22. */
  23. class AddPaymentWeeeItemTest extends TestCase
  24. {
  25. /**
  26. * Testable object
  27. *
  28. * @var AddPaymentWeeeItem
  29. */
  30. private $observer;
  31. /**
  32. * @var Data|MockObject
  33. */
  34. private $weeeHelperMock;
  35. /**
  36. * @var StoreManagerInterface|MockObject
  37. */
  38. private $storeManagerMock;
  39. /**
  40. * Set Up
  41. */
  42. protected function setUp()
  43. {
  44. $this->weeeHelperMock = $this->createMock(Data::class);
  45. $this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
  46. $this->observer = new AddPaymentWeeeItem(
  47. $this->weeeHelperMock,
  48. $this->storeManagerMock
  49. );
  50. }
  51. /**
  52. * Test execute
  53. *
  54. * @dataProvider dataProvider
  55. * @param bool $isEnabled
  56. * @param bool $includeInSubtotal
  57. * @return void
  58. */
  59. public function testExecute(bool $isEnabled, bool $includeInSubtotal): void
  60. {
  61. /** @var Observer|MockObject $observerMock */
  62. $observerMock = $this->createMock(Observer::class);
  63. $cartModelMock = $this->createMock(Cart::class);
  64. $salesModelMock = $this->createMock(SalesModelInterface::class);
  65. $itemMock = $this->createPartialMock(Item::class, ['getOriginalItem']);
  66. $originalItemMock = $this->createPartialMock(Item::class, ['getParentItem']);
  67. $parentItemMock = $this->createMock(Item::class);
  68. $eventMock = $this->getMockBuilder(Event::class)
  69. ->disableOriginalConstructor()
  70. ->setMethods(['getCart'])
  71. ->getMock();
  72. $asCustomItem = $this->prepareShouldBeAddedAsCustomItem($isEnabled, $includeInSubtotal);
  73. $toBeCalled = 1;
  74. if (!$asCustomItem) {
  75. $toBeCalled = 0;
  76. }
  77. $eventMock->expects($this->exactly($toBeCalled))
  78. ->method('getCart')
  79. ->willReturn($cartModelMock);
  80. $observerMock->expects($this->exactly($toBeCalled))
  81. ->method('getEvent')
  82. ->willReturn($eventMock);
  83. $itemMock->expects($this->exactly($toBeCalled))
  84. ->method('getOriginalItem')
  85. ->willReturn($originalItemMock);
  86. $originalItemMock->expects($this->exactly($toBeCalled))
  87. ->method('getParentItem')
  88. ->willReturn($parentItemMock);
  89. $salesModelMock->expects($this->exactly($toBeCalled))
  90. ->method('getAllItems')
  91. ->willReturn([$itemMock]);
  92. $cartModelMock->expects($this->exactly($toBeCalled))
  93. ->method('getSalesModel')
  94. ->willReturn($salesModelMock);
  95. $this->observer->execute($observerMock);
  96. }
  97. /**
  98. * @return array
  99. */
  100. public function dataProvider(): array
  101. {
  102. return [
  103. [true, false],
  104. [true, true],
  105. [false, true],
  106. [false, false],
  107. ];
  108. }
  109. /**
  110. * Prepare if FPT should be added to payment cart as custom item or not.
  111. *
  112. * @param bool $isEnabled
  113. * @param bool $includeInSubtotal
  114. * @return bool
  115. */
  116. private function prepareShouldBeAddedAsCustomItem(bool $isEnabled, bool $includeInSubtotal): bool
  117. {
  118. $storeMock = $this->getMockBuilder(StoreInterface::class)
  119. ->setMethods(['getId'])
  120. ->getMockForAbstractClass();
  121. $storeMock->expects($this->once())
  122. ->method('getId')
  123. ->willReturn(Store::DEFAULT_STORE_ID);
  124. $this->storeManagerMock->expects($this->once())
  125. ->method('getStore')
  126. ->willReturn($storeMock);
  127. $this->weeeHelperMock->expects($this->once())
  128. ->method('isEnabled')
  129. ->with(Store::DEFAULT_STORE_ID)
  130. ->willReturn($isEnabled);
  131. if ($isEnabled) {
  132. $this->weeeHelperMock->expects($this->once())
  133. ->method('includeInSubtotal')
  134. ->with(Store::DEFAULT_STORE_ID)
  135. ->willReturn($includeInSubtotal);
  136. }
  137. return $isEnabled && !$includeInSubtotal;
  138. }
  139. }