77
88namespace Magento \GraphQl \CatalogCustomer ;
99
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 ;
1511use Magento \TestFramework \TestCase \GraphQlAbstract ;
1612use Magento \Store \Api \StoreRepositoryInterface ;
1713use Magento \TestFramework \Helper \Bootstrap ;
14+ use Magento \TestFramework \ObjectManager ;
1815
1916class PriceTiersTest extends GraphQlAbstract
2017{
2118 /**
22- * @var \Magento\TestFramework\ ObjectManager
19+ * @var ObjectManager
2320 */
2421 private $ objectManager ;
2522
23+ /**
24+ * @var GetCustomerAuthenticationHeader
25+ */
26+ private $ getCustomerAuthenticationHeader ;
27+
2628 protected function setUp (): void
2729 {
2830 $ this ->objectManager = Bootstrap::getObjectManager ();
31+ $ this ->getCustomerAuthenticationHeader = $ this ->objectManager ->get (GetCustomerAuthenticationHeader::class);
2932 }
3033
3134 /**
3235 * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
3336 */
3437 public function testAllGroups ()
3538 {
36- /** @var string $productSku */
3739 $ productSku = 'simple ' ;
3840 /** @var string $query */
3941 $ query = $ this ->getProductSearchQuery ($ productSku );
4042
4143 $ response = $ this ->graphQlQuery ($ query );
4244
4345 $ itemTiers = $ response ['products ' ]['items ' ][0 ]['price_tiers ' ];
44- $ this ->assertEquals (5 , sizeof ( $ itemTiers) );
46+ $ this ->assertCount (5 , $ itemTiers );
4547 $ this ->assertEquals (8 , $ this ->getValueForQuantity (2 , $ itemTiers ));
4648 $ this ->assertEquals (5 , $ this ->getValueForQuantity (3 , $ itemTiers ));
4749 $ this ->assertEquals (6 , $ this ->getValueForQuantity (3.2 , $ itemTiers ));
4850 }
4951
5052 /**
51- * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
5253 * @magentoApiDataFixture Magento/Customer/_files/customer.php
54+ * @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_multiple_groups.php
5355 */
5456 public function testLoggedInCustomer ()
5557 {
56- /** @var string $productSku */
5758 $ 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 );
9059 /** @var string $query */
91-
9260 $ query = $ this ->getProductSearchQuery ($ productSku );
9361 $ response = $ this ->graphQlQuery (
9462 $ query ,
9563 [],
9664 '' ,
97- $ this ->getHeaderAuthorization ('customer@example.com ' , 'password ' )
65+ $ this ->getCustomerAuthenticationHeader -> execute ('customer@example.com ' , 'password ' )
9866 );
9967
10068 $ 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 ));
10371 $ 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 ));
10573 }
10674
10775 /**
10876 * @magentoApiDataFixture Magento/Store/_files/second_store_with_second_currency.php
10977 * @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
11179 */
11280 public function testSecondStoreViewWithCurrencyRate ()
11381 {
114- /** @var string $storeViewCode */
11582 $ storeViewCode = 'fixture_second_store ' ;
116- /** @var StoreRepositoryInterface $storeRepository */
11783 $ storeRepository = $ this ->objectManager ->get (StoreRepositoryInterface::class);
118- /** @var float $rate */
11984 $ rate = $ storeRepository ->get ($ storeViewCode )->getCurrentCurrencyRate ();
120- /** @var string $productSku */
12185 $ productSku = 'simple ' ;
12286 /** @var string $query */
12387 $ query = $ this ->getProductSearchQuery ($ productSku );
12488 /** @var array $headers */
12589 $ headers = array_merge (
126- $ this ->getHeaderAuthorization ('customer@example.com ' , 'password ' ),
90+ $ this ->getCustomerAuthenticationHeader -> execute ('customer@example.com ' , 'password ' ),
12791 $ this ->getHeaderStore ($ storeViewCode )
12892 );
12993
@@ -135,17 +99,20 @@ public function testSecondStoreViewWithCurrencyRate()
13599 );
136100
137101 $ 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 ));
141106 }
142107
143108 /**
109+ * Get the tier price value for the given product quantity
110+ *
144111 * @param float $quantity
145112 * @param array $tiers
146113 * @return float
147114 */
148- private function getValueForQuantity (float $ quantity , array $ tiers )
115+ private function getValueForQuantity (float $ quantity , array $ tiers ): float
149116 {
150117 $ filteredResult = array_values (array_filter ($ tiers , function ($ tier ) use ($ quantity ) {
151118 if ((float )$ tier ['quantity ' ] == $ quantity ) {
@@ -157,37 +124,16 @@ private function getValueForQuantity(float $quantity, array $tiers)
157124 }
158125
159126 /**
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+ *
183129 * @param string $productSku
184130 * @return string
185131 */
186132 private function getProductSearchQuery (string $ productSku ): string
187133 {
188134 return <<<QUERY
189135{
190- products(search: " {$ productSku }") {
136+ products(filter: {sku: {eq: " {$ productSku }"}} ) {
191137 items {
192138 price_tiers {
193139 final_price {
@@ -207,19 +153,8 @@ private function getProductSearchQuery(string $productSku): string
207153 }
208154
209155 /**
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+ *
223158 * @param string $storeViewCode
224159 * @return array
225160 */
0 commit comments