|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\SalesRule\Test\Unit\Model\ResourceModel; |
| 9 | + |
| 10 | +use Magento\Framework\App\ResourceConnection; |
| 11 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 12 | +use Magento\Framework\Model\ResourceModel\Db\Context; |
| 13 | +use Magento\Framework\Stdlib\DateTime; |
| 14 | +use Magento\SalesRule\Model\ResourceModel\Coupon; |
| 15 | +use Magento\SalesRule\Model\Rule; |
| 16 | +use PHPUnit\Framework\MockObject\Exception; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +class CouponTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var Context|MockObject |
| 24 | + */ |
| 25 | + private $contextMock; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var DateTime\DateTime|MockObject |
| 29 | + */ |
| 30 | + private $dateMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var DateTime|MockObject |
| 34 | + */ |
| 35 | + private $datetimeMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ResourceConnection|MockObject |
| 39 | + */ |
| 40 | + private $resourcesMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var AdapterInterface|MockObject |
| 44 | + */ |
| 45 | + private $connectionMock; |
| 46 | + /** |
| 47 | + * @var Coupon |
| 48 | + */ |
| 49 | + private $model; |
| 50 | + |
| 51 | + protected function setUp(): void |
| 52 | + { |
| 53 | + parent::setUp(); |
| 54 | + $this->contextMock = $this->createMock(Context::class); |
| 55 | + $this->dateMock = $this->createMock(DateTime\DateTime::class); |
| 56 | + $this->datetimeMock = $this->createMock(DateTime::class); |
| 57 | + $this->resourcesMock = $this->createMock(ResourceConnection::class); |
| 58 | + $this->connectionMock = $this->createMock(AdapterInterface::class); |
| 59 | + $this->contextMock->method('getResources') |
| 60 | + ->willReturn($this->resourcesMock); |
| 61 | + $this->resourcesMock->expects($this->any()) |
| 62 | + ->method('getConnection') |
| 63 | + ->willReturn($this->connectionMock); |
| 64 | + $this->model = new Coupon( |
| 65 | + $this->contextMock, |
| 66 | + $this->dateMock, |
| 67 | + $this->datetimeMock |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @dataProvider updateSpecificCouponsDataProvider |
| 73 | + * @param array $origData |
| 74 | + * @param array $data |
| 75 | + * @param array|null $update |
| 76 | + * @return void |
| 77 | + * @throws Exception |
| 78 | + */ |
| 79 | + public function testUpdateSpecificCoupons(array $origData, array $data, ?array $update = null): void |
| 80 | + { |
| 81 | + /** @var Rule|MockObject $abstractModel */ |
| 82 | + $ruleMock = $this->getMockBuilder(Rule::class) |
| 83 | + ->disableOriginalConstructor() |
| 84 | + ->onlyMethods(['save']) |
| 85 | + ->getMock(); |
| 86 | + |
| 87 | + $ruleMock->setData($origData); |
| 88 | + $ruleMock->setOrigData(); |
| 89 | + $ruleMock->addData($data); |
| 90 | + |
| 91 | + $this->resourcesMock->expects($this->any()) |
| 92 | + ->method('getTableName') |
| 93 | + ->willReturnArgument(0); |
| 94 | + |
| 95 | + if ($update) { |
| 96 | + $this->connectionMock->expects($this->once()) |
| 97 | + ->method('update') |
| 98 | + ->with( |
| 99 | + 'salesrule_coupon', |
| 100 | + $update, |
| 101 | + ['rule_id = ?' => $data['id']] |
| 102 | + ); |
| 103 | + } else { |
| 104 | + $this->connectionMock->expects($this->never()) |
| 105 | + ->method('update'); |
| 106 | + } |
| 107 | + $this->model->updateSpecificCoupons($ruleMock); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + public static function updateSpecificCouponsDataProvider(): array |
| 114 | + { |
| 115 | + return [ |
| 116 | + [ |
| 117 | + ['uses_per_coupon' => 1], |
| 118 | + ['uses_per_coupon' => 0], |
| 119 | + null |
| 120 | + ], |
| 121 | + [ |
| 122 | + ['uses_per_customer' => 1], |
| 123 | + ['uses_per_customer' => 0], |
| 124 | + null |
| 125 | + ], |
| 126 | + [ |
| 127 | + ['coupon_type' => Rule::COUPON_TYPE_SPECIFIC, 'uses_per_coupon' => 1, 'uses_per_customer' => 1], |
| 128 | + ['coupon_type' => Rule::COUPON_TYPE_AUTO, 'uses_per_coupon' => 0, 'uses_per_customer' => 0], |
| 129 | + null |
| 130 | + ], |
| 131 | + [ |
| 132 | + ['uses_per_coupon' => 1], |
| 133 | + ['id' => 1, 'uses_per_coupon' => 0], |
| 134 | + ['usage_limit' => 0], |
| 135 | + ], |
| 136 | + [ |
| 137 | + ['uses_per_customer' => 1], |
| 138 | + ['id' => 1, 'uses_per_customer' => 0], |
| 139 | + ['usage_per_customer' => 0], |
| 140 | + ], |
| 141 | + [ |
| 142 | + ['coupon_type' => Rule::COUPON_TYPE_SPECIFIC, 'uses_per_coupon' => 1, 'uses_per_customer' => 1], |
| 143 | + ['id' => 1, 'coupon_type' => Rule::COUPON_TYPE_AUTO, 'uses_per_coupon' => 1, 'uses_per_customer' => 1], |
| 144 | + ['usage_limit' => 1, 'usage_per_customer' => 1], |
| 145 | + ], |
| 146 | + [ |
| 147 | + ['uses_per_coupon' => 1, 'uses_per_customer' => 1], |
| 148 | + ['id' => 1, 'uses_per_coupon' => 0, 'uses_per_customer' => 0], |
| 149 | + ['usage_limit' => 0, 'usage_per_customer' => 0], |
| 150 | + ], |
| 151 | + [ |
| 152 | + ['uses_per_coupon' => 1, 'uses_per_customer' => 1], |
| 153 | + ['id' => 1, 'uses_per_coupon' => 0, 'uses_per_customer' => 1], |
| 154 | + ['usage_limit' => 0], |
| 155 | + ], |
| 156 | + [ |
| 157 | + ['uses_per_coupon' => 1, 'uses_per_customer' => 1], |
| 158 | + ['id' => 1, 'uses_per_coupon' => 1, 'uses_per_customer' => 0], |
| 159 | + ['usage_per_customer' => 0], |
| 160 | + ], |
| 161 | + ]; |
| 162 | + } |
| 163 | +} |
0 commit comments