|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Meta\Conversion\Test\Unit\Block\Pixel; |
| 5 | + |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 8 | +use Meta\Conversion\Block\Pixel\InitiateCheckout; |
| 9 | +use Magento\Framework\Pricing\Helper\Data as PricingHelper; |
| 10 | +use Magento\Quote\Model\Quote; |
| 11 | +use Magento\Quote\Model\Quote\Item as QuoteItem; |
| 12 | +use Magento\Catalog\Model\Product; |
| 13 | +use Meta\Conversion\Helper\MagentoDataHelper; |
| 14 | +use Magento\Checkout\Model\Session as CheckoutSession; |
| 15 | +use Meta\Conversion\Model\CapiEventIdHandler; |
| 16 | +use Magento\Framework\View\Element\Template\Context; |
| 17 | +use Meta\BusinessExtension\Helper\FBEHelper; |
| 18 | +use Meta\BusinessExtension\Model\System\Config as SystemConfig; |
| 19 | +use Magento\Framework\Escaper; |
| 20 | + |
| 21 | +class InitiateCheckoutTest extends TestCase |
| 22 | +{ |
| 23 | + private $contextMock; |
| 24 | + private $fbeHelperMock; |
| 25 | + private $magentoDataHelperMock; |
| 26 | + private $systemConfigMock; |
| 27 | + private $escaperMock; |
| 28 | + private $checkoutSessionMock; |
| 29 | + private $quoteMock; |
| 30 | + private $quoteItemMock; |
| 31 | + private $productMock; |
| 32 | + private $pricingHelperMock; |
| 33 | + private $capiEventIdHandlerMock; |
| 34 | + private $subject; |
| 35 | + |
| 36 | + public function setUp(): void |
| 37 | + { |
| 38 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 39 | + ->disableOriginalConstructor() |
| 40 | + ->getMock(); |
| 41 | + $this->fbeHelperMock = $this->getMockBuilder(FBEHelper::class) |
| 42 | + ->disableOriginalConstructor() |
| 43 | + ->getMock(); |
| 44 | + $this->magentoDataHelperMock = $this->getMockBuilder(MagentoDataHelper::class) |
| 45 | + ->disableOriginalConstructor() |
| 46 | + ->getMock(); |
| 47 | + $this->systemConfigMock = $this->getMockBuilder(SystemConfig::class) |
| 48 | + ->disableOriginalConstructor() |
| 49 | + ->getMock(); |
| 50 | + $this->escaperMock = $this->getMockBuilder(Escaper::class) |
| 51 | + ->disableOriginalConstructor() |
| 52 | + ->getMock(); |
| 53 | + $this->checkoutSessionMock = $this->getMockBuilder(CheckoutSession::class) |
| 54 | + ->disableOriginalConstructor() |
| 55 | + ->getMock(); |
| 56 | + $this->quoteMock = $this->getMockBuilder(Quote::class) |
| 57 | + ->disableOriginalConstructor() |
| 58 | + ->getMock(); |
| 59 | + $this->quoteItemMock = $this->getMockBuilder(QuoteItem::class) |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->getMock(); |
| 62 | + $this->productMock = $this->getMockBuilder(Product::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->getMock(); |
| 65 | + $this->pricingHelperMock = $this->getMockBuilder(PricingHelper::class) |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->getMock(); |
| 68 | + $this->capiEventIdHandlerMock = $this->getMockBuilder(CapiEventIdHandler::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + |
| 72 | + $objectManager = new ObjectManager($this); |
| 73 | + $this->subject = $objectManager->getObject(InitiateCheckout::class, [ |
| 74 | + 'context' => $this->contextMock, |
| 75 | + 'fbeHelper' => $this->fbeHelperMock, |
| 76 | + 'magentoDataHelper' => $this->magentoDataHelperMock, |
| 77 | + 'systemConfig' => $this->systemConfigMock, |
| 78 | + 'escaper' => $this->escaperMock, |
| 79 | + 'checkoutSession' => $this->checkoutSessionMock, |
| 80 | + 'pricingHelper' => $this->pricingHelperMock, |
| 81 | + 'capiEventIdHandler' => $this->capiEventIdHandlerMock, |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + public function testGetEventToObserveName() |
| 86 | + { |
| 87 | + $this->assertEquals('facebook_businessextension_ssapi_initiate_checkout', $this->subject->getEventToObserveName()); |
| 88 | + } |
| 89 | + |
| 90 | + public function testGetContentTypeQuote() |
| 91 | + { |
| 92 | + $this->assertEquals('product', $this->subject->getContentTypeQuote()); |
| 93 | + } |
| 94 | + |
| 95 | + public function testGetEventId() |
| 96 | + { |
| 97 | + $eventId = 'sdfg-234e2-sfgd23-123ss'; |
| 98 | + $this->capiEventIdHandlerMock->expects($this->once()) |
| 99 | + ->method('getMetaEventId') |
| 100 | + ->with('facebook_businessextension_ssapi_initiate_checkout') |
| 101 | + ->willReturn($eventId); |
| 102 | + $this->assertEquals($eventId, $this->subject->getEventId()); |
| 103 | + } |
| 104 | + |
| 105 | + public function testGetContentIDs() |
| 106 | + { |
| 107 | + $productId = 1; |
| 108 | + $this->checkoutSessionMock->expects($this->once()) |
| 109 | + ->method('getQuote') |
| 110 | + ->willReturn($this->quoteMock); |
| 111 | + $this->quoteMock->expects($this->once()) |
| 112 | + ->method('getAllVisibleItems') |
| 113 | + ->willReturn([$this->quoteItemMock]); |
| 114 | + $this->quoteItemMock->expects($this->once()) |
| 115 | + ->method('getProduct') |
| 116 | + ->willReturn($this->productMock); |
| 117 | + $this->magentoDataHelperMock->expects($this->once()) |
| 118 | + ->method('getContentId') |
| 119 | + ->with($this->productMock) |
| 120 | + ->willReturn($productId); |
| 121 | + $this->assertEquals([$productId], $this->subject->getContentIDs()); |
| 122 | + } |
| 123 | + |
| 124 | + public function testGetValue() |
| 125 | + { |
| 126 | + $itemPrice = 10.00; |
| 127 | + $this->checkoutSessionMock->expects($this->once()) |
| 128 | + ->method('getQuote') |
| 129 | + ->willReturn($this->quoteMock); |
| 130 | + $this->magentoDataHelperMock->expects($this->once()) |
| 131 | + ->method('getCartTotal') |
| 132 | + ->with($this->quoteMock) |
| 133 | + ->willReturn($itemPrice); |
| 134 | + $this->assertEquals($itemPrice, $this->subject->getValue()); |
| 135 | + } |
| 136 | + |
| 137 | + public function testGetContents() |
| 138 | + { |
| 139 | + $productId = 1; |
| 140 | + $productQty = 1; |
| 141 | + $productPrice = 10.00; |
| 142 | + $contents = [ |
| 143 | + [ |
| 144 | + 'id' => $productId, |
| 145 | + 'quantity' => $productQty, |
| 146 | + 'item_price' => $productPrice |
| 147 | + ] |
| 148 | + ]; |
| 149 | + $this->checkoutSessionMock->expects($this->once()) |
| 150 | + ->method('getQuote') |
| 151 | + ->willReturn($this->quoteMock); |
| 152 | + $this->quoteMock->expects($this->once()) |
| 153 | + ->method('getAllVisibleItems') |
| 154 | + ->willReturn([$this->quoteItemMock]); |
| 155 | + $this->quoteItemMock->expects($this->once()) |
| 156 | + ->method('getProduct') |
| 157 | + ->willReturn($this->productMock); |
| 158 | + $this->productMock->expects($this->once()) |
| 159 | + ->method('getFinalPrice') |
| 160 | + ->willReturn($productPrice); |
| 161 | + $this->pricingHelperMock->expects($this->once()) |
| 162 | + ->method('currency') |
| 163 | + ->with($productPrice, false, false) |
| 164 | + ->willReturn($productPrice); |
| 165 | + $this->magentoDataHelperMock->expects($this->once()) |
| 166 | + ->method('getContentId') |
| 167 | + ->with($this->productMock) |
| 168 | + ->willReturn($productId); |
| 169 | + $this->quoteItemMock->expects($this->once()) |
| 170 | + ->method('getQty') |
| 171 | + ->willReturn($productQty); |
| 172 | + $this->assertEquals($contents, $this->subject->getContents()); |
| 173 | + } |
| 174 | + |
| 175 | + public function testGetNumItems() |
| 176 | + { |
| 177 | + $itemQty = 1; |
| 178 | + $this->checkoutSessionMock->expects($this->once()) |
| 179 | + ->method('getQuote') |
| 180 | + ->willReturn($this->quoteMock); |
| 181 | + $this->magentoDataHelperMock->expects($this->once()) |
| 182 | + ->method('getCartNumItems') |
| 183 | + ->with($this->quoteMock) |
| 184 | + ->willReturn($itemQty); |
| 185 | + $this->assertEquals($itemQty, $this->subject->getNumItems()); |
| 186 | + } |
| 187 | + |
| 188 | + public function testGetContentCategory() |
| 189 | + { |
| 190 | + $categoryName = 'Test Category'; |
| 191 | + $this->checkoutSessionMock->expects($this->once()) |
| 192 | + ->method('getQuote') |
| 193 | + ->willReturn($this->quoteMock); |
| 194 | + $this->quoteMock->expects($this->once()) |
| 195 | + ->method('getAllVisibleItems') |
| 196 | + ->willReturn([$this->quoteItemMock]); |
| 197 | + $this->quoteItemMock->expects($this->once()) |
| 198 | + ->method('getProduct') |
| 199 | + ->willReturn($this->productMock); |
| 200 | + $this->magentoDataHelperMock->expects($this->once()) |
| 201 | + ->method('getCategoriesForProduct') |
| 202 | + ->with($this->productMock) |
| 203 | + ->willReturn($categoryName); |
| 204 | + $this->assertEquals($categoryName, $this->subject->getContentCategory()); |
| 205 | + } |
| 206 | +} |
0 commit comments