|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\SalesRule\Test\Unit\Observer; |
| 10 | + |
| 11 | +use Magento\Framework\Event; |
| 12 | +use Magento\Framework\Event\Observer; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 14 | +use Magento\Sales\Api\Data\OrderInterface; |
| 15 | +use Magento\SalesRule\Model\Coupon\UpdateCouponUsages; |
| 16 | +use Magento\SalesRule\Observer\AssignCouponDataAfterOrderCustomerAssignObserver; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit test for Magento\SalesRule\Observer\AssignCouponDataAfterOrderCustomerAssignObserver |
| 22 | + */ |
| 23 | +class AssignCouponDataAfterOrderCustomerAssignObserverTest extends TestCase |
| 24 | +{ |
| 25 | + /* |
| 26 | + * Stub event key order |
| 27 | + */ |
| 28 | + private const STUB_EVENT_KEY_ORDER = 'order'; |
| 29 | + |
| 30 | + /* |
| 31 | + * Stub customer ID |
| 32 | + */ |
| 33 | + private const STUB_CUSTOMER_ID = 1; |
| 34 | + |
| 35 | + /** |
| 36 | + * Testable Object |
| 37 | + * |
| 38 | + * @var AssignCouponDataAfterOrderCustomerAssignObserver |
| 39 | + */ |
| 40 | + private $observer; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ObjectManager |
| 44 | + */ |
| 45 | + private $objectManager; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Observer|MockObject |
| 49 | + */ |
| 50 | + private $observerMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var Event|MockObject |
| 54 | + */ |
| 55 | + private $eventMock; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var OrderInterface|MockObject |
| 59 | + */ |
| 60 | + private $orderMock; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var UpdateCouponUsages|MockObject |
| 64 | + */ |
| 65 | + private $updateCouponUsagesMock; |
| 66 | + |
| 67 | + protected function setUp(): void |
| 68 | + { |
| 69 | + $this->objectManager = new ObjectManager($this); |
| 70 | + $this->observerMock = $this->createMock(Observer::class); |
| 71 | + |
| 72 | + $this->eventMock = $this->getMockBuilder(Event::class) |
| 73 | + ->disableOriginalConstructor() |
| 74 | + ->setMethods(['getData']) |
| 75 | + ->getMock(); |
| 76 | + |
| 77 | + $this->orderMock = $this->getMockBuilder(OrderInterface::class) |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->getMock(); |
| 80 | + |
| 81 | + $this->updateCouponUsagesMock = $this->getMockBuilder(UpdateCouponUsages::class) |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->setMethods(['execute']) |
| 84 | + ->getMock(); |
| 85 | + |
| 86 | + $this->observer = $this->objectManager->getObject( |
| 87 | + AssignCouponDataAfterOrderCustomerAssignObserver::class, |
| 88 | + [ |
| 89 | + 'updateCouponUsages' => $this->updateCouponUsagesMock |
| 90 | + ] |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Test for execute(), covers test case for assign coupon data after order customer |
| 96 | + */ |
| 97 | + public function testExecuteAssignCouponData(): void |
| 98 | + { |
| 99 | + $this->observerMock |
| 100 | + ->expects($this->once()) |
| 101 | + ->method('getEvent') |
| 102 | + ->willReturn($this->eventMock); |
| 103 | + |
| 104 | + $this->eventMock |
| 105 | + ->expects($this->once()) |
| 106 | + ->method('getData') |
| 107 | + ->with(self::STUB_EVENT_KEY_ORDER) |
| 108 | + ->willReturn($this->orderMock); |
| 109 | + |
| 110 | + $this->orderMock |
| 111 | + ->expects($this->once()) |
| 112 | + ->method('getCustomerId') |
| 113 | + ->willReturn(self::STUB_CUSTOMER_ID); |
| 114 | + |
| 115 | + $this->updateCouponUsagesMock |
| 116 | + ->expects($this->once()) |
| 117 | + ->method('execute') |
| 118 | + ->with($this->orderMock, true); |
| 119 | + |
| 120 | + $this->observer->execute($this->observerMock); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Test for execute(), covers test case for assign coupon data after order customer with empty customer ID |
| 125 | + */ |
| 126 | + public function testExecuteAssignCouponDataWithEmptyCustomerId(): void |
| 127 | + { |
| 128 | + $this->observerMock |
| 129 | + ->expects($this->once()) |
| 130 | + ->method('getEvent') |
| 131 | + ->willReturn($this->eventMock); |
| 132 | + |
| 133 | + $this->eventMock |
| 134 | + ->expects($this->once()) |
| 135 | + ->method('getData') |
| 136 | + ->with(self::STUB_EVENT_KEY_ORDER) |
| 137 | + ->willReturn($this->orderMock); |
| 138 | + |
| 139 | + $this->orderMock |
| 140 | + ->expects($this->once()) |
| 141 | + ->method('getCustomerId') |
| 142 | + ->willReturn(null); |
| 143 | + |
| 144 | + $this->updateCouponUsagesMock |
| 145 | + ->expects($this->never()) |
| 146 | + ->method('execute'); |
| 147 | + |
| 148 | + $this->observer->execute($this->observerMock); |
| 149 | + } |
| 150 | +} |
0 commit comments