|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Test\Unit\Service; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Api\InsightsClient; |
| 6 | +use Algolia\AlgoliaSearch\Service\Insights\EventProcessor; |
| 7 | +use Magento\Sales\Model\Order\Item as OrderItem; |
| 8 | +use Magento\Store\Model\Store; |
| 9 | +use Magento\Store\Model\StoreManagerInterface; |
| 10 | +use Magento\Tax\Model\Config as TaxConfig; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +class EventProcessorTest extends TestCase |
| 14 | +{ |
| 15 | + protected ?TaxConfig $taxConfig; |
| 16 | + protected ?InsightsClient $client; |
| 17 | + protected ?string $userToken; |
| 18 | + protected ?string $authenticatedUserToken; |
| 19 | + protected ?StoreManagerInterface $storeManager; |
| 20 | + protected ?EventProcessor $eventProcessor; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + $this->client = $this->createMock(InsightsClient::class); |
| 25 | + $this->userToken = 'foo'; |
| 26 | + $this->authenticatedUserToken = 'authenticated-foo'; |
| 27 | + $this->storeManager = $this->createMock(StoreManagerInterface::class); |
| 28 | + |
| 29 | + $store = $this->createMock(Store::class); |
| 30 | + $store->method('getId')->willReturn(1); |
| 31 | + $this->storeManager->method('getStore')->willReturn($store); |
| 32 | + $this->taxConfig = $this->createMock(TaxConfig::class); |
| 33 | + |
| 34 | + $this->eventProcessor = new EventProcessorTestable( |
| 35 | + $this->taxConfig, |
| 36 | + $this->client, |
| 37 | + $this->userToken, |
| 38 | + $this->authenticatedUserToken, |
| 39 | + $this->storeManager |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @dataProvider orderItemsProvider |
| 45 | + */ |
| 46 | + public function testObjectDataForPurchase($priceIncludesTax, $orderItemsData, $expectedResult, $expectedTotalRevenue): void |
| 47 | + { |
| 48 | + $this->taxConfig->method('priceIncludesTax')->willReturn($priceIncludesTax); |
| 49 | + |
| 50 | + $orderItems = []; |
| 51 | + |
| 52 | + foreach ($orderItemsData as $orderItemData) { |
| 53 | + $orderItem = $this->getMockBuilder(OrderItem::class) |
| 54 | + ->disableOriginalConstructor() |
| 55 | + ->getMock(); |
| 56 | + |
| 57 | + foreach ($orderItemData as $method => $value){ |
| 58 | + $orderItem->method($method)->willReturn($value); |
| 59 | + } |
| 60 | + |
| 61 | + $orderItems[] = $orderItem; |
| 62 | + } |
| 63 | + |
| 64 | + $object = $this->eventProcessor->getObjectDataForPurchase($orderItems); |
| 65 | + $this->assertEquals($expectedResult, $object); |
| 66 | + |
| 67 | + $totalRevenue = $this->eventProcessor->getTotalRevenueForEvent($object); |
| 68 | + $this->assertEquals($expectedTotalRevenue, $totalRevenue); |
| 69 | + } |
| 70 | + |
| 71 | + public function orderItemsProvider(): array |
| 72 | + { |
| 73 | + return [ |
| 74 | + [ // One item |
| 75 | + 'priceIncludesTax' => true, |
| 76 | + 'orderItemsData' => [ |
| 77 | + [ |
| 78 | + 'getPrice' => 32.00, |
| 79 | + 'getPriceInclTax' => 32.00, |
| 80 | + 'getOriginalPrice' => 32.00, |
| 81 | + 'getDiscountAmount' => 0.00, |
| 82 | + 'getQtyOrdered' => 1, |
| 83 | + ] |
| 84 | + ], |
| 85 | + 'expectedResult' => [ |
| 86 | + [ |
| 87 | + 'price' => 32.00, |
| 88 | + 'discount' => 0.00, |
| 89 | + 'quantity' => 1, |
| 90 | + ] |
| 91 | + ], |
| 92 | + 'expectedTotalRevenue' => 32.00 |
| 93 | + ], |
| 94 | + [ // One item (tax excluded) |
| 95 | + 'priceIncludesTax' => false, |
| 96 | + 'orderItemsData' => [ |
| 97 | + [ |
| 98 | + 'getPrice' => 25.00, |
| 99 | + 'getPriceInclTax' => 32.00, |
| 100 | + 'getOriginalPrice' => 25.00, |
| 101 | + 'getDiscountAmount' => 0.00, |
| 102 | + 'getQtyOrdered' => 1, |
| 103 | + ] |
| 104 | + ], |
| 105 | + 'expectedResult' => [ |
| 106 | + [ |
| 107 | + 'price' => 25.00, |
| 108 | + 'discount' => 0.00, |
| 109 | + 'quantity' => 1, |
| 110 | + ] |
| 111 | + ], |
| 112 | + 'expectedTotalRevenue' => 25.00 |
| 113 | + ], |
| 114 | + [ // One item with discount |
| 115 | + 'priceIncludesTax' => true, |
| 116 | + 'orderItemsData' => [ |
| 117 | + [ |
| 118 | + 'getPrice' => 32.00, |
| 119 | + 'getPriceInclTax' => 32.00, |
| 120 | + 'getOriginalPrice' => 32.00, |
| 121 | + 'getDiscountAmount' => 7.00, |
| 122 | + 'getQtyOrdered' => 1, |
| 123 | + ] |
| 124 | + ], |
| 125 | + 'expectedResult' => [ |
| 126 | + [ |
| 127 | + 'price' => 25.00, |
| 128 | + 'discount' => 7.00, |
| 129 | + 'quantity' => 1, |
| 130 | + ] |
| 131 | + ], |
| 132 | + 'expectedTotalRevenue' => 25.00 |
| 133 | + ], |
| 134 | + [ // One item with discount (tax excluded) |
| 135 | + 'priceIncludesTax' => false, |
| 136 | + 'orderItemsData' => [ |
| 137 | + [ |
| 138 | + 'getPrice' => 25.00, |
| 139 | + 'getPriceInclTax' => 32.00, |
| 140 | + 'getOriginalPrice' => 25.00, |
| 141 | + 'getDiscountAmount' => 7.00, |
| 142 | + 'getQtyOrdered' => 1, |
| 143 | + ] |
| 144 | + ], |
| 145 | + 'expectedResult' => [ |
| 146 | + [ |
| 147 | + 'price' => 18.00, |
| 148 | + 'discount' => 7.00, |
| 149 | + 'quantity' => 1, |
| 150 | + ] |
| 151 | + ], |
| 152 | + 'expectedTotalRevenue' => 18.00 |
| 153 | + ], |
| 154 | + [ // Two items |
| 155 | + 'priceIncludesTax' => true, |
| 156 | + 'orderItemsData' => [ |
| 157 | + [ |
| 158 | + 'getPrice' => 32.00, |
| 159 | + 'getPriceInclTax' => 32.00, |
| 160 | + 'getOriginalPrice' => 32.00, |
| 161 | + 'getDiscountAmount' => 7.00, |
| 162 | + 'getQtyOrdered' => 1, |
| 163 | + ], |
| 164 | + [ |
| 165 | + 'getPrice' => 32.00, |
| 166 | + 'getPriceInclTax' => 32.00, |
| 167 | + 'getOriginalPrice' => 32.00, |
| 168 | + 'getDiscountAmount' => 0.00, |
| 169 | + 'getQtyOrdered' => 2, |
| 170 | + ], |
| 171 | + ], |
| 172 | + 'expectedResult' => [ |
| 173 | + [ |
| 174 | + 'price' => 25.00, |
| 175 | + 'discount' => 7.00, |
| 176 | + 'quantity' => 1, |
| 177 | + ], |
| 178 | + [ |
| 179 | + 'price' => 32.00, |
| 180 | + 'discount' => 0.00, |
| 181 | + 'quantity' => 2, |
| 182 | + ] |
| 183 | + ], |
| 184 | + 'expectedTotalRevenue' => 89.00 // 25 + 32*2 |
| 185 | + ], |
| 186 | + ]; |
| 187 | + } |
| 188 | +} |
0 commit comments