Skip to content

Commit f34f015

Browse files
TomashKhamlaiValeriy Nayda
authored and
Valeriy Nayda
committed
Test coverage for added breadcrumbs support #158
1 parent 06267e6 commit f34f015

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Catalog;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\GraphQlAbstract;
16+
17+
/**
18+
* Covers breadcrumbs support by GraphQl
19+
*/
20+
class BreadcrumbsTest extends GraphQlAbstract
21+
{
22+
/**
23+
* Verify the fields of CMS Block selected by identifiers.
24+
*
25+
* @magentoApiDataFixture Magento/Catalog/_files/category_tree.php
26+
* @return void
27+
*/
28+
public function testGetBreadcrumbs(): void
29+
{
30+
$categoryCollection = ObjectManager::getInstance()->get(CollectionFactory::class)->create();
31+
$categoryCollection->addAttributeToFilter('name', ['eq' => 'Category 1.1.1']);
32+
$selectedCategoryId = (int)$categoryCollection->getFirstItem()->getId();
33+
$query =
34+
<<<QUERY
35+
{
36+
category(id: $selectedCategoryId) {
37+
name
38+
breadcrumbs {
39+
category_id
40+
category_name
41+
category_level
42+
category_url_key
43+
}
44+
}
45+
}
46+
QUERY;
47+
48+
$response = $this->graphQlQuery($query);
49+
$this->assertArrayHasKey('category', $response);
50+
$this->assertArrayHasKey('breadcrumbs', $response['category']);
51+
$this->assertBaseFields($selectedCategoryId, $response);
52+
}
53+
54+
/**
55+
* Verify the fields of CMS Block selected by identifiers.
56+
*
57+
* @magentoApiDataFixture Magento/Catalog/_files/category_tree.php
58+
* @return void
59+
*/
60+
public function testGetBreadcrumbsForNonExistingCategory(): void
61+
{
62+
$query =
63+
<<<QUERY
64+
{
65+
category(id: 0) {
66+
name
67+
breadcrumbs {
68+
category_id
69+
category_name
70+
category_level
71+
category_url_key
72+
}
73+
}
74+
}
75+
QUERY;
76+
77+
$response = $this->graphQlQuery($query);
78+
$this->assertArrayHasKey('category', $response);
79+
$this->assertNull(
80+
$response['category'],
81+
'Value of "category" field must be NULL if requested category doesn\'t exist'
82+
);
83+
$this->assertCount(
84+
1,
85+
$response,
86+
'There should be only "category" field if requested category doesn\'t exist '
87+
);
88+
}
89+
90+
/**
91+
* Asserts the equality of the response fields to the fields given in assertion map.
92+
* Assert that values of the fields are different from NULL
93+
*
94+
* @param array $actualResponse
95+
* @param array $assertionMap
96+
* @return void
97+
*/
98+
private function assertResponseFields(array $actualResponse, array $assertionMap): void
99+
{
100+
foreach ($assertionMap as $key => $assertionData) {
101+
$expectedValue = isset($assertionData['expected_value'])
102+
? $assertionData['expected_value']
103+
: $assertionData;
104+
$responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key;
105+
$this->assertNotNull(
106+
$expectedValue,
107+
"Value of '{$responseField}' field must not be NULL"
108+
);
109+
$this->assertEquals(
110+
$expectedValue[$key],
111+
$actualResponse[$responseField],
112+
"Value of '{$responseField}' field in response does not match expected value: "
113+
. var_export($expectedValue, true)
114+
);
115+
}
116+
}
117+
118+
/**
119+
* Get breadcrumbs for given category.
120+
*
121+
* @param Category $category
122+
* @return array
123+
*/
124+
private function getBreadcrumbs(Category $category): array
125+
{
126+
$breadcrumbs = [];
127+
$rootId = Bootstrap::getObjectManager()->get(StoreManagerInterface::class)
128+
->getStore()
129+
->getRootCategoryId();
130+
foreach ($category->getParentCategories() as $parentCategory) {
131+
if ($parentCategory->getId() !== $rootId) {
132+
$breadcrumbs[] = [
133+
'category_id' => $parentCategory->getId(),
134+
'category_name' => $parentCategory->getName(),
135+
'category_level' => $parentCategory->getLevel(),
136+
'category_url_key' => $parentCategory->getUrlKey(),
137+
];
138+
}
139+
}
140+
141+
return $breadcrumbs;
142+
}
143+
144+
/**
145+
* Asserts base fields
146+
*
147+
* @param int $categoryId
148+
* @param array $actualResponse
149+
* @return void
150+
*/
151+
private function assertBaseFields(int $categoryId, array $actualResponse): void
152+
{
153+
$category = Bootstrap::getObjectManager()->create(Category::class)->load($categoryId);
154+
$assertionMap = [
155+
[
156+
'response_field' => 'category',
157+
'expected_value' =>
158+
[
159+
[
160+
'name' => $category->getName(),
161+
'breadcrumbs' => $this->getBreadcrumbs($category->getParentCategory()),
162+
],
163+
164+
],
165+
],
166+
];
167+
168+
/**
169+
* @param array $actualResponse
170+
* @param array $assertionMap ['response_field_name' => 'response_field_value', ...]
171+
* OR [['response_field' => $field, 'expected_value' => $value], ...]
172+
*/
173+
$this->assertResponseFields($actualResponse, $assertionMap);
174+
}
175+
}

0 commit comments

Comments
 (0)