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
30
+ */
31
+ private $ objectManager ;
32
+
33
+ /**
34
+ * @var Price
17
35
*/
18
- protected $ _model ;
36
+ private $ productPrice ;
19
37
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
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 ' => '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 ' );
39
126
40
127
// 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 ));
44
131
45
132
// with options
46
133
$ buyRequest = $ this ->prepareBuyRequest ($ product );
47
134
$ product ->getTypeInstance ()->prepareForCart ($ buyRequest , $ product );
48
135
49
136
//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 ));
51
138
52
139
//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 ));
54
141
}
55
142
56
- public function testGetFormatedPrice ()
143
+ /**
144
+ * Assert that formated price is correct.
145
+ *
146
+ * @return void
147
+ */
148
+ public function testGetFormatedPrice (): void
57
149
{
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 ));
64
152
}
65
153
66
- public function testCalculatePrice ()
154
+ /**
155
+ * Test calculate price by date.
156
+ *
157
+ * @return void
158
+ */
159
+ public function testCalculatePrice (): 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
+ $ 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
+ );
70
169
}
71
170
72
- public function testCalculateSpecialPrice ()
171
+ /**
172
+ * Test calculate price by date.
173
+ *
174
+ * @return void
175
+ */
176
+ public function testCalculateSpecialPrice (): void
73
177
{
74
178
$ this ->assertEquals (
75
179
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 ' )
77
181
);
78
182
$ this ->assertEquals (
79
183
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 ' )
81
185
);
82
186
}
83
187
84
- public function testIsTierPriceFixed ()
188
+ /**
189
+ * Assert that product tier price is fixed.
190
+ *
191
+ * @return void
192
+ */
193
+ public function testIsTierPriceFixed (): void
85
194
{
86
- $ this ->assertTrue ($ this ->_model ->isTierPriceFixed ());
195
+ $ this ->assertTrue ($ this ->productPrice ->isTierPriceFixed ());
87
196
}
88
197
89
198
/**
90
- * Build buy request based on product custom options
199
+ * Build buy request based on product custom options.
91
200
*
92
201
* @param Product $product
93
- * @return \Magento\Framework\ DataObject
202
+ * @return DataObject
94
203
*/
95
- private function prepareBuyRequest (Product $ product )
204
+ private function prepareBuyRequest (Product $ product ): DataObject
96
205
{
97
206
$ options = [];
98
- /** @var $option \Magento\Catalog\Model\Product\Option */
207
+ /** @var Option $option */
99
208
foreach ($ product ->getOptions () as $ option ) {
100
209
switch ($ option ->getGroupByType ()) {
101
- case \ Magento \ Catalog \ Api \ Data \ ProductCustomOptionInterface::OPTION_GROUP_DATE :
210
+ case ProductCustomOptionInterface::OPTION_GROUP_DATE :
102
211
$ value = ['year ' => 2013 , 'month ' => 8 , 'day ' => 9 , 'hour ' => 13 , 'minute ' => 35 ];
103
212
break ;
104
- case \ Magento \ Catalog \ Api \ Data \ ProductCustomOptionInterface::OPTION_GROUP_SELECT :
213
+ case ProductCustomOptionInterface::OPTION_GROUP_SELECT :
105
214
$ value = key ($ option ->getValues ());
106
215
break ;
107
216
default :
@@ -111,6 +220,6 @@ private function prepareBuyRequest(Product $product)
111
220
$ options [$ option ->getId ()] = $ value ;
112
221
}
113
222
114
- return new \ Magento \ Framework \ DataObject ([ ' qty ' => 1 , 'options ' => $ options ]);
223
+ return $ this -> objectManager -> create (DataObject::class, [ ' data ' => [ ' qty ' => 1 , 'options ' => $ options] ]);
115
224
}
116
225
}
0 commit comments