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

containsUpdateHandle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
5
6
use IntegerNet\GlobalCustomLayout\Test\src\CategoryLayoutUpdateManager;
7
use Magento\Catalog\Api\CategoryRepositoryInterface;
8
use Magento\Catalog\Api\Data\CategoryInterface;
9
use Magento\Framework\Exception\CouldNotSaveException;
10
use Magento\Framework\Exception\NoSuchEntityException;
11
12
/**
13
 * Tests whether global layout handles are correctly saved on categories
14
 * and retrieved on the frontend on Category views
15
 */
16
class CategoryFrontendControllerTest extends AbstractFrontendControllerTest
17
{
18
    /** @var int */
19
    const CATEGORY_ID_FROM_FIXTURE = 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
38
    /**
39
     * Check that Global Custom Layout Update files work for Category views.
40
     *
41
     * @return void
42
     * @throws CouldNotSaveException
43
     * @throws NoSuchEntityException
44
     *
45
     * @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
46
     */
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()
84
    {
85
        $this->setCustomUpdate(self::CATEGORY_ID_FROM_FIXTURE);
86
    }
87
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();
97
98
        $this->layoutManager->setFakeFiles($forCategoryId, [$fileName]);
99
100
        //Updating the custom attribute.
101
        $category->setCustomAttribute('custom_layout_update_file', $fileName);
102
        $this->repository->save($category);
103
    }
104
105
    /**
106
     * Viewing the category
107
     *
108
     * @param int $categoryId
109
     */
110
    protected function whenCategoryViewed(?int $categoryId = null)
111
    {
112
        if (!$categoryId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryId 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...
113
            $categoryId = self::CATEGORY_ID_FROM_FIXTURE;
114
        }
115
        $this->dispatch("catalog/category/view/id/{$categoryId}");
116
    }
117
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_FROM_FIXTURE, 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_FROM_FIXTURE): CategoryInterface
150
    {
151
        if (!$this->category) {
152
            $this->category = $this->repository->get($categoryId);
153
        }
154
        return $this->category;
155
    }
156
}
157