7
7
8
8
namespace Magento \GraphQl \CatalogCustomer ;
9
9
10
- use Magento \Catalog \Api \Data \ProductInterface ;
11
- use Magento \Catalog \Api \Data \ProductTierPriceInterfaceFactory ;
12
- use Magento \Catalog \Api \ProductRepositoryInterface ;
13
- use Magento \Catalog \Model \Product ;
14
- use Magento \Integration \Api \CustomerTokenServiceInterface ;
10
+ use Magento \GraphQl \GetCustomerAuthenticationHeader ;
15
11
use Magento \TestFramework \TestCase \GraphQlAbstract ;
16
12
use Magento \Store \Api \StoreRepositoryInterface ;
17
13
use Magento \TestFramework \Helper \Bootstrap ;
14
+ use Magento \TestFramework \ObjectManager ;
18
15
19
16
class PriceTiersTest extends GraphQlAbstract
20
17
{
21
18
/**
22
- * @var \Magento\TestFramework\ ObjectManager
19
+ * @var ObjectManager
23
20
*/
24
21
private $ objectManager ;
25
22
23
+ /**
24
+ * @var GetCustomerAuthenticationHeader
25
+ */
26
+ private $ getCustomerAuthenticationHeader ;
27
+
26
28
protected function setUp (): void
27
29
{
28
30
$ this ->objectManager = Bootstrap::getObjectManager ();
31
+ $ this ->getCustomerAuthenticationHeader = $ this ->objectManager ->get (GetCustomerAuthenticationHeader::class);
29
32
}
30
33
31
34
/**
32
35
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
33
36
*/
34
37
public function testAllGroups ()
35
38
{
36
- /** @var string $productSku */
37
39
$ productSku = 'simple ' ;
38
40
/** @var string $query */
39
41
$ query = $ this ->getProductSearchQuery ($ productSku );
40
42
41
43
$ response = $ this ->graphQlQuery ($ query );
42
44
43
45
$ itemTiers = $ response ['products ' ]['items ' ][0 ]['price_tiers ' ];
44
- $ this ->assertEquals (5 , sizeof ( $ itemTiers) );
46
+ $ this ->assertCount (5 , $ itemTiers );
45
47
$ this ->assertEquals (8 , $ this ->getValueForQuantity (2 , $ itemTiers ));
46
48
$ this ->assertEquals (5 , $ this ->getValueForQuantity (3 , $ itemTiers ));
47
49
$ this ->assertEquals (6 , $ this ->getValueForQuantity (3.2 , $ itemTiers ));
48
50
}
49
51
50
52
/**
51
- * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
52
53
* @magentoApiDataFixture Magento/Customer/_files/customer.php
54
+ * @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_multiple_groups.php
53
55
*/
54
56
public function testLoggedInCustomer ()
55
57
{
56
- /** @var string $productSku */
57
58
$ productSku = 'simple ' ;
58
- /** @var ProductRepositoryInterface $productRepository */
59
- $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
60
- /** @var Product $product */
61
- $ product = $ productRepository ->get ($ productSku , false , null , true );
62
- $ tierPriceData =[
63
- [
64
- 'customer_group_id ' => 1 ,
65
- 'percentage_value ' => null ,
66
- 'qty ' => 2 ,
67
- 'value ' => 9
68
- ],
69
- [
70
- 'customer_group_id ' => 1 ,
71
- 'percentage_value ' => null ,
72
- 'qty ' => 3 ,
73
- 'value ' => 8.25
74
- ],
75
- [
76
- 'customer_group_id ' => 1 ,
77
- 'percentage_value ' => null ,
78
- 'qty ' => 5 ,
79
- 'value ' => 7
80
- ],
81
- [
82
- 'customer_group_id ' => 2 ,
83
- 'percentage_value ' => null ,
84
- 'qty ' => 3 ,
85
- 'value ' => 8
86
- ]
87
- ];
88
-
89
- $ this ->saveTierPrices ($ product , $ tierPriceData );
90
59
/** @var string $query */
91
-
92
60
$ query = $ this ->getProductSearchQuery ($ productSku );
93
61
$ response = $ this ->graphQlQuery (
94
62
$ query ,
95
63
[],
96
64
'' ,
97
- $ this ->
getHeaderAuthorization (
'[email protected] ' ,
'password ' )
65
+ $ this ->
getCustomerAuthenticationHeader -> execute (
'[email protected] ' ,
'password ' )
98
66
);
99
67
100
68
$ itemTiers = $ response ['products ' ]['items ' ][0 ]['price_tiers ' ];
101
- $ this ->assertEquals (3 , sizeof ( $ itemTiers) );
102
- $ this ->assertEquals (9 , $ this ->getValueForQuantity (2 , $ itemTiers ));
69
+ $ this ->assertCount (3 , $ itemTiers );
70
+ $ this ->assertEquals (9.25 , $ this ->getValueForQuantity (2 , $ itemTiers ));
103
71
$ this ->assertEquals (8.25 , $ this ->getValueForQuantity (3 , $ itemTiers ));
104
- $ this ->assertEquals (7 , $ this ->getValueForQuantity (5 , $ itemTiers ));
72
+ $ this ->assertEquals (7.25 , $ this ->getValueForQuantity (5 , $ itemTiers ));
105
73
}
106
74
107
75
/**
108
76
* @magentoApiDataFixture Magento/Store/_files/second_store_with_second_currency.php
109
77
* @magentoApiDataFixture Magento/Customer/_files/customer.php
110
- * @magentoApiDataFixture Magento/Catalog/_files/product_simple .php
78
+ * @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_multiple_groups .php
111
79
*/
112
80
public function testSecondStoreViewWithCurrencyRate ()
113
81
{
114
- /** @var string $storeViewCode */
115
82
$ storeViewCode = 'fixture_second_store ' ;
116
- /** @var StoreRepositoryInterface $storeRepository */
117
83
$ storeRepository = $ this ->objectManager ->get (StoreRepositoryInterface::class);
118
- /** @var float $rate */
119
84
$ rate = $ storeRepository ->get ($ storeViewCode )->getCurrentCurrencyRate ();
120
- /** @var string $productSku */
121
85
$ productSku = 'simple ' ;
122
86
/** @var string $query */
123
87
$ query = $ this ->getProductSearchQuery ($ productSku );
124
88
/** @var array $headers */
125
89
$ headers = array_merge (
126
- $ this ->
getHeaderAuthorization (
'[email protected] ' ,
'password ' ),
90
+ $ this ->
getCustomerAuthenticationHeader -> execute (
'[email protected] ' ,
'password ' ),
127
91
$ this ->getHeaderStore ($ storeViewCode )
128
92
);
129
93
@@ -135,17 +99,20 @@ public function testSecondStoreViewWithCurrencyRate()
135
99
);
136
100
137
101
$ itemTiers = $ response ['products ' ]['items ' ][0 ]['price_tiers ' ];
138
- $ this ->assertEquals (2 , sizeof ($ itemTiers ));
139
- $ this ->assertEquals (round (8 * $ rate , 2 ), $ this ->getValueForQuantity (2 , $ itemTiers ));
140
- $ this ->assertEquals (round (5 * $ rate , 2 ), $ this ->getValueForQuantity (5 , $ itemTiers ));
102
+ $ this ->assertCount (3 , $ itemTiers );
103
+ $ this ->assertEquals (round (9.25 * $ rate , 2 ), $ this ->getValueForQuantity (2 , $ itemTiers ));
104
+ $ this ->assertEquals (round (8.25 * $ rate , 2 ), $ this ->getValueForQuantity (3 , $ itemTiers ));
105
+ $ this ->assertEquals (round (7.25 * $ rate , 2 ), $ this ->getValueForQuantity (5 , $ itemTiers ));
141
106
}
142
107
143
108
/**
109
+ * Get the tier price value for the given product quantity
110
+ *
144
111
* @param float $quantity
145
112
* @param array $tiers
146
113
* @return float
147
114
*/
148
- private function getValueForQuantity (float $ quantity , array $ tiers )
115
+ private function getValueForQuantity (float $ quantity , array $ tiers ): float
149
116
{
150
117
$ filteredResult = array_values (array_filter ($ tiers , function ($ tier ) use ($ quantity ) {
151
118
if ((float )$ tier ['quantity ' ] == $ quantity ) {
@@ -157,37 +124,16 @@ private function getValueForQuantity(float $quantity, array $tiers)
157
124
}
158
125
159
126
/**
160
- * @param ProductInterface $product
161
- * @param array $tierPriceData
162
- */
163
- private function saveTierPrices (ProductInterface $ product , array $ tierPriceData )
164
- {
165
- /** @var array $tierPrices */
166
- $ tierPrices = [];
167
- /** @var ProductTierPriceInterfaceFactory $tierPriceFactory */
168
- $ tierPriceFactory = $ this ->objectManager ->get (ProductTierPriceInterfaceFactory::class);
169
-
170
- foreach ($ tierPriceData as $ tierPrice ) {
171
- $ tierPrices [] = $ tierPriceFactory ->create (
172
- [
173
- 'data ' => $ tierPrice
174
- ]
175
- );
176
- }
177
-
178
- $ product ->setTierPrices ($ tierPrices );
179
- $ product ->save ();
180
- }
181
-
182
- /**
127
+ * Get a query which user filter for product sku and returns price_tiers
128
+ *
183
129
* @param string $productSku
184
130
* @return string
185
131
*/
186
132
private function getProductSearchQuery (string $ productSku ): string
187
133
{
188
134
return <<<QUERY
189
135
{
190
- products(search: " {$ productSku }") {
136
+ products(filter: {sku: {eq: " {$ productSku }"}} ) {
191
137
items {
192
138
price_tiers {
193
139
final_price {
@@ -207,19 +153,8 @@ private function getProductSearchQuery(string $productSku): string
207
153
}
208
154
209
155
/**
210
- * @param string $username
211
- * @param string $password
212
- * @return array
213
- */
214
- private function getHeaderAuthorization (string $ username , string $ password ): array
215
- {
216
- $ customerToken = $ this ->objectManager ->get (CustomerTokenServiceInterface::class)
217
- ->createCustomerAccessToken ($ username , $ password );
218
-
219
- return ['Authorization ' => 'Bearer ' . $ customerToken ];
220
- }
221
-
222
- /**
156
+ * Get array that would be used in request header
157
+ *
223
158
* @param string $storeViewCode
224
159
* @return array
225
160
*/
0 commit comments