Skip to content

Inheriting from a class that doesn't exist#33334 #33449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,20 @@ class ConfiguredPrice extends CatalogPrice\FinalPrice implements ConfiguredPrice
*/
private $configuredPriceSelection;

/**
* @var DiscountCalculator
*/
private $discountCalculator;

/**
* @param Product $saleableItem
* @param float $quantity
* @param BundleCalculatorInterface $calculator
* @param PriceCurrencyInterface $priceCurrency
* @param ItemInterface $item
* @param ItemInterface|null $item
* @param JsonSerializer|null $serializer
* @param ConfiguredPriceSelection|null $configuredPriceSelection
* @param DiscountCalculator|null $discountCalculator
*/
public function __construct(
Product $saleableItem,
Expand All @@ -65,14 +71,17 @@ public function __construct(
PriceCurrencyInterface $priceCurrency,
ItemInterface $item = null,
JsonSerializer $serializer = null,
ConfiguredPriceSelection $configuredPriceSelection = null
ConfiguredPriceSelection $configuredPriceSelection = null,
DiscountCalculator $discountCalculator = null
) {
$this->item = $item;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(JsonSerializer::class);
$this->configuredPriceSelection = $configuredPriceSelection
?: \Magento\Framework\App\ObjectManager::getInstance()
->get(ConfiguredPriceSelection::class);
$this->discountCalculator = $discountCalculator
?: \Magento\Framework\App\ObjectManager::getInstance()->get(DiscountCalculator::class);
parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
}

Expand Down Expand Up @@ -144,12 +153,9 @@ public function getConfiguredAmount($baseValue = 0.)
*/
public function getValue()
{
if ($this->item) {
if ($this->item && $this->item->getProduct()->getId()) {
$configuredOptionsAmount = $this->getConfiguredAmount()->getBaseAmount();
return parent::getValue() +
$this->priceInfo
->getPrice(BundleDiscountPrice::PRICE_CODE)
->calculateDiscount($configuredOptionsAmount);
return parent::getValue() + $this->discountCalculator->calculateDiscount($this->item->getProduct(), $configuredOptionsAmount);
}
return parent::getValue();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Bundle\Test\Unit\Pricing\Price;

use Magento\Bundle\Pricing\Price\DiscountCalculator;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
use Magento\Bundle\Pricing\Price\ConfiguredPrice;
use Magento\Bundle\Pricing\Adjustment\Calculator;
use Magento\Catalog\Pricing\Price\ConfiguredPriceSelection;
use Magento\Framework\DataObject;
use Magento\Framework\Pricing\Amount\AmountInterface;
use Magento\Framework\Pricing\Price\PriceInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\Pricing\PriceInfo\Base;
use Magento\Framework\Serialize\Serializer\Json;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Test for \Magento\Bundle\Pricing\Price\ConfiguredPrice
*/
class ConfiguredPriceTest extends TestCase
{
/**
* @var float
*/
private $basePriceValue = 100.00;

/**
* @var ItemInterface|MockObject
*/
private $itemMock;

/**
* @var Product|MockObject
*/
private $productMock;

/**
* @var Calculator|MockObject
*/
private $calculatorMock;

/**
* @var Base|MockObject
*/
private $priceInfoMock;

/**
* @var ConfiguredPrice
*/
private $model;

/**
* @var PriceCurrencyInterface|MockObject
*/
private $priceCurrencyMock;

/**
* @var Json|MockObject
*/
private $jsonSerializerMock;

/**
* @var ConfiguredPriceSelection|MockObject
*/
private $configuredPriceSelectionMock;

/**
* @var AmountInterface|MockObject
*/
private $amountInterfaceMock;

/**
* @var DiscountCalculator|MockObject
*/
private $discountCalculatorMock;

/**
* Initialize base dependencies
*/
protected function setUp(): void
{
$basePrice = $this->getMockForAbstractClass(PriceInterface::class);
$basePrice->expects($this->any())->method('getValue')->willReturn($this->basePriceValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically the setUp method, is used to mock the needed objects, that will be used later by different test cases. Please move all the assertions into a separate test case instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. But in setUp I dont have assertions


$this->priceInfoMock = $this->createMock(Base::class);
$this->priceInfoMock->expects($this->any())->method('getPrice')->willReturn($basePrice);
$this->productMock = $this->getMockBuilder(Product::class)
->setMethods(['getPriceInfo', 'getOptionById', 'getResource', 'getId'])
->disableOriginalConstructor()
->getMock();
$this->productMock->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfoMock);
$this->priceCurrencyMock = $this->getMockForAbstractClass(PriceCurrencyInterface::class);

$this->jsonSerializerMock = $this->getMockBuilder(Json::class)
->getMock();
$this->configuredPriceSelectionMock = $this->getMockBuilder(ConfiguredPriceSelection::class)
->setMethods(['getSelectionPriceList'])
->disableOriginalConstructor()
->getMock();
$this->configuredPriceSelectionMock->expects($this->any())->method('getSelectionPriceList')
->willReturn($this->prepareAndReturnSelectionPriceDataStub());
$this->amountInterfaceMock = $this->getMockBuilder(AmountInterface::class)->getMock();
$this->amountInterfaceMock->expects($this->any())->method('getBaseAmount')
->willReturn(100.00);
$this->calculatorMock = $this->getMockBuilder(Calculator::class)
->disableOriginalConstructor()
->getMock();
$this->calculatorMock->expects($this->any())->method('calculateBundleAmount')
->willReturn($this->amountInterfaceMock);
$this->discountCalculatorMock = $this->getMockBuilder(DiscountCalculator::class)
->disableOriginalConstructor()
->getMock();
$this->discountCalculatorMock->expects($this->any())->method('calculateDiscount')
->willReturn(-5.00);
$this->model = new ConfiguredPrice(
$this->productMock,
1,
$this->calculatorMock,
$this->priceCurrencyMock,
null,
$this->jsonSerializerMock,
$this->configuredPriceSelectionMock,
$this->discountCalculatorMock,
);
}

/**
* Test of value getter when item presented
*/
public function testGetValueMethod(): void
{
$this->productMock->expects($this->any())->method('getId')->willReturn(123);
$this->itemMock = $this->getMockBuilder(ItemInterface::class)
->getMock();
$this->itemMock->expects($this->any())->method('getProduct')->willReturn($this->productMock);
$this->model->setItem($this->itemMock);
$valueFromMock = $this->model->getValue();
$this->assertEquals(95.00, $valueFromMock);
}

/**
* Test of value getter if no product item
*/
public function testGetValueMethodNoItem(): void
{
$this->productMock = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();
$this->itemMock = $this->getMockBuilder(ItemInterface::class)
->getMock();
$this->itemMock->expects($this->any())->method('getProduct')->willReturn($this->productMock);
$this->productMock->expects($this->any())->method('getId')->willReturn(false);
$this->model->setItem($this->itemMock);
$valueFromMock = $this->model->getValue();
$this->assertEquals(100.00, $valueFromMock);
}

/**
* Stub data for calculation amount of bundle
* @return \Magento\Framework\DataObject[]
*/
private function prepareAndReturnSelectionPriceDataStub(): array
{
$first = new DataObject();
$first->setValue(2);
$first->setQuantity(1);
$second = new DataObject();
$second->setValue(3);
$second->setQuantity(1);
return [
$first,
$second
];
}
}