Skip to content

Commit 3cd62c7

Browse files
MC-29866: getBasePrice function returns a string sometimes
1 parent c3630c9 commit 3cd62c7

File tree

2 files changed

+167
-47
lines changed
  • app/code/Magento/Catalog/Model/Product/Type
  • dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Type

2 files changed

+167
-47
lines changed

app/code/Magento/Catalog/Model/Product/Type/Price.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ protected function _applyTierPrice($product, $qty, $finalPrice)
262262

263263
$tierPrice = $product->getTierPrice($qty);
264264
if (is_numeric($tierPrice)) {
265-
$finalPrice = min($finalPrice, $tierPrice);
265+
$finalPrice = min($finalPrice, (float) $tierPrice);
266266
}
267267
return $finalPrice;
268268
}
@@ -645,7 +645,7 @@ public function calculateSpecialPrice(
645645
) {
646646
if ($specialPrice !== null && $specialPrice != false) {
647647
if ($this->_localeDate->isScopeDateInInterval($store, $specialPriceFrom, $specialPriceTo)) {
648-
$finalPrice = min($finalPrice, $specialPrice);
648+
$finalPrice = min($finalPrice, (float) $specialPrice);
649649
}
650650
}
651651
return $finalPrice;

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Type/PriceTest.php

Lines changed: 165 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,225 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Model\Product\Type;
79

10+
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
812
use Magento\Catalog\Model\Product;
13+
use Magento\Catalog\Model\Product\Option;
14+
use Magento\Customer\Model\Session;
15+
use Magento\Framework\DataObject;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\ObjectManager;
18+
use PHPUnit\Framework\TestCase;
919

1020
/**
21+
* Simple product price test.
22+
*
1123
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
24+
* @magentoDbIsolation enabled
1225
*/
13-
class PriceTest extends \PHPUnit\Framework\TestCase
26+
class PriceTest extends TestCase
1427
{
1528
/**
16-
* @var \Magento\Catalog\Model\Product\Type\Price
29+
* @var ObjectManager
1730
*/
18-
protected $_model;
31+
private $objectManager;
1932

20-
protected function setUp()
33+
/**
34+
* @var Price
35+
*/
36+
private $productPrice;
37+
38+
/**
39+
* @var ProductRepositoryInterface
40+
*/
41+
private $productRepository;
42+
43+
/**
44+
* @var Session
45+
*/
46+
private $customerSession;
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
protected function setUp(): void
2152
{
22-
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
23-
\Magento\Catalog\Model\Product\Type\Price::class
24-
);
53+
$this->objectManager = Bootstrap::getObjectManager();
54+
$this->productPrice = $this->objectManager->create(Price::class);
55+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
56+
$this->customerSession = $this->objectManager->get(Session::class);
2557
}
2658

27-
public function testGetPrice()
59+
/**
60+
* Assert that for logged user product price equal to price from catalog rule.
61+
*
62+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
63+
* @magentoDataFixture Magento/CatalogRule/_files/catalog_rule_6_off_logged_user.php
64+
* @magentoDataFixture Magento/Customer/_files/customer.php
65+
*
66+
* @magentoDbIsolation disabled
67+
* @magentoAppArea frontend
68+
* @magentoAppIsolation enabled
69+
*
70+
* @return void
71+
*/
72+
public function testPriceByRuleForLoggedUser(): void
2873
{
29-
$this->assertEquals('test', $this->_model->getPrice(new \Magento\Framework\DataObject(['price' => 'test'])));
74+
$product = $this->productRepository->get('simple');
75+
$this->assertEquals(10, $this->productPrice->getFinalPrice(1, $product));
76+
$this->customerSession->setCustomerId(1);
77+
try {
78+
$this->assertEquals(4, $this->productPrice->getFinalPrice(1, $product));
79+
} finally {
80+
$this->customerSession->setCustomerId(null);
81+
}
3082
}
3183

32-
public function testGetFinalPrice()
84+
/**
85+
* Assert price for different customer groups.
86+
*
87+
* @magentoDataFixture Magento/Catalog/_files/simple_product_with_tier_price_for_logged_user.php
88+
* @magentoDataFixture Magento/Customer/_files/customer.php
89+
*
90+
* @magentoAppIsolation enabled
91+
*
92+
* @return void
93+
*/
94+
public function testTierPriceWithDifferentCustomerGroups(): void
3395
{
34-
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
35-
\Magento\Catalog\Model\ProductRepository::class
36-
);
37-
$product = $repository->get('simple');
38-
// fixture
96+
$product = $this->productRepository->get('simple');
97+
$this->assertEquals(8, $this->productPrice->getFinalPrice(2, $product));
98+
$this->assertEquals(5, $this->productPrice->getFinalPrice(3, $product));
99+
$this->customerSession->setCustomerId(1);
100+
try {
101+
$this->assertEquals(1, $this->productPrice->getFinalPrice(3, $product));
102+
} finally {
103+
$this->customerSession->setCustomerId(null);
104+
}
105+
}
106+
107+
/**
108+
* Get price from custom object.
109+
*
110+
* @return void
111+
*/
112+
public function testGetPrice(): void
113+
{
114+
$objectWithPrice = $this->objectManager->create(DataObject::class, ['data' => ['price' => 9.0]]);
115+
$this->assertEquals(9.0, $this->productPrice->getPrice($objectWithPrice));
116+
}
117+
118+
/**
119+
* Get base price from product.
120+
*
121+
* @return void
122+
*/
123+
public function testGetBasePrice(): void
124+
{
125+
$product = $this->productRepository->get('simple');
126+
$this->assertSame(10.0, $this->productPrice->getBasePrice($product));
127+
}
128+
129+
/**
130+
* Get product final price for different product count.
131+
*
132+
* @return void
133+
*/
134+
public function testGetFinalPrice(): void
135+
{
136+
$product = $this->productRepository->get('simple');
39137

40138
// regular & tier prices
41-
$this->assertEquals(10.0, $this->_model->getFinalPrice(1, $product));
42-
$this->assertEquals(8.0, $this->_model->getFinalPrice(2, $product));
43-
$this->assertEquals(5.0, $this->_model->getFinalPrice(5, $product));
139+
$this->assertEquals(10.0, $this->productPrice->getFinalPrice(1, $product));
140+
$this->assertEquals(8.0, $this->productPrice->getFinalPrice(2, $product));
141+
$this->assertEquals(5.0, $this->productPrice->getFinalPrice(5, $product));
44142

45143
// with options
46144
$buyRequest = $this->prepareBuyRequest($product);
47145
$product->getTypeInstance()->prepareForCart($buyRequest, $product);
48146

49147
//product price + options price(10+1+2+3+3)
50-
$this->assertEquals(19.0, $this->_model->getFinalPrice(1, $product));
148+
$this->assertEquals(19.0, $this->productPrice->getFinalPrice(1, $product));
51149

52150
//product tier price + options price(5+1+2+3+3)
53-
$this->assertEquals(14.0, $this->_model->getFinalPrice(5, $product));
54-
}
55-
56-
public function testGetFormatedPrice()
57-
{
58-
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
59-
\Magento\Catalog\Model\ProductRepository::class
60-
);
61-
$product = $repository->get('simple');
62-
// fixture
63-
$this->assertEquals('<span class="price">$10.00</span>', $this->_model->getFormatedPrice($product));
151+
$this->assertEquals(14.0, $this->productPrice->getFinalPrice(5, $product));
64152
}
65153

66-
public function testCalculatePrice()
154+
/**
155+
* Assert that formated price is correct.
156+
*
157+
* @return void
158+
*/
159+
public function testGetFormatedPrice(): void
67160
{
68-
$this->assertEquals(10, $this->_model->calculatePrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01'));
69-
$this->assertEquals(8, $this->_model->calculatePrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01'));
161+
$product = $this->productRepository->get('simple');
162+
$this->assertEquals('<span class="price">$10.00</span>', $this->productPrice->getFormatedPrice($product));
70163
}
71164

72-
public function testCalculateSpecialPrice()
165+
/**
166+
* Test calculate price by date.
167+
*
168+
* @return void
169+
*/
170+
public function testCalculatePrice(): void
73171
{
74172
$this->assertEquals(
75173
10,
76-
$this->_model->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01')
174+
$this->productPrice->calculatePrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01')
77175
);
78176
$this->assertEquals(
79177
8,
80-
$this->_model->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01')
178+
$this->productPrice->calculatePrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01')
179+
);
180+
}
181+
182+
/**
183+
* Test calculate price by date.
184+
*
185+
* @return void
186+
*/
187+
public function testCalculateSpecialPrice(): void
188+
{
189+
$this->assertSame(
190+
10.0,
191+
$this->productPrice->calculateSpecialPrice(10.0, 8.0, '1970-12-12 23:59:59', '1971-01-01 01:01:01')
192+
);
193+
$this->assertSame(
194+
8.0,
195+
$this->productPrice->calculateSpecialPrice(10.0, 8.0, '1970-12-12 23:59:59', '2034-01-01 01:01:01')
81196
);
82197
}
83198

84-
public function testIsTierPriceFixed()
199+
/**
200+
* Assert that product tier price is fixed.
201+
*
202+
* @return void
203+
*/
204+
public function testIsTierPriceFixed(): void
85205
{
86-
$this->assertTrue($this->_model->isTierPriceFixed());
206+
$this->assertTrue($this->productPrice->isTierPriceFixed());
87207
}
88208

89209
/**
90-
* Build buy request based on product custom options
210+
* Build buy request based on product custom options.
91211
*
92212
* @param Product $product
93-
* @return \Magento\Framework\DataObject
213+
* @return DataObject
94214
*/
95-
private function prepareBuyRequest(Product $product)
215+
private function prepareBuyRequest(Product $product): DataObject
96216
{
97217
$options = [];
98-
/** @var $option \Magento\Catalog\Model\Product\Option */
218+
/** @var Option $option */
99219
foreach ($product->getOptions() as $option) {
100220
switch ($option->getGroupByType()) {
101-
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_GROUP_DATE:
221+
case ProductCustomOptionInterface::OPTION_GROUP_DATE:
102222
$value = ['year' => 2013, 'month' => 8, 'day' => 9, 'hour' => 13, 'minute' => 35];
103223
break;
104-
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_GROUP_SELECT:
224+
case ProductCustomOptionInterface::OPTION_GROUP_SELECT:
105225
$value = key($option->getValues());
106226
break;
107227
default:
@@ -111,6 +231,6 @@ private function prepareBuyRequest(Product $product)
111231
$options[$option->getId()] = $value;
112232
}
113233

114-
return new \Magento\Framework\DataObject(['qty' => 1, 'options' => $options]);
234+
return $this->objectManager->create(DataObject::class, ['data' => ['qty' => 1, 'options' => $options]]);
115235
}
116236
}

0 commit comments

Comments
 (0)