Skip to content

Commit 58d8234

Browse files
author
Willem Wigman
committed
Refactor tests to use real (fixture) layout files instead of mocks
1 parent a834dc8 commit 58d8234

16 files changed

+191
-223
lines changed

tests/Integration/AbstractFrontendControllerTest.php

+68-22
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33

44
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
55

6-
use IntegerNet\GlobalCustomLayout\Test\src\CategoryLayoutUpdateManager;
7-
use IntegerNet\GlobalCustomLayout\Test\src\PageLayoutUpdateManager;
8-
use IntegerNet\GlobalCustomLayout\Test\src\ProductLayoutUpdateManager;
6+
use Magento\Framework\App\DeploymentConfig\Reader;
7+
use Magento\Framework\App\DeploymentConfig\Writer;
8+
use Magento\Framework\Config\File\ConfigFilePool;
9+
use Magento\Framework\Exception\FileSystemException;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\RuntimeException;
12+
use Magento\Framework\Module\Status as ModuleStatus;
913
use Magento\Framework\ObjectManagerInterface;
1014
use Magento\Framework\View\LayoutInterface;
1115
use Magento\TestFramework\Helper\Bootstrap;
@@ -14,50 +18,92 @@
1418
/**
1519
* @magentoAppIsolation enabled
1620
* @magentoAppArea frontend
21+
* @magentoComponentsDir ../../../../vendor/integer-net/magento2-global-custom-layout/tests/Integration/_files/app/code/IntegerNet
1722
*/
1823
abstract class AbstractFrontendControllerTest extends AbstractController
1924
{
2025
/** @var int */
2126
const STORE_ID = 0;
2227

23-
const TEST_FILE = 'test-file';
28+
/** @var string */
29+
const GLOBAL_TEST_FILE = 'globalfile';
30+
31+
/** @var string */
32+
const DEFAULT_TEST_FILE = 'defaultfile';
2433

2534
/** @var int */
2635
const GLOBAL_IDENTIFIER = 0;
2736

28-
/**
29-
* @var ObjectManagerInterface
30-
*/
37+
/** @var ObjectManagerInterface */
3138
protected $objectManager;
3239

33-
/**
34-
* @var LayoutInterface
35-
*/
36-
protected $layoutInterface;
40+
/** @var LayoutInterface */
41+
protected $layout;
42+
43+
/** @var ModuleStatus */
44+
protected $moduleStatus;
45+
46+
/** @var Reader */
47+
protected $configReader;
48+
49+
/** @var Writer */
50+
protected $configWriter;
51+
52+
/** @var array */
53+
protected $initialConfig;
3754

3855
/**
3956
* @inheritdoc
57+
* @throws LocalizedException
58+
* @magentoComponentsDir ../../../../vendor/integer-net/magento2-global-custom-layout/tests/Integration/_files/app/code/IntegerNet
4059
*/
4160
protected function setUp(): void
4261
{
62+
Bootstrap::getInstance()->reinitialize();
4363
$this->objectManager = Bootstrap::getObjectManager();
44-
$this->layoutInterface = $this->objectManager->get(LayoutInterface::class);
64+
$this->layout = $this->objectManager->get(LayoutInterface::class);
65+
$this->configWriter = $this->objectManager->get(Writer::class);
4566

46-
$this->setUpPreferences();
67+
$this->backupInitialConfig();
68+
$this->enableTestModuleInConfig();
4769

4870
parent::setUp();
4971
}
5072

51-
private function setUpPreferences(): void
73+
protected function enableTestModuleInConfig()
74+
{
75+
$this->moduleStatus = $this->objectManager->create(ModuleStatus::class);
76+
$this->moduleStatus->setIsEnabled(true, ['IntegerNet_GlobalCustomLayoutTest']);
77+
}
78+
79+
/**
80+
* @throws FileSystemException
81+
*/
82+
protected function tearDown()
83+
{
84+
$this->restoreInitialConfig();
85+
}
86+
87+
/**
88+
* @throws FileSystemException
89+
*/
90+
protected function restoreInitialConfig(): void
5291
{
53-
$this->objectManager->configure(
54-
[
55-
'preferences' => [
56-
\Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
57-
\Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager::class => ProductLayoutUpdateManager::class,
58-
\Magento\Cms\Model\Page\CustomLayoutManagerInterface::class => PageLayoutUpdateManager::class,
59-
],
60-
]
92+
$this->configWriter->saveConfig(
93+
[ConfigFilePool::APP_CONFIG => ['modules' => $this->initialConfig['modules']]],
94+
true
6195
);
6296
}
97+
98+
/**
99+
* @throws FileSystemException
100+
* @throws RuntimeException
101+
*/
102+
protected function backupInitialConfig(): void
103+
{
104+
if (!$this->initialConfig) {
105+
$this->configReader = $this->objectManager->get(Reader::class);
106+
$this->initialConfig = $this->configReader->load();
107+
}
108+
}
63109
}

tests/Integration/CategoryFrontendControllerTest.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
55

6-
use IntegerNet\GlobalCustomLayout\Test\src\CategoryLayoutUpdateManager;
76
use Magento\Catalog\Api\CategoryRepositoryInterface;
87
use Magento\Catalog\Api\Data\CategoryInterface;
98
use Magento\Framework\Exception\CouldNotSaveException;
@@ -12,26 +11,29 @@
1211
/**
1312
* Tests whether global layout handles are correctly saved on categories
1413
* and retrieved on the frontend on Category views
14+
*
15+
* @magentoAppIsolation enabled
16+
* @magentoAppArea frontend
17+
* @magentoComponentsDir ../../../../vendor/integer-net/magento2-global-custom-layout/tests/Integration/_files/app/code/IntegerNet
1518
*/
1619
class CategoryFrontendControllerTest extends AbstractFrontendControllerTest
1720
{
1821
/** @var int */
1922
const CATEGORY_ID_FROM_FIXTURE = 5;
2023

24+
/** @var string */
25+
const DEFAULT_TEST_FILE = 'defaultfile';
26+
2127
/** @var CategoryRepositoryInterface $repository */
2228
protected $repository;
2329

24-
/** @var CategoryLayoutUpdateManager $layoutManager */
25-
protected $layoutManager;
26-
2730
/** @var CategoryInterface $category */
2831
protected $category;
2932

3033
protected function setUp(): void
3134
{
3235
parent::setUp();
3336

34-
$this->layoutManager = $this->objectManager->get(CategoryLayoutUpdateManager::class);
3537
$this->repository = $this->objectManager->create(CategoryRepositoryInterface::class);
3638
}
3739

@@ -82,7 +84,7 @@ protected function givenGlobalCustomUpdateSelected()
8284
*/
8385
protected function givenDefaultCustomUpdateSelected()
8486
{
85-
$this->setCustomUpdate(self::CATEGORY_ID_FROM_FIXTURE);
87+
$this->setCustomUpdate(self::CATEGORY_ID_FROM_FIXTURE, self::DEFAULT_TEST_FILE);
8688
}
8789

8890
/**
@@ -91,13 +93,9 @@ protected function givenDefaultCustomUpdateSelected()
9193
* @throws CouldNotSaveException
9294
* @throws NoSuchEntityException
9395
*/
94-
protected function setCustomUpdate(int $forCategoryId, string $fileName = self::TEST_FILE)
96+
protected function setCustomUpdate(int $forCategoryId, string $fileName = self::GLOBAL_TEST_FILE)
9597
{
9698
$category = $this->getCategory();
97-
98-
$this->layoutManager->setFakeFiles($forCategoryId, [$fileName]);
99-
100-
//Updating the custom attribute.
10199
$category->setCustomAttribute('custom_layout_update_file', $fileName);
102100
$this->repository->save($category);
103101
}
@@ -117,12 +115,12 @@ protected function whenCategoryViewed(?int $categoryId = null)
117115

118116
protected function thenContainsGlobalUpdateHandle()
119117
{
120-
$this->containsUpdateHandle(self::GLOBAL_IDENTIFIER, self::TEST_FILE);
118+
$this->containsUpdateHandle(self::GLOBAL_IDENTIFIER, self::GLOBAL_TEST_FILE);
121119
}
122120

123121
protected function thenContainsDefaultUpdateHandle()
124122
{
125-
$this->containsUpdateHandle(self::CATEGORY_ID_FROM_FIXTURE, self::TEST_FILE);
123+
$this->containsUpdateHandle(self::CATEGORY_ID_FROM_FIXTURE, self::DEFAULT_TEST_FILE);
126124
}
127125

128126
/**
@@ -133,11 +131,11 @@ protected function thenContainsDefaultUpdateHandle()
133131
*/
134132
protected function containsUpdateHandle(
135133
$identifier = self::GLOBAL_IDENTIFIER,
136-
string $fileName = self::TEST_FILE)
134+
string $fileName = self::GLOBAL_TEST_FILE)
137135
{
138136
$expectedHandle = "catalog_category_view_selectable_{$identifier}_{$fileName}";
139137

140-
$handles = $this->layoutInterface->getUpdate()->getHandles();
138+
$handles = $this->layout->getUpdate()->getHandles();
141139
$this->assertContains($expectedHandle, $handles);
142140
}
143141

tests/Integration/ModuleTest.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
use Magento\Framework\Module\ModuleList;
99
use PHPUnit\Framework\TestCase;
1010

11+
/**
12+
* @magentoAppIsolation enabled
13+
* @magentoAppArea frontend
14+
* @magentoComponentsDir ../../../../vendor/integer-net/magento2-global-custom-layout/tests/Integration/_files/app/code/IntegerNet
15+
*/
1116
class ModuleTest extends TestCase
1217
{
1318
private const MODULE_NAME = 'IntegerNet_GlobalCustomLayout';
14-
19+
private const TEST_MODULE_NAME = 'IntegerNet_GlobalCustomLayoutTest';
1520
/**
1621
* @var ObjectManager
1722
*/
@@ -29,6 +34,13 @@ public function testModuleIsRegistered()
2934
$this->assertArrayHasKey(self::MODULE_NAME, $paths);
3035
}
3136

37+
public function testTestModuleIsRegistered()
38+
{
39+
$registrar = new ComponentRegistrar();
40+
$paths = $registrar->getPaths(ComponentRegistrar::MODULE);
41+
$this->assertArrayHasKey(self::TEST_MODULE_NAME, $paths);
42+
}
43+
3244
public function testModuleIsActive()
3345
{
3446
/** @var ModuleList $moduleList */

tests/Integration/PageFrontendControllerTest.php

+14-18
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33

44
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
55

6-
use IntegerNet\GlobalCustomLayout\Test\src\PageLayoutUpdateManager;
76
use Magento\Cms\Model\Page;
87
use Magento\Cms\Model\Page\CustomLayoutRepositoryInterface;
98
use Magento\Cms\Model\PageFactory;
109
use Magento\Cms\Model\ResourceModel\Page as PageResourceModel;
1110

11+
/**
12+
* Tests whether global layout handles are correctly saved on CMS Pages
13+
* and retrieved on the frontend on Page views
14+
*
15+
* @magentoAppIsolation enabled
16+
* @magentoAppArea frontend
17+
* @magentoComponentsDir ../../../../vendor/integer-net/magento2-global-custom-layout/tests/Integration/_files/app/code/IntegerNet
18+
*/
1219
class PageFrontendControllerTest extends AbstractFrontendControllerTest
1320
{
1421
/** @var string */
1522
const PAGE_ID_FROM_FIXTURE = 'page100';
1623

17-
/** @var PageLayoutUpdateManager */
18-
protected $layoutManager;
19-
2024
/** @var Page */
2125
protected $page;
2226

@@ -33,13 +37,8 @@ protected function setUp(): void
3337
{
3438
parent::setUp();
3539

36-
$this->layoutManager = $this->objectManager->get(PageLayoutUpdateManager::class);
3740
$this->pageResource = $this->objectManager->get(PageResourceModel::class);
3841
$this->pageFactory = $this->objectManager->get(PageFactory::class);
39-
$this->repository = $this->objectManager->create(
40-
CustomLayoutRepositoryInterface::class,
41-
['manager' => $this->layoutManager]
42-
);
4342
}
4443

4544
/**
@@ -75,7 +74,7 @@ protected function givenGlobalCustomUpdateSelected()
7574

7675
protected function givenDefaultCustomUpdateSelected()
7776
{
78-
$this->setCustomUpdate($this->getPageId());
77+
$this->setCustomUpdate($this->getPageId(), self::DEFAULT_TEST_FILE);
7978
}
8079

8180
/**
@@ -98,7 +97,7 @@ protected function thenContainsGlobalUpdateHandle()
9897

9998
protected function thenContainsDefaultUpdateHandle()
10099
{
101-
$this->containsUpdateHandle(self::PAGE_ID_FROM_FIXTURE);
100+
$this->containsUpdateHandle(self::PAGE_ID_FROM_FIXTURE, self::DEFAULT_TEST_FILE);
102101
}
103102

104103
/**
@@ -109,20 +108,17 @@ protected function thenContainsDefaultUpdateHandle()
109108
*/
110109
protected function containsUpdateHandle(
111110
$identifier = self::GLOBAL_IDENTIFIER,
112-
string $fileName = self::TEST_FILE)
111+
string $fileName = self::GLOBAL_TEST_FILE)
113112
{
114113
$expectedHandle = "cms_page_view_selectable_{$identifier}_{$fileName}";
115114

116-
$handles = $this->layoutInterface->getUpdate()->getHandles();
115+
$handles = $this->layout->getUpdate()->getHandles();
117116
$this->assertContains($expectedHandle, $handles);
118117
}
119118

120-
protected function setCustomUpdate(int $forPageId, string $fileName = self::TEST_FILE)
119+
protected function setCustomUpdate(int $forPageId, string $fileName = self::GLOBAL_TEST_FILE)
121120
{
122121
$page = $this->getPage();
123-
124-
$this->layoutManager->setFakeFiles($forPageId, [$fileName]);
125-
126122
$page->setData('layout_update_selected', $fileName);
127123
$this->pageResource->save($page);
128124
}
@@ -133,7 +129,7 @@ protected function setCustomUpdate(int $forPageId, string $fileName = self::TEST
133129
*/
134130
protected function createPage(?string $pageIdentifier = self::PAGE_ID_FROM_FIXTURE): Page
135131
{
136-
$page = $this->pageFactory->create(['customLayoutRepository' => $this->repository]);
132+
$page = $this->pageFactory->create();
137133
$page->setStoreId(self::STORE_ID);
138134
$page->load($pageIdentifier, Page::IDENTIFIER);
139135

0 commit comments

Comments
 (0)