4
4
* See COPYING.txt for license details.
5
5
*/
6
6
declare (strict_types=1 );
7
-
8
7
namespace Magento \GraphQl \Catalog ;
9
-
10
8
use Magento \Catalog \Api \Data \CategoryInterface ;
11
9
use Magento \Catalog \Model \ResourceModel \Category \Collection as CategoryCollection ;
12
10
use Magento \Framework \DataObject ;
13
11
use Magento \TestFramework \TestCase \GraphQlAbstract ;
14
12
use Magento \Catalog \Api \Data \ProductInterface ;
15
13
use Magento \Catalog \Api \ProductRepositoryInterface ;
16
14
use Magento \TestFramework \ObjectManager ;
17
-
18
15
class CategoryTest extends GraphQlAbstract
19
16
{
20
17
/**
21
18
* @var \Magento\TestFramework\ObjectManager
22
19
*/
23
20
private $ objectManager ;
24
-
25
21
protected function setUp ()
26
22
{
27
23
$ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
28
24
}
29
-
30
25
/**
31
26
* @magentoApiDataFixture Magento/Customer/_files/customer.php
32
27
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
@@ -70,21 +65,19 @@ public function testCategoriesTree()
70
65
}
71
66
}
72
67
QUERY ;
73
-
74
68
// get customer ID token
75
69
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
76
70
$ customerTokenService = $ this ->objectManager ->create (
77
71
\Magento \Integration \Api \CustomerTokenServiceInterface::class
78
72
);
79
73
$ customerToken =
$ customerTokenService->
createCustomerAccessToken (
'[email protected] ' ,
'password ' );
80
-
81
74
$ headerMap = ['Authorization ' => 'Bearer ' . $ customerToken ];
82
75
$ response = $ this ->graphQlQuery ($ query , [], '' , $ headerMap );
83
76
$ responseDataObject = new DataObject ($ response );
84
77
//Some sort of smoke testing
85
78
self ::assertEquals (
86
- 'Ololo ' ,
87
- $ responseDataObject ->getData ('category/children/7 /children/1/description ' )
79
+ 'Its a description of Test Category 1.2 ' ,
80
+ $ responseDataObject ->getData ('category/children/0 /children/1/description ' )
88
81
);
89
82
self ::assertEquals (
90
83
'default-category ' ,
@@ -99,19 +92,53 @@ public function testCategoriesTree()
99
92
$ responseDataObject ->getData ('category/children/0/default_sort_by ' )
100
93
);
101
94
self ::assertCount (
102
- 8 ,
95
+ 7 ,
103
96
$ responseDataObject ->getData ('category/children ' )
104
97
);
105
98
self ::assertCount (
106
99
2 ,
107
- $ responseDataObject ->getData ('category/children/7 /children ' )
100
+ $ responseDataObject ->getData ('category/children/0 /children ' )
108
101
);
109
102
self ::assertEquals (
110
- 5 ,
111
- $ responseDataObject ->getData ('category/children/7/children/1/children/0/id ' )
103
+ 13 ,
104
+ $ responseDataObject ->getData ('category/children/0/children/1/id ' )
105
+ );
106
+ }
107
+ /**
108
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
109
+ * @magentoApiDataFixture Magento/Catalog/_files/categories.php
110
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
111
+ */
112
+ public function testGetCategoryById ()
113
+ {
114
+ $ rootCategoryId = 13 ;
115
+ $ query = <<<QUERY
116
+ {
117
+ category(id: {$ rootCategoryId }) {
118
+ id
119
+ name
120
+ }
121
+ }
122
+ QUERY ;
123
+ // get customer ID token
124
+ /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
125
+ $ customerTokenService = $ this ->objectManager ->create (
126
+ \Magento \Integration \Api \CustomerTokenServiceInterface::class
127
+ );
128
+ $ customerToken =
$ customerTokenService->
createCustomerAccessToken (
'[email protected] ' ,
'password ' );
129
+ $ headerMap = ['Authorization ' => 'Bearer ' . $ customerToken ];
130
+ $ response = $ this ->graphQlQuery ($ query , [], '' , $ headerMap );
131
+ $ responseDataObject = new DataObject ($ response );
132
+ //Some sort of smoke testing
133
+ self ::assertEquals (
134
+ 'Category 1.2 ' ,
135
+ $ responseDataObject ->getData ('category/name ' )
136
+ );
137
+ self ::assertEquals (
138
+ 13 ,
139
+ $ responseDataObject ->getData ('category/id ' )
112
140
);
113
141
}
114
-
115
142
/**
116
143
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
117
144
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -259,17 +286,14 @@ public function testCategoryProducts()
259
286
}
260
287
}
261
288
QUERY ;
262
-
263
289
$ response = $ this ->graphQlQuery ($ query );
264
290
$ this ->assertArrayHasKey ('products ' , $ response ['category ' ]);
265
291
$ this ->assertArrayHasKey ('total_count ' , $ response ['category ' ]['products ' ]);
266
292
$ this ->assertGreaterThanOrEqual (1 , $ response ['category ' ]['products ' ]['total_count ' ]);
267
293
$ this ->assertEquals (1 , $ response ['category ' ]['products ' ]['page_info ' ]['current_page ' ]);
268
294
$ this ->assertEquals (20 , $ response ['category ' ]['products ' ]['page_info ' ]['page_size ' ]);
269
-
270
295
$ this ->assertArrayHasKey ('sku ' , $ response ['category ' ]['products ' ]['items ' ][0 ]);
271
296
$ firstProductSku = $ response ['category ' ]['products ' ]['items ' ][0 ]['sku ' ];
272
-
273
297
/**
274
298
* @var ProductRepositoryInterface $productRepository
275
299
*/
@@ -279,7 +303,6 @@ public function testCategoryProducts()
279
303
$ this ->assertAttributes ($ response ['category ' ]['products ' ]['items ' ][0 ]);
280
304
$ this ->assertWebsites ($ firstProduct , $ response ['category ' ]['products ' ]['items ' ][0 ]['websites ' ]);
281
305
}
282
-
283
306
/**
284
307
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
285
308
*/
@@ -291,9 +314,7 @@ public function testAnchorCategory()
291
314
/** @var CategoryInterface $category */
292
315
$ category = $ categoryCollection ->getFirstItem ();
293
316
$ categoryId = $ category ->getId ();
294
-
295
317
$ this ->assertNotEmpty ($ categoryId , "Preconditions failed: category is not available. " );
296
-
297
318
$ query = <<<QUERY
298
319
{
299
320
category(id: {$ categoryId }) {
@@ -307,7 +328,6 @@ public function testAnchorCategory()
307
328
}
308
329
}
309
330
QUERY ;
310
-
311
331
$ response = $ this ->graphQlQuery ($ query );
312
332
$ expectedResponse = [
313
333
'category ' => [
@@ -324,14 +344,12 @@ public function testAnchorCategory()
324
344
];
325
345
$ this ->assertEquals ($ expectedResponse , $ response );
326
346
}
327
-
328
347
/**
329
348
* @param ProductInterface $product
330
349
* @param array $actualResponse
331
350
*/
332
351
private function assertBaseFields ($ product , $ actualResponse )
333
352
{
334
-
335
353
$ assertionMap = [
336
354
['response_field ' => 'attribute_set_id ' , 'expected_value ' => $ product ->getAttributeSetId ()],
337
355
['response_field ' => 'created_at ' , 'expected_value ' => $ product ->getCreatedAt ()],
@@ -365,10 +383,8 @@ private function assertBaseFields($product, $actualResponse)
365
383
['response_field ' => 'type_id ' , 'expected_value ' => $ product ->getTypeId ()],
366
384
['response_field ' => 'updated_at ' , 'expected_value ' => $ product ->getUpdatedAt ()],
367
385
];
368
-
369
386
$ this ->assertResponseFields ($ actualResponse , $ assertionMap );
370
387
}
371
-
372
388
/**
373
389
* @param ProductInterface $product
374
390
* @param array $actualResponse
@@ -385,10 +401,8 @@ private function assertWebsites($product, $actualResponse)
385
401
'is_default ' => true ,
386
402
]
387
403
];
388
-
389
404
$ this ->assertEquals ($ actualResponse , $ assertionMap );
390
405
}
391
-
392
406
/**
393
407
* @param array $actualResponse
394
408
*/
@@ -410,9 +424,8 @@ private function assertAttributes($actualResponse)
410
424
'special_from_date ' ,
411
425
'special_to_date ' ,
412
426
];
413
-
414
427
foreach ($ eavAttributes as $ eavAttribute ) {
415
428
$ this ->assertArrayHasKey ($ eavAttribute , $ actualResponse );
416
429
}
417
430
}
418
- }
431
+ }
0 commit comments