|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Weee\Test\Unit\Plugin\ConfigurableProduct\Pricing; |
| 9 | + |
| 10 | +use Magento\Catalog\Pricing\Price\FinalPrice as CatalogFinalPrice; |
| 11 | +use Magento\ConfigurableProduct\Pricing\Price\FinalPriceResolver as ConfigurableProductFinalPriceResolver; |
| 12 | +use Magento\Framework\Pricing\Amount\AmountInterface; |
| 13 | +use Magento\Framework\Pricing\PriceInfo\Base as PriceInfo; |
| 14 | +use Magento\Framework\Pricing\SaleableInterface; |
| 15 | +use Magento\Tax\Pricing\Adjustment; |
| 16 | +use Magento\Weee\Helper\Data as WeeeHelperData; |
| 17 | +use Magento\Weee\Plugin\ConfigurableProduct\Pricing\FinalPriceResolver; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Unit test for FinalPriceResolver plugin |
| 23 | + */ |
| 24 | +class FinalPriceResolverTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var FinalPriceResolver |
| 28 | + */ |
| 29 | + private FinalPriceResolver $plugin; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var WeeeHelperData|MockObject |
| 33 | + */ |
| 34 | + private WeeeHelperData|MockObject $weeeHelperDataMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ConfigurableProductFinalPriceResolver|MockObject |
| 38 | + */ |
| 39 | + private ConfigurableProductFinalPriceResolver|MockObject $subjectMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var SaleableInterface|MockObject |
| 43 | + */ |
| 44 | + private SaleableInterface|MockObject $productMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var PriceInfo|MockObject |
| 48 | + */ |
| 49 | + private PriceInfo|MockObject $priceInfoMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var CatalogFinalPrice|MockObject |
| 53 | + */ |
| 54 | + private CatalogFinalPrice|MockObject $finalPriceMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var AmountInterface|MockObject |
| 58 | + */ |
| 59 | + private AmountInterface|MockObject $amountMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @inheritdoc |
| 63 | + */ |
| 64 | + protected function setUp(): void |
| 65 | + { |
| 66 | + $this->weeeHelperDataMock = $this->createMock(WeeeHelperData::class); |
| 67 | + $this->subjectMock = $this->createMock(ConfigurableProductFinalPriceResolver::class); |
| 68 | + $this->productMock = $this->getMockForAbstractClass(SaleableInterface::class); |
| 69 | + $this->priceInfoMock = $this->createMock(PriceInfo::class); |
| 70 | + $this->finalPriceMock = $this->createMock(CatalogFinalPrice::class); |
| 71 | + $this->amountMock = $this->createMock(AmountInterface::class); |
| 72 | + |
| 73 | + $this->plugin = new FinalPriceResolver($this->weeeHelperDataMock); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Test afterResolvePrice when WEEE display is enabled via isDisplayIncl |
| 78 | + * |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + public function testAfterResolvePriceWhenWeeeDisplayIsEnabledViaDisplayIncl(): void |
| 82 | + { |
| 83 | + $originalResult = 100.0; |
| 84 | + $expectedPrice = 119.0; |
| 85 | + |
| 86 | + // Mock WEEE display settings - isDisplayIncl returns true |
| 87 | + $this->weeeHelperDataMock->expects($this->once()) |
| 88 | + ->method('isDisplayIncl') |
| 89 | + ->willReturn(true); |
| 90 | + |
| 91 | + // Should not call isDisplayInclDesc since isDisplayIncl is already true |
| 92 | + $this->weeeHelperDataMock->expects($this->never()) |
| 93 | + ->method('isDisplayInclDesc'); |
| 94 | + |
| 95 | + // Mock product price info chain |
| 96 | + $this->productMock->expects($this->once()) |
| 97 | + ->method('getPriceInfo') |
| 98 | + ->willReturn($this->priceInfoMock); |
| 99 | + |
| 100 | + $this->priceInfoMock->expects($this->once()) |
| 101 | + ->method('getPrice') |
| 102 | + ->with(CatalogFinalPrice::PRICE_CODE) |
| 103 | + ->willReturn($this->finalPriceMock); |
| 104 | + |
| 105 | + $this->finalPriceMock->expects($this->once()) |
| 106 | + ->method('getAmount') |
| 107 | + ->willReturn($this->amountMock); |
| 108 | + |
| 109 | + $this->amountMock->expects($this->once()) |
| 110 | + ->method('getValue') |
| 111 | + ->with(Adjustment::ADJUSTMENT_CODE) |
| 112 | + ->willReturn($expectedPrice); |
| 113 | + |
| 114 | + // Execute |
| 115 | + $result = $this->plugin->afterResolvePrice( |
| 116 | + $this->subjectMock, |
| 117 | + $originalResult, |
| 118 | + $this->productMock |
| 119 | + ); |
| 120 | + |
| 121 | + // Assert |
| 122 | + $this->assertEquals($expectedPrice, $result); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test afterResolvePrice when WEEE display is enabled via isDisplayInclDesc |
| 127 | + * |
| 128 | + * @return void |
| 129 | + */ |
| 130 | + public function testAfterResolvePriceWhenWeeeDisplayIsEnabledViaDisplayInclDesc(): void |
| 131 | + { |
| 132 | + $originalResult = 100.0; |
| 133 | + $expectedPrice = 119.0; |
| 134 | + |
| 135 | + // Mock WEEE display settings - isDisplayIncl returns false, isDisplayInclDesc returns true |
| 136 | + $this->weeeHelperDataMock->expects($this->once()) |
| 137 | + ->method('isDisplayIncl') |
| 138 | + ->willReturn(false); |
| 139 | + |
| 140 | + $this->weeeHelperDataMock->expects($this->once()) |
| 141 | + ->method('isDisplayInclDesc') |
| 142 | + ->willReturn(true); |
| 143 | + |
| 144 | + // Mock product price info chain |
| 145 | + $this->productMock->expects($this->once()) |
| 146 | + ->method('getPriceInfo') |
| 147 | + ->willReturn($this->priceInfoMock); |
| 148 | + |
| 149 | + $this->priceInfoMock->expects($this->once()) |
| 150 | + ->method('getPrice') |
| 151 | + ->with(CatalogFinalPrice::PRICE_CODE) |
| 152 | + ->willReturn($this->finalPriceMock); |
| 153 | + |
| 154 | + $this->finalPriceMock->expects($this->once()) |
| 155 | + ->method('getAmount') |
| 156 | + ->willReturn($this->amountMock); |
| 157 | + |
| 158 | + $this->amountMock->expects($this->once()) |
| 159 | + ->method('getValue') |
| 160 | + ->with(Adjustment::ADJUSTMENT_CODE) |
| 161 | + ->willReturn($expectedPrice); |
| 162 | + |
| 163 | + // Execute |
| 164 | + $result = $this->plugin->afterResolvePrice( |
| 165 | + $this->subjectMock, |
| 166 | + $originalResult, |
| 167 | + $this->productMock |
| 168 | + ); |
| 169 | + |
| 170 | + // Assert |
| 171 | + $this->assertEquals($expectedPrice, $result); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Test afterResolvePrice when WEEE display is disabled |
| 176 | + * |
| 177 | + * @return void |
| 178 | + */ |
| 179 | + public function testAfterResolvePriceWhenWeeeDisplayIsDisabled(): void |
| 180 | + { |
| 181 | + $originalResult = 100.0; |
| 182 | + $expectedPrice = 100.0; |
| 183 | + |
| 184 | + // Mock WEEE display settings - both return false |
| 185 | + $this->weeeHelperDataMock->expects($this->once()) |
| 186 | + ->method('isDisplayIncl') |
| 187 | + ->willReturn(false); |
| 188 | + |
| 189 | + $this->weeeHelperDataMock->expects($this->once()) |
| 190 | + ->method('isDisplayInclDesc') |
| 191 | + ->willReturn(false); |
| 192 | + |
| 193 | + // Mock product price info chain |
| 194 | + $this->productMock->expects($this->once()) |
| 195 | + ->method('getPriceInfo') |
| 196 | + ->willReturn($this->priceInfoMock); |
| 197 | + |
| 198 | + $this->priceInfoMock->expects($this->once()) |
| 199 | + ->method('getPrice') |
| 200 | + ->with(CatalogFinalPrice::PRICE_CODE) |
| 201 | + ->willReturn($this->finalPriceMock); |
| 202 | + |
| 203 | + // Should call getValue() without any argument (not getValue(Adjustment::ADJUSTMENT_CODE)) |
| 204 | + $this->finalPriceMock->expects($this->once()) |
| 205 | + ->method('getValue') |
| 206 | + ->willReturn($expectedPrice); |
| 207 | + |
| 208 | + // Execute |
| 209 | + $result = $this->plugin->afterResolvePrice( |
| 210 | + $this->subjectMock, |
| 211 | + $originalResult, |
| 212 | + $this->productMock |
| 213 | + ); |
| 214 | + |
| 215 | + // Assert |
| 216 | + $this->assertEquals($expectedPrice, $result); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Test afterResolvePrice returns correct price when tax adjustment is excluded |
| 221 | + * |
| 222 | + * @return void |
| 223 | + */ |
| 224 | + public function testAfterResolvePriceReturnsPriceWithoutTaxAdjustment(): void |
| 225 | + { |
| 226 | + $originalResult = 141.61; // Double-taxed price (incorrect) |
| 227 | + $priceWithoutTaxAdjustment = 100.0; // Base price without tax adjustment |
| 228 | + |
| 229 | + // Mock WEEE display enabled |
| 230 | + $this->weeeHelperDataMock->expects($this->once()) |
| 231 | + ->method('isDisplayIncl') |
| 232 | + ->willReturn(true); |
| 233 | + |
| 234 | + // Mock product price info chain |
| 235 | + $this->productMock->expects($this->once()) |
| 236 | + ->method('getPriceInfo') |
| 237 | + ->willReturn($this->priceInfoMock); |
| 238 | + |
| 239 | + $this->priceInfoMock->expects($this->once()) |
| 240 | + ->method('getPrice') |
| 241 | + ->with(CatalogFinalPrice::PRICE_CODE) |
| 242 | + ->willReturn($this->finalPriceMock); |
| 243 | + |
| 244 | + $this->finalPriceMock->expects($this->once()) |
| 245 | + ->method('getAmount') |
| 246 | + ->willReturn($this->amountMock); |
| 247 | + |
| 248 | + $this->amountMock->expects($this->once()) |
| 249 | + ->method('getValue') |
| 250 | + ->with(Adjustment::ADJUSTMENT_CODE) |
| 251 | + ->willReturn($priceWithoutTaxAdjustment); |
| 252 | + |
| 253 | + // Execute |
| 254 | + $result = $this->plugin->afterResolvePrice( |
| 255 | + $this->subjectMock, |
| 256 | + $originalResult, |
| 257 | + $this->productMock |
| 258 | + ); |
| 259 | + |
| 260 | + // Assert - should return the price without tax adjustment, not the original result |
| 261 | + $this->assertEquals($priceWithoutTaxAdjustment, $result); |
| 262 | + $this->assertNotEquals($originalResult, $result); |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Test that original result parameter is not used when WEEE is enabled |
| 267 | + * |
| 268 | + * Verifies that the plugin completely overrides the result from the subject |
| 269 | + * |
| 270 | + * @return void |
| 271 | + */ |
| 272 | + public function testOriginalResultIsIgnoredWhenWeeeEnabled(): void |
| 273 | + { |
| 274 | + $originalResult = 999.99; |
| 275 | + $actualPrice = 119.0; |
| 276 | + |
| 277 | + $this->weeeHelperDataMock->method('isDisplayIncl')->willReturn(true); |
| 278 | + |
| 279 | + $this->productMock->method('getPriceInfo')->willReturn($this->priceInfoMock); |
| 280 | + $this->priceInfoMock->method('getPrice')->willReturn($this->finalPriceMock); |
| 281 | + $this->finalPriceMock->method('getAmount')->willReturn($this->amountMock); |
| 282 | + $this->amountMock->method('getValue')->willReturn($actualPrice); |
| 283 | + |
| 284 | + $result = $this->plugin->afterResolvePrice( |
| 285 | + $this->subjectMock, |
| 286 | + $originalResult, |
| 287 | + $this->productMock |
| 288 | + ); |
| 289 | + |
| 290 | + // The result should be the actual price from product, not the original result |
| 291 | + $this->assertEquals($actualPrice, $result); |
| 292 | + $this->assertNotEquals($originalResult, $result); |
| 293 | + } |
| 294 | + |
| 295 | + /** |
| 296 | + * Test that original result parameter is not used when WEEE is disabled |
| 297 | + * |
| 298 | + * Verifies that the plugin completely overrides the result from the subject |
| 299 | + * |
| 300 | + * @return void |
| 301 | + */ |
| 302 | + public function testOriginalResultIsIgnoredWhenWeeeDisabled(): void |
| 303 | + { |
| 304 | + $originalResult = 999.99; |
| 305 | + $actualPrice = 100.0; |
| 306 | + |
| 307 | + $this->weeeHelperDataMock->method('isDisplayIncl')->willReturn(false); |
| 308 | + $this->weeeHelperDataMock->method('isDisplayInclDesc')->willReturn(false); |
| 309 | + |
| 310 | + $this->productMock->method('getPriceInfo')->willReturn($this->priceInfoMock); |
| 311 | + $this->priceInfoMock->method('getPrice')->willReturn($this->finalPriceMock); |
| 312 | + $this->finalPriceMock->method('getValue')->willReturn($actualPrice); |
| 313 | + |
| 314 | + $result = $this->plugin->afterResolvePrice( |
| 315 | + $this->subjectMock, |
| 316 | + $originalResult, |
| 317 | + $this->productMock |
| 318 | + ); |
| 319 | + |
| 320 | + // The result should be the actual price from product, not the original result |
| 321 | + $this->assertEquals($actualPrice, $result); |
| 322 | + $this->assertNotEquals($originalResult, $result); |
| 323 | + } |
| 324 | +} |
0 commit comments