Skip to content

Commit 72eca03

Browse files
author
Willem Wigman
committed
#4 Refactor Tests
1 parent fafb5a6 commit 72eca03

8 files changed

+457
-164
lines changed

tests/Integration/AbstractFrontendControllerTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
55

66
use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
7-
use IntegerNet\GlobalCustomLayout\Test\Util\CustomLayoutManager;
7+
use IntegerNet\GlobalCustomLayout\Test\Util\PageLayoutUpdateManager;
88
use IntegerNet\GlobalCustomLayout\Test\Util\ProductLayoutUpdateManager;
99
use Magento\Framework\ObjectManagerInterface;
1010
use Magento\Framework\View\LayoutInterface;
@@ -17,6 +17,14 @@
1717
*/
1818
abstract class AbstractFrontendControllerTest extends AbstractController
1919
{
20+
/** @var int */
21+
const STORE_ID = 0;
22+
23+
const TEST_FILE = 'test-file';
24+
25+
/** @var int */
26+
const GLOBAL_IDENTIFIER = 0;
27+
2028
/**
2129
* @var ObjectManagerInterface
2230
*/
@@ -47,8 +55,8 @@ private function setUpPreferences(): void
4755
'preferences' => [
4856
\Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
4957
\Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager::class => ProductLayoutUpdateManager::class,
50-
\Magento\Cms\Model\Page\CustomLayoutManagerInterface::class => CustomLayoutManager::class,
51-
]
58+
\Magento\Cms\Model\Page\CustomLayoutManagerInterface::class => PageLayoutUpdateManager::class,
59+
],
5260
]
5361
);
5462
}

tests/Integration/CategoryFrontendControllerTest.php

Lines changed: 122 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
77
use Magento\Catalog\Api\CategoryRepositoryInterface;
8+
use Magento\Catalog\Api\Data\CategoryInterface;
89
use Magento\Framework\Exception\CouldNotSaveException;
910
use Magento\Framework\Exception\NoSuchEntityException;
1011

@@ -14,6 +15,26 @@
1415
*/
1516
class CategoryFrontendControllerTest extends AbstractFrontendControllerTest
1617
{
18+
/** @var int */
19+
const CATEGORY_ID = 5;
20+
21+
/** @var CategoryRepositoryInterface $repository */
22+
protected $repository;
23+
24+
/** @var CategoryLayoutUpdateManager $layoutManager */
25+
protected $layoutManager;
26+
27+
/** @var CategoryInterface $category */
28+
protected $category;
29+
30+
protected function setUp()
31+
{
32+
parent::setUp();
33+
34+
$this->layoutManager = $this->objectManager->get(CategoryLayoutUpdateManager::class);
35+
$this->repository = $this->objectManager->create(CategoryRepositoryInterface::class);
36+
}
37+
1738
/**
1839
* Check that Global Custom Layout Update files work for Category views.
1940
*
@@ -23,31 +44,113 @@ class CategoryFrontendControllerTest extends AbstractFrontendControllerTest
2344
*
2445
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
2546
*/
26-
public function testViewWithGlobalCustomUpdate(): void
47+
public function testViewContainsGlobalCustomUpdate(): void
48+
{
49+
$this->givenGlobalCustomUpdateSelected();
50+
$this->whenCategoryViewed();
51+
$this->thenContainsGlobalUpdateHandle();
52+
}
53+
54+
/**
55+
* Check that Default Custom Layout Update files still work for Category views.
56+
*
57+
* @return void
58+
* @throws CouldNotSaveException
59+
* @throws NoSuchEntityException
60+
*
61+
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
62+
*/
63+
public function testViewContainsDefaultCustomUpdate(): void
64+
{
65+
$this->givenDefaultCustomUpdateSelected();
66+
$this->whenCategoryViewed();
67+
$this->thenContainsDefaultUpdateHandle();
68+
}
69+
70+
/**
71+
* @throws CouldNotSaveException
72+
* @throws NoSuchEntityException
73+
*/
74+
protected function givenGlobalCustomUpdateSelected()
75+
{
76+
$this->setCustomUpdate(self::GLOBAL_IDENTIFIER);
77+
}
78+
79+
/**
80+
* @throws CouldNotSaveException
81+
* @throws NoSuchEntityException
82+
*/
83+
protected function givenDefaultCustomUpdateSelected()
2784
{
28-
//Setting a fake file for the category.
29-
$file = 'test-file';
30-
$categoryId = 5;
85+
$this->setCustomUpdate(self::CATEGORY_ID);
86+
}
3187

32-
/** @var CategoryLayoutUpdateManager $layoutManager */
33-
$layoutManager = $this->objectManager->get(CategoryLayoutUpdateManager::class);
34-
$layoutManager->setCategoryFakeFiles(0, [$file]);
88+
/**
89+
* @param int $forCategoryId
90+
* @param string $fileName
91+
* @throws CouldNotSaveException
92+
* @throws NoSuchEntityException
93+
*/
94+
protected function setCustomUpdate(int $forCategoryId, string $fileName = self::TEST_FILE)
95+
{
96+
$category = $this->getCategory();
3597

36-
/** @var CategoryRepositoryInterface $categoryRepo */
37-
$categoryRepo = $this->objectManager->create(CategoryRepositoryInterface::class);
38-
$category = $categoryRepo->get($categoryId);
98+
$this->layoutManager->setFakeFiles($forCategoryId, [$fileName]);
3999

40100
//Updating the custom attribute.
41-
$category->setCustomAttribute('custom_layout_update_file', $file);
42-
$categoryRepo->save($category);
101+
$category->setCustomAttribute('custom_layout_update_file', $fileName);
102+
$this->repository->save($category);
103+
}
43104

44-
//Viewing the category
45-
$this->dispatch("catalog/category/view/id/$categoryId");
105+
/**
106+
* Viewing the category
107+
*
108+
* @param int $categoryId
109+
*/
110+
protected function whenCategoryViewed(?int $categoryId = null)
111+
{
112+
if (!$categoryId) {
113+
$categoryId = self::CATEGORY_ID;
114+
}
115+
$this->dispatch("catalog/category/view/id/{$categoryId}");
116+
}
46117

47-
//Layout handles must contain the file.
48-
$handles = $this->layoutInterface
49-
->getUpdate()
50-
->getHandles();
51-
$this->assertContains("catalog_category_view_selectable_0_{$file}", $handles);
118+
protected function thenContainsGlobalUpdateHandle()
119+
{
120+
$this->containsUpdateHandle(self::GLOBAL_IDENTIFIER, self::TEST_FILE);
121+
}
122+
123+
protected function thenContainsDefaultUpdateHandle()
124+
{
125+
$this->containsUpdateHandle(self::CATEGORY_ID, self::TEST_FILE);
126+
}
127+
128+
/**
129+
* Layout handles must contain the file.
130+
*
131+
* @param int|string $identifier
132+
* @param string $fileName
133+
*/
134+
protected function containsUpdateHandle(
135+
$identifier = self::GLOBAL_IDENTIFIER,
136+
string $fileName = self::TEST_FILE)
137+
{
138+
$expectedHandle = "catalog_category_view_selectable_{$identifier}_{$fileName}";
139+
140+
$handles = $this->layoutInterface->getUpdate()->getHandles();
141+
$this->assertContains($expectedHandle, $handles);
142+
}
143+
144+
/**
145+
* @param int $categoryId
146+
* @return CategoryInterface
147+
* @throws NoSuchEntityException
148+
*/
149+
protected function getCategory(int $categoryId = self::CATEGORY_ID): CategoryInterface
150+
{
151+
if (!$this->category) {
152+
$this->category = $this->repository->get($categoryId);
153+
}
154+
return $this->category;
52155
}
53156
}

0 commit comments

Comments
 (0)