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

ProductFrontendControllerTest::getProduct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
5
6
use IntegerNet\GlobalCustomLayout\Test\src\ProductLayoutUpdateManager;
7
use Magento\Catalog\Api\Data\ProductInterface;
8
use Magento\Catalog\Api\ProductRepositoryInterface;
9
use Magento\Framework\Exception\CouldNotSaveException;
10
use Magento\Framework\Exception\InputException;
11
use Magento\Framework\Exception\NoSuchEntityException;
12
use Magento\Framework\Exception\StateException;
13
14
class ProductFrontendControllerTest extends AbstractFrontendControllerTest
15
{
16
    /** @var string */
17
    const PRODUCT_SKU_FROM_FIXTURE = 'simple_product_1';
18
19
    /** @var ProductRepositoryInterface $repository */
20
    protected $repository;
21
22
    /** @var ProductInterface $product */
23
    protected $product;
24
25
    /** @var ProductLayoutUpdateManager $layoutManager */
26
    protected $layoutManager;
27
28
    protected function setUp()
29
    {
30
        parent::setUp();
31
32
        $this->layoutManager = $this->objectManager->get(ProductLayoutUpdateManager::class);
33
        $this->repository = $this->objectManager->create(ProductRepositoryInterface::class);
34
    }
35
36
    /**
37
     * Check that Global Custom Layout Update files work for Product views.
38
     *
39
     * @magentoDataFixture Magento/Catalog/controllers/_files/products.php
40
     * @return void
41
     * @throws CouldNotSaveException
42
     * @throws InputException
43
     * @throws NoSuchEntityException
44
     * @throws StateException
45
     */
46
    public function testViewContainsGlobalCustomUpdate(): void
47
    {
48
        $this->givenGlobalCustomUpdateSelected();
49
        $this->whenProductViewed();
50
        $this->thenContainsGlobalUpdateHandle();
51
    }
52
53
    /**
54
     * Check that Default Custom Layout Update files still work for Product views.
55
     *
56
     * @magentoDataFixture Magento/Catalog/controllers/_files/products.php
57
     * @return void
58
     * @throws CouldNotSaveException
59
     * @throws InputException
60
     * @throws NoSuchEntityException
61
     * @throws StateException
62
     */
63
    public function testViewContainsDefaultCustomUpdate(): void
64
    {
65
        $this->givenDefaultCustomUpdateSelected();
66
        $this->whenProductViewed();
67
        $this->thenContainsDefaultUpdateHandle();
68
    }
69
70
    /**
71
     * @throws CouldNotSaveException
72
     * @throws InputException
73
     * @throws NoSuchEntityException
74
     * @throws StateException
75
     */
76
    protected function givenGlobalCustomUpdateSelected()
77
    {
78
        $this->setCustomUpdate(self::GLOBAL_IDENTIFIER);
79
    }
80
81
    /**
82
     * @throws CouldNotSaveException
83
     * @throws InputException
84
     * @throws NoSuchEntityException
85
     * @throws StateException
86
     */
87
    protected function givenDefaultCustomUpdateSelected()
88
    {
89
        $this->setCustomUpdate($this->getProductId());
90
    }
91
92
    /**
93
     * Viewing the product
94
     *
95
     * @param int|null $productId
96
     * @throws NoSuchEntityException
97
     */
98
    protected function whenProductViewed(?int $productId = null): void
99
    {
100
        if (!$productId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $productId 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...
101
            $productId = $this->getProductId();
102
        }
103
        $this->dispatch("catalog/product/view/id/{$productId}");
104
    }
105
106
    protected function thenContainsGlobalUpdateHandle()
107
    {
108
        $this->containsUpdateHandle(self::GLOBAL_IDENTIFIER);
109
    }
110
111
    protected function thenContainsDefaultUpdateHandle()
112
    {
113
        $this->containsUpdateHandle(self::PRODUCT_SKU_FROM_FIXTURE);
114
    }
115
116
    /**
117
     * Layout handles must contain the file.
118
     *
119
     * @param int|string $identifier
120
     * @param string $fileName
121
     */
122
    protected function containsUpdateHandle(
123
        $identifier = self::GLOBAL_IDENTIFIER,
124
        string $fileName = self::TEST_FILE)
125
    {
126
        $expectedHandle = "catalog_product_view_selectable_{$identifier}_{$fileName}";
127
128
        $handles = $this->layoutInterface->getUpdate()->getHandles();
129
        $this->assertContains($expectedHandle, $handles);
130
    }
131
132
    /**
133
     * @param int $forProductId
134
     * @param string $fileName
135
     * @throws CouldNotSaveException
136
     * @throws InputException
137
     * @throws NoSuchEntityException
138
     * @throws StateException
139
     */
140
    protected function setCustomUpdate(int $forProductId, string $fileName = self::TEST_FILE)
141
    {
142
        $product = $this->getProduct();
143
144
        $this->layoutManager->setFakeFiles($forProductId, [$fileName]);
145
146
        $product->setCustomAttribute('custom_layout_update_file', $fileName);
147
        $this->repository->save($product);
148
    }
149
150
    /**
151
     * @param string $sku
152
     * @return ProductInterface
153
     * @throws NoSuchEntityException
154
     */
155
    protected function getProduct(string $sku = self::PRODUCT_SKU_FROM_FIXTURE): ProductInterface
156
    {
157
        if (!$this->product) {
158
            $this->product = $this->repository->get($sku);
159
        }
160
        return $this->product;
161
    }
162
163
    /**
164
     * @return int|null
165
     * @throws NoSuchEntityException
166
     */
167
    protected function getProductId()
168
    {
169
        return (int)$this->getProduct()->getId();
170
    }
171
}
172