|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Bundle\Test\Unit\Pricing\Price; |
| 9 | + |
| 10 | +use Magento\Bundle\Pricing\Price\DiscountCalculator; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface; |
| 13 | +use Magento\Bundle\Pricing\Price\ConfiguredPrice; |
| 14 | +use Magento\Bundle\Pricing\Adjustment\Calculator; |
| 15 | +use Magento\Catalog\Pricing\Price\ConfiguredPriceSelection; |
| 16 | +use Magento\Framework\DataObject; |
| 17 | +use Magento\Framework\Pricing\Amount\AmountInterface; |
| 18 | +use Magento\Framework\Pricing\Price\PriceInterface; |
| 19 | +use Magento\Framework\Pricing\PriceCurrencyInterface; |
| 20 | +use Magento\Framework\Pricing\PriceInfo\Base; |
| 21 | +use Magento\Framework\Serialize\Serializer\Json; |
| 22 | +use PHPUnit\Framework\MockObject\MockObject; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +/** |
| 26 | + * Test for \Magento\Bundle\Pricing\Price\ConfiguredPrice |
| 27 | + */ |
| 28 | +class ConfiguredPriceTest extends TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var float |
| 32 | + */ |
| 33 | + private $basePriceValue = 100.00; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ItemInterface|MockObject |
| 37 | + */ |
| 38 | + private $itemMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Product|MockObject |
| 42 | + */ |
| 43 | + private $productMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var Calculator|MockObject |
| 47 | + */ |
| 48 | + private $calculatorMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var Base|MockObject |
| 52 | + */ |
| 53 | + private $priceInfoMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var ConfiguredPrice |
| 57 | + */ |
| 58 | + private $model; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var PriceCurrencyInterface|MockObject |
| 62 | + */ |
| 63 | + private $priceCurrencyMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var Json|MockObject |
| 67 | + */ |
| 68 | + private $jsonSerializerMock; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var ConfiguredPriceSelection|MockObject |
| 72 | + */ |
| 73 | + private $configuredPriceSelectionMock; |
| 74 | + |
| 75 | + /** |
| 76 | + * @var AmountInterface|MockObject |
| 77 | + */ |
| 78 | + private $amountInterfaceMock; |
| 79 | + |
| 80 | + /** |
| 81 | + * @var DiscountCalculator|MockObject |
| 82 | + */ |
| 83 | + private $discountCalculatorMock; |
| 84 | + |
| 85 | + /** |
| 86 | + * Initialize base dependencies |
| 87 | + */ |
| 88 | + protected function setUp(): void |
| 89 | + { |
| 90 | + $basePrice = $this->getMockForAbstractClass(PriceInterface::class); |
| 91 | + $basePrice->expects($this->any())->method('getValue')->willReturn($this->basePriceValue); |
| 92 | + |
| 93 | + $this->priceInfoMock = $this->createMock(Base::class); |
| 94 | + $this->priceInfoMock->expects($this->any())->method('getPrice')->willReturn($basePrice); |
| 95 | + $this->productMock = $this->getMockBuilder(Product::class) |
| 96 | + ->setMethods(['getPriceInfo', 'getOptionById', 'getResource', 'getId']) |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->getMock(); |
| 99 | + $this->productMock->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfoMock); |
| 100 | + $this->priceCurrencyMock = $this->getMockForAbstractClass(PriceCurrencyInterface::class); |
| 101 | + |
| 102 | + $this->jsonSerializerMock = $this->getMockBuilder(Json::class) |
| 103 | + ->getMock(); |
| 104 | + $this->configuredPriceSelectionMock = $this->getMockBuilder(ConfiguredPriceSelection::class) |
| 105 | + ->setMethods(['getSelectionPriceList']) |
| 106 | + ->disableOriginalConstructor() |
| 107 | + ->getMock(); |
| 108 | + $this->configuredPriceSelectionMock->expects($this->any())->method('getSelectionPriceList') |
| 109 | + ->willReturn($this->prepareAndReturnSelectionPriceDataStub()); |
| 110 | + $this->amountInterfaceMock = $this->getMockBuilder(AmountInterface::class)->getMock(); |
| 111 | + $this->amountInterfaceMock->expects($this->any())->method('getBaseAmount') |
| 112 | + ->willReturn(100.00); |
| 113 | + $this->calculatorMock = $this->getMockBuilder(Calculator::class) |
| 114 | + ->disableOriginalConstructor() |
| 115 | + ->getMock(); |
| 116 | + $this->calculatorMock->expects($this->any())->method('calculateBundleAmount') |
| 117 | + ->willReturn($this->amountInterfaceMock); |
| 118 | + $this->discountCalculatorMock = $this->getMockBuilder(DiscountCalculator::class) |
| 119 | + ->disableOriginalConstructor() |
| 120 | + ->getMock(); |
| 121 | + $this->discountCalculatorMock->expects($this->any())->method('calculateDiscount') |
| 122 | + ->willReturn(-5.00); |
| 123 | + $this->model = new ConfiguredPrice( |
| 124 | + $this->productMock, |
| 125 | + 1, |
| 126 | + $this->calculatorMock, |
| 127 | + $this->priceCurrencyMock, |
| 128 | + null, |
| 129 | + $this->jsonSerializerMock, |
| 130 | + $this->configuredPriceSelectionMock, |
| 131 | + $this->discountCalculatorMock, |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Test of value getter when item presented |
| 137 | + */ |
| 138 | + public function testGetValueMethod(): void |
| 139 | + { |
| 140 | + $this->productMock->expects($this->any())->method('getId')->willReturn(123); |
| 141 | + $this->itemMock = $this->getMockBuilder(ItemInterface::class) |
| 142 | + ->getMock(); |
| 143 | + $this->itemMock->expects($this->any())->method('getProduct')->willReturn($this->productMock); |
| 144 | + $this->model->setItem($this->itemMock); |
| 145 | + $valueFromMock = $this->model->getValue(); |
| 146 | + $this->assertEquals(95.00, $valueFromMock); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Test of value getter if no product item |
| 151 | + */ |
| 152 | + public function testGetValueMethodNoItem(): void |
| 153 | + { |
| 154 | + $this->productMock = $this->getMockBuilder(Product::class) |
| 155 | + ->disableOriginalConstructor() |
| 156 | + ->getMock(); |
| 157 | + $this->itemMock = $this->getMockBuilder(ItemInterface::class) |
| 158 | + ->getMock(); |
| 159 | + $this->itemMock->expects($this->any())->method('getProduct')->willReturn($this->productMock); |
| 160 | + $this->productMock->expects($this->any())->method('getId')->willReturn(false); |
| 161 | + $this->model->setItem($this->itemMock); |
| 162 | + $valueFromMock = $this->model->getValue(); |
| 163 | + $this->assertEquals(100.00, $valueFromMock); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Stub data for calculation amount of bundle |
| 168 | + * @return \Magento\Framework\DataObject[] |
| 169 | + */ |
| 170 | + private function prepareAndReturnSelectionPriceDataStub(): array |
| 171 | + { |
| 172 | + $first = new DataObject(); |
| 173 | + $first->setValue(2); |
| 174 | + $first->setQuantity(1); |
| 175 | + $second = new DataObject(); |
| 176 | + $second->setValue(3); |
| 177 | + $second->setQuantity(1); |
| 178 | + return [ |
| 179 | + $first, |
| 180 | + $second |
| 181 | + ]; |
| 182 | + } |
| 183 | +} |
0 commit comments