Completed
Push — master ( 1c4fa3...04dec7 )
by Willem
24s queued 11s
created

PageFrontendControllerTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
eloc 42
c 1
b 0
f 1
dl 0
loc 148
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setCustomUpdate() 0 8 1
A testViewWithGlobalCustomUpdate() 0 5 1
A containsUpdateHandle() 0 8 1
A thenContainsGlobalUpdateHandle() 0 3 1
A createPage() 0 7 1
A whenPageViewed() 0 6 2
A testViewWithDefaultCustomUpdate() 0 5 1
A getPageId() 0 3 1
A givenDefaultCustomUpdateSelected() 0 3 1
A thenContainsDefaultUpdateHandle() 0 3 1
A getPage() 0 6 3
A givenGlobalCustomUpdateSelected() 0 3 1
A setUp() 0 10 1
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
5
6
use IntegerNet\GlobalCustomLayout\Test\src\PageLayoutUpdateManager;
7
use Magento\Cms\Model\Page;
8
use Magento\Cms\Model\Page\CustomLayoutRepositoryInterface;
9
use Magento\Cms\Model\PageFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Cms\Model\PageFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Magento\Cms\Model\ResourceModel\Page as PageResourceModel;
11
12
class PageFrontendControllerTest extends AbstractFrontendControllerTest
13
{
14
    /** @var string */
15
    const PAGE_ID_FROM_FIXTURE = 'page100';
16
17
    /** @var PageLayoutUpdateManager */
18
    protected $layoutManager;
19
20
    /** @var Page */
21
    protected $page;
22
23
    /** @var PageResourceModel $pageResource */
24
    protected $pageResource;
25
26
    /** @var pageFactory $pageFactory */
27
    protected $pageFactory;
28
29
    /** @var CustomLayoutRepositoryInterface $repository */
30
    protected $repository;
31
32
    protected function setUp()
33
    {
34
        parent::setUp();
35
36
        $this->layoutManager = $this->objectManager->get(PageLayoutUpdateManager::class);
37
        $this->pageResource = $this->objectManager->get(PageResourceModel::class);
38
        $this->pageFactory = $this->objectManager->get(PageFactory::class);
39
        $this->repository = $this->objectManager->create(
40
            CustomLayoutRepositoryInterface::class,
41
            ['manager' => $this->layoutManager]
42
        );
43
    }
44
45
    /**
46
     * Check that custom handles are applied when rendering a page.
47
     *
48
     * @return void
49
     * @magentoDataFixture Magento/Cms/_files/pages.php
50
     */
51
    public function testViewWithGlobalCustomUpdate(): void
52
    {
53
        $this->givenGlobalCustomUpdateSelected();
54
        $this->whenPageViewed();
55
        $this->thenContainsGlobalUpdateHandle();
56
    }
57
58
    /**
59
     * Check that custom handles are applied when rendering a page.
60
     *
61
     * @return void
62
     * @magentoDataFixture Magento/Cms/_files/pages.php
63
     */
64
    public function testViewWithDefaultCustomUpdate(): void
65
    {
66
        $this->givenDefaultCustomUpdateSelected();
67
        $this->whenPageViewed();
68
        $this->thenContainsDefaultUpdateHandle();
69
    }
70
71
    protected function givenGlobalCustomUpdateSelected()
72
    {
73
        $this->setCustomUpdate(self::GLOBAL_IDENTIFIER);
74
    }
75
76
    protected function givenDefaultCustomUpdateSelected()
77
    {
78
        $this->setCustomUpdate($this->getPageId());
79
    }
80
81
    /**
82
     * Viewing the product
83
     *
84
     * @param int|null $pageId
85
     */
86
    protected function whenPageViewed(?int $pageId = null): void
87
    {
88
        if (!$pageId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pageId of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
89
            $pageId = $this->getPageId();
90
        }
91
        $this->dispatch("/cms/page/view/page_id/{$pageId}");
92
    }
93
94
    protected function thenContainsGlobalUpdateHandle()
95
    {
96
        $this->containsUpdateHandle(self::GLOBAL_IDENTIFIER);
97
    }
98
99
    protected function thenContainsDefaultUpdateHandle()
100
    {
101
        $this->containsUpdateHandle(self::PAGE_ID_FROM_FIXTURE);
102
    }
103
104
    /**
105
     * Layout handles must contain the file.
106
     *
107
     * @param int|string $identifier
108
     * @param string $fileName
109
     */
110
    protected function containsUpdateHandle(
111
        $identifier = self::GLOBAL_IDENTIFIER,
112
        string $fileName = self::TEST_FILE)
113
    {
114
        $expectedHandle = "cms_page_view_selectable_{$identifier}_{$fileName}";
115
116
        $handles = $this->layoutInterface->getUpdate()->getHandles();
117
        $this->assertContains($expectedHandle, $handles);
118
    }
119
120
    protected function setCustomUpdate(int $forPageId, string $fileName = self::TEST_FILE)
121
    {
122
        $page = $this->getPage();
123
124
        $this->layoutManager->setFakeFiles($forPageId, [$fileName]);
125
126
        $page->setData('layout_update_selected', $fileName);
127
        $this->pageResource->save($page);
128
    }
129
130
    /**
131
     * @param string $pageIdentifier
132
     * @return Page
133
     */
134
    protected function createPage(?string $pageIdentifier = self::PAGE_ID_FROM_FIXTURE): Page
135
    {
136
        $page = $this->pageFactory->create(['customLayoutRepository' => $this->repository]);
137
        $page->setStoreId(self::STORE_ID);
138
        $page->load($pageIdentifier, Page::IDENTIFIER);
139
140
        return $page;
141
    }
142
143
    /**
144
     * @return Page
145
     */
146
    protected function getPage(): Page
147
    {
148
        if (!$this->page || !$this->page->getId()) {
149
            $this->page = $this->createPage();
150
        }
151
        return $this->page;
152
    }
153
154
    /**
155
     * @return int
156
     */
157
    private function getPageId(): int
158
    {
159
        return (int)$this->getPage()->getId();
160
    }
161
}
162