3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Model \Product \Type ;
7
9
10
+ use Magento \Catalog \Api \Data \ProductCustomOptionInterface ;
11
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
8
12
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 ;
9
19
10
20
/**
21
+ * Simple product price test.
22
+ *
11
23
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
24
+ * @magentoDbIsolation enabled
12
25
*/
13
- class PriceTest extends \ PHPUnit \ Framework \ TestCase
26
+ class PriceTest extends TestCase
14
27
{
15
28
/**
16
- * @var \Magento\Catalog\Model\Product\Type\Price
29
+ * @var ObjectManager
17
30
*/
18
- protected $ _model ;
31
+ private $ objectManager ;
19
32
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
21
52
{
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);
25
57
}
26
58
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
28
73
{
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
+ }
30
82
}
31
83
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
33
95
{
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 ' );
39
137
40
138
// 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 ));
44
142
45
143
// with options
46
144
$ buyRequest = $ this ->prepareBuyRequest ($ product );
47
145
$ product ->getTypeInstance ()->prepareForCart ($ buyRequest , $ product );
48
146
49
147
//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 ));
51
149
52
150
//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 ));
64
152
}
65
153
66
- public function testCalculatePrice ()
154
+ /**
155
+ * Assert that formated price is correct.
156
+ *
157
+ * @return void
158
+ */
159
+ public function testGetFormatedPrice (): void
67
160
{
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 ));
70
163
}
71
164
72
- public function testCalculateSpecialPrice ()
165
+ /**
166
+ * Test calculate price by date.
167
+ *
168
+ * @return void
169
+ */
170
+ public function testCalculatePrice (): void
73
171
{
74
172
$ this ->assertEquals (
75
173
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 ' )
77
175
);
78
176
$ this ->assertEquals (
79
177
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 ' )
81
196
);
82
197
}
83
198
84
- public function testIsTierPriceFixed ()
199
+ /**
200
+ * Assert that product tier price is fixed.
201
+ *
202
+ * @return void
203
+ */
204
+ public function testIsTierPriceFixed (): void
85
205
{
86
- $ this ->assertTrue ($ this ->_model ->isTierPriceFixed ());
206
+ $ this ->assertTrue ($ this ->productPrice ->isTierPriceFixed ());
87
207
}
88
208
89
209
/**
90
- * Build buy request based on product custom options
210
+ * Build buy request based on product custom options.
91
211
*
92
212
* @param Product $product
93
- * @return \Magento\Framework\ DataObject
213
+ * @return DataObject
94
214
*/
95
- private function prepareBuyRequest (Product $ product )
215
+ private function prepareBuyRequest (Product $ product ): DataObject
96
216
{
97
217
$ options = [];
98
- /** @var $option \Magento\Catalog\Model\Product\Option */
218
+ /** @var Option $option */
99
219
foreach ($ product ->getOptions () as $ option ) {
100
220
switch ($ option ->getGroupByType ()) {
101
- case \ Magento \ Catalog \ Api \ Data \ ProductCustomOptionInterface::OPTION_GROUP_DATE :
221
+ case ProductCustomOptionInterface::OPTION_GROUP_DATE :
102
222
$ value = ['year ' => 2013 , 'month ' => 8 , 'day ' => 9 , 'hour ' => 13 , 'minute ' => 35 ];
103
223
break ;
104
- case \ Magento \ Catalog \ Api \ Data \ ProductCustomOptionInterface::OPTION_GROUP_SELECT :
224
+ case ProductCustomOptionInterface::OPTION_GROUP_SELECT :
105
225
$ value = key ($ option ->getValues ());
106
226
break ;
107
227
default :
@@ -111,6 +231,6 @@ private function prepareBuyRequest(Product $product)
111
231
$ options [$ option ->getId ()] = $ value ;
112
232
}
113
233
114
- return new \ Magento \ Framework \ DataObject ([ ' qty ' => 1 , 'options ' => $ options ]);
234
+ return $ this -> objectManager -> create (DataObject::class, [ ' data ' => [ ' qty ' => 1 , 'options ' => $ options] ]);
115
235
}
116
236
}
0 commit comments