|
| 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\StoreManagerInterface; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class EventProcessorTest extends TestCase |
| 12 | +{ |
| 13 | + protected ?InsightsClient $client; |
| 14 | + protected ?string $userToken; |
| 15 | + protected ?string $authenticatedUserToken; |
| 16 | + protected ?StoreManagerInterface $storeManager; |
| 17 | + protected ?EventProcessor $eventProcessor; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + $this->client = $this->createMock(InsightsClient::class); |
| 22 | + $this->userToken = 'foo'; |
| 23 | + $this->authenticatedUserToken = 'authenticated-foo'; |
| 24 | + $this->storeManager = $this->createMock(StoreManagerInterface::class); |
| 25 | + |
| 26 | + $this->eventProcessor = new EventProcessorTestable( |
| 27 | + $this->client, |
| 28 | + $this->userToken, |
| 29 | + $this->authenticatedUserToken, |
| 30 | + $this->storeManager |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @dataProvider orderItemsProvider |
| 36 | + */ |
| 37 | + public function testObjectDataForPurchase($orderItemsData, $expectedResult, $expectedTotalRevenue): void |
| 38 | + { |
| 39 | + $orderItems = []; |
| 40 | + |
| 41 | + foreach ($orderItemsData as $orderItemData) { |
| 42 | + $orderItem = $this->getMockBuilder(OrderItem::class) |
| 43 | + ->disableOriginalConstructor() |
| 44 | + ->getMock(); |
| 45 | + |
| 46 | + foreach ($orderItemData as $method => $value){ |
| 47 | + $orderItem->method($method)->willReturn($value); |
| 48 | + } |
| 49 | + |
| 50 | + $orderItems[] = $orderItem; |
| 51 | + } |
| 52 | + |
| 53 | + $object = $this->eventProcessor->getObjectDataForPurchase($orderItems); |
| 54 | + $this->assertEquals($expectedResult, $object); |
| 55 | + |
| 56 | + $totalRevenue = $this->eventProcessor->getTotalRevenueForEvent($object); |
| 57 | + $this->assertEquals($expectedTotalRevenue, $totalRevenue); |
| 58 | + } |
| 59 | + |
| 60 | + public function orderItemsProvider(): array |
| 61 | + { |
| 62 | + return [ |
| 63 | + [ // One item |
| 64 | + 'orderItemsData' => [ |
| 65 | + [ |
| 66 | + 'getPriceInclTax' => 32.00, |
| 67 | + 'getOriginalPrice' => 32.00, |
| 68 | + 'getDiscountAmount' => 0.00, |
| 69 | + 'getQtyOrdered' => 1, |
| 70 | + ] |
| 71 | + ], |
| 72 | + 'expectedResult' => [ |
| 73 | + [ |
| 74 | + 'price' => 32.00, |
| 75 | + 'discount' => 0.00, |
| 76 | + 'quantity' => 1, |
| 77 | + ] |
| 78 | + ], |
| 79 | + 'expectedTotalRevenue' => 32.00 |
| 80 | + ], |
| 81 | + [ // One item with discount |
| 82 | + 'orderItemsData' => [ |
| 83 | + [ |
| 84 | + 'getPriceInclTax' => 32.00, |
| 85 | + 'getOriginalPrice' => 32.00, |
| 86 | + 'getDiscountAmount' => 7.00, |
| 87 | + 'getQtyOrdered' => 1, |
| 88 | + ] |
| 89 | + ], |
| 90 | + 'expectedResult' => [ |
| 91 | + [ |
| 92 | + 'price' => 25.00, |
| 93 | + 'discount' => 7.00, |
| 94 | + 'quantity' => 1, |
| 95 | + ] |
| 96 | + ], |
| 97 | + 'expectedTotalRevenue' => 25.00 |
| 98 | + ], |
| 99 | + [ // Two items |
| 100 | + 'orderItemsData' => [ |
| 101 | + [ |
| 102 | + 'getPriceInclTax' => 32.00, |
| 103 | + 'getOriginalPrice' => 32.00, |
| 104 | + 'getDiscountAmount' => 7.00, |
| 105 | + 'getQtyOrdered' => 1, |
| 106 | + ], |
| 107 | + [ |
| 108 | + 'getPriceInclTax' => 32.00, |
| 109 | + 'getOriginalPrice' => 32.00, |
| 110 | + 'getDiscountAmount' => 0.00, |
| 111 | + 'getQtyOrdered' => 2, |
| 112 | + ], |
| 113 | + ], |
| 114 | + 'expectedResult' => [ |
| 115 | + [ |
| 116 | + 'price' => 25.00, |
| 117 | + 'discount' => 7.00, |
| 118 | + 'quantity' => 1, |
| 119 | + ], |
| 120 | + [ |
| 121 | + 'price' => 32.00, |
| 122 | + 'discount' => 0.00, |
| 123 | + 'quantity' => 2, |
| 124 | + ] |
| 125 | + ], |
| 126 | + 'expectedTotalRevenue' => 89.00 // 25 + 32*2 |
| 127 | + ], |
| 128 | + ]; |
| 129 | + } |
| 130 | +} |
0 commit comments