33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Catalog \Model \Product \Type ;
79
10+ use Magento \Catalog \Api \Data \ProductCustomOptionInterface ;
11+ use Magento \Catalog \Api \ProductRepositoryInterface ;
812use 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
30+ */
31+ private $ objectManager ;
32+
33+ /**
34+ * @var Price
1735 */
18- protected $ _model ;
36+ private $ productPrice ;
1937
20- protected function setUp ()
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 ' => 'test ' ]]);
115+ $ this ->assertEquals ('test ' , $ this ->productPrice ->getPrice ($ objectWithPrice ));
116+ }
117+
118+ /**
119+ * Get product final price for different product count.
120+ *
121+ * @return void
122+ */
123+ public function testGetFinalPrice (): void
124+ {
125+ $ product = $ this ->productRepository ->get ('simple ' );
39126
40127 // 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 ));
128+ $ this ->assertEquals (10.0 , $ this ->productPrice ->getFinalPrice (1 , $ product ));
129+ $ this ->assertEquals (8.0 , $ this ->productPrice ->getFinalPrice (2 , $ product ));
130+ $ this ->assertEquals (5.0 , $ this ->productPrice ->getFinalPrice (5 , $ product ));
44131
45132 // with options
46133 $ buyRequest = $ this ->prepareBuyRequest ($ product );
47134 $ product ->getTypeInstance ()->prepareForCart ($ buyRequest , $ product );
48135
49136 //product price + options price(10+1+2+3+3)
50- $ this ->assertEquals (19.0 , $ this ->_model ->getFinalPrice (1 , $ product ));
137+ $ this ->assertEquals (19.0 , $ this ->productPrice ->getFinalPrice (1 , $ product ));
51138
52139 //product tier price + options price(5+1+2+3+3)
53- $ this ->assertEquals (14.0 , $ this ->_model ->getFinalPrice (5 , $ product ));
140+ $ this ->assertEquals (14.0 , $ this ->productPrice ->getFinalPrice (5 , $ product ));
54141 }
55142
56- public function testGetFormatedPrice ()
143+ /**
144+ * Assert that formated price is correct.
145+ *
146+ * @return void
147+ */
148+ public function testGetFormatedPrice (): void
57149 {
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 ));
150+ $ product = $ this ->productRepository ->get ('simple ' );
151+ $ this ->assertEquals ('<span class="price">$10.00</span> ' , $ this ->productPrice ->getFormatedPrice ($ product ));
64152 }
65153
66- public function testCalculatePrice ()
154+ /**
155+ * Test calculate price by date.
156+ *
157+ * @return void
158+ */
159+ public function testCalculatePrice (): 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+ $ this ->assertEquals (
162+ 10 ,
163+ $ this ->productPrice ->calculatePrice (10 , 8 , '1970-12-12 23:59:59 ' , '1971-01-01 01:01:01 ' )
164+ );
165+ $ this ->assertEquals (
166+ 8 ,
167+ $ this ->productPrice ->calculatePrice (10 , 8 , '1970-12-12 23:59:59 ' , '2034-01-01 01:01:01 ' )
168+ );
70169 }
71170
72- public function testCalculateSpecialPrice ()
171+ /**
172+ * Test calculate price by date.
173+ *
174+ * @return void
175+ */
176+ public function testCalculateSpecialPrice (): void
73177 {
74178 $ this ->assertEquals (
75179 10 ,
76- $ this ->_model ->calculateSpecialPrice (10 , 8 , '1970-12-12 23:59:59 ' , '1971-01-01 01:01:01 ' )
180+ $ this ->productPrice ->calculateSpecialPrice (10 , 8 , '1970-12-12 23:59:59 ' , '1971-01-01 01:01:01 ' )
77181 );
78182 $ this ->assertEquals (
79183 8 ,
80- $ this ->_model ->calculateSpecialPrice (10 , 8 , '1970-12-12 23:59:59 ' , '2034-01-01 01:01:01 ' )
184+ $ this ->productPrice ->calculateSpecialPrice (10 , 8 , '1970-12-12 23:59:59 ' , '2034-01-01 01:01:01 ' )
81185 );
82186 }
83187
84- public function testIsTierPriceFixed ()
188+ /**
189+ * Assert that product tier price is fixed.
190+ *
191+ * @return void
192+ */
193+ public function testIsTierPriceFixed (): void
85194 {
86- $ this ->assertTrue ($ this ->_model ->isTierPriceFixed ());
195+ $ this ->assertTrue ($ this ->productPrice ->isTierPriceFixed ());
87196 }
88197
89198 /**
90- * Build buy request based on product custom options
199+ * Build buy request based on product custom options.
91200 *
92201 * @param Product $product
93- * @return \Magento\Framework\ DataObject
202+ * @return DataObject
94203 */
95- private function prepareBuyRequest (Product $ product )
204+ private function prepareBuyRequest (Product $ product ): DataObject
96205 {
97206 $ options = [];
98- /** @var $option \Magento\Catalog\Model\Product\Option */
207+ /** @var Option $option */
99208 foreach ($ product ->getOptions () as $ option ) {
100209 switch ($ option ->getGroupByType ()) {
101- case \ Magento \ Catalog \ Api \ Data \ ProductCustomOptionInterface::OPTION_GROUP_DATE :
210+ case ProductCustomOptionInterface::OPTION_GROUP_DATE :
102211 $ value = ['year ' => 2013 , 'month ' => 8 , 'day ' => 9 , 'hour ' => 13 , 'minute ' => 35 ];
103212 break ;
104- case \ Magento \ Catalog \ Api \ Data \ ProductCustomOptionInterface::OPTION_GROUP_SELECT :
213+ case ProductCustomOptionInterface::OPTION_GROUP_SELECT :
105214 $ value = key ($ option ->getValues ());
106215 break ;
107216 default :
@@ -111,6 +220,6 @@ private function prepareBuyRequest(Product $product)
111220 $ options [$ option ->getId ()] = $ value ;
112221 }
113222
114- return new \ Magento \ Framework \ DataObject ([ ' qty ' => 1 , 'options ' => $ options ]);
223+ return $ this -> objectManager -> create (DataObject::class, [ ' data ' => [ ' qty ' => 1 , 'options ' => $ options] ]);
115224 }
116225}
0 commit comments