|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Catalog\Controller;
|
7 | 9 |
|
| 10 | +use Magento\Catalog\Model\Category; |
| 11 | +use Magento\Catalog\Model\Session; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Framework\Registry as Registry; |
| 14 | +use Magento\Framework\View\LayoutInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\TestFramework\TestCase\AbstractController; |
| 17 | + |
8 | 18 | /**
|
9 |
| - * Test class for \Magento\Catalog\Controller\Category. |
| 19 | + * Responsible for testing category view action on strorefront. |
10 | 20 | *
|
| 21 | + * @see \Magento\Catalog\Controller\Category\View |
11 | 22 | * @magentoAppArea frontend
|
12 | 23 | */
|
13 |
| -class CategoryTest extends \Magento\TestFramework\TestCase\AbstractController |
| 24 | +class CategoryTest extends AbstractController |
14 | 25 | {
|
| 26 | + /** @var ObjectManagerInterface */ |
| 27 | + private $objectManager; |
| 28 | + |
| 29 | + /** @var Registry */ |
| 30 | + private $registry; |
| 31 | + |
| 32 | + /** @var Session */ |
| 33 | + private $session; |
| 34 | + |
| 35 | + /** @var LayoutInterface */ |
| 36 | + private $layout; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + protected function setUp() |
| 42 | + { |
| 43 | + parent::setUp(); |
| 44 | + |
| 45 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 46 | + $this->registry = $this->objectManager->get(Registry::class); |
| 47 | + $this->layout = $this->objectManager->get(LayoutInterface::class); |
| 48 | + $this->session = $this->objectManager->get(Session::class); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritdoc |
| 53 | + */ |
15 | 54 | public function assert404NotFound()
|
16 | 55 | {
|
17 | 56 | parent::assert404NotFound();
|
18 |
| - /** @var $objectManager \Magento\TestFramework\ObjectManager */ |
19 |
| - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
20 |
| - $this->assertNull($objectManager->get(\Magento\Framework\Registry::class)->registry('current_category')); |
| 57 | + |
| 58 | + $this->assertNull($this->registry->registry('current_category')); |
21 | 59 | }
|
22 | 60 |
|
23 |
| - public function getViewActionDataProvider() |
| 61 | + /** |
| 62 | + * @return array |
| 63 | + */ |
| 64 | + public function getViewActionDataProvider(): array |
24 | 65 | {
|
25 | 66 | return [
|
26 | 67 | 'category without children' => [
|
@@ -56,51 +97,66 @@ public function getViewActionDataProvider()
|
56 | 97 | * @dataProvider getViewActionDataProvider
|
57 | 98 | * @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
|
58 | 99 | * @magentoDbIsolation disabled
|
| 100 | + * @param int $categoryId |
| 101 | + * @param array $expectedHandles |
| 102 | + * @param array $expectedContent |
| 103 | + * @return void |
59 | 104 | */
|
60 |
| - public function testViewAction($categoryId, array $expectedHandles, array $expectedContent) |
| 105 | + public function testViewAction(int $categoryId, array $expectedHandles, array $expectedContent): void |
61 | 106 | {
|
62 | 107 | $this->dispatch("catalog/category/view/id/{$categoryId}");
|
63 |
| - |
64 |
| - /** @var $objectManager \Magento\TestFramework\ObjectManager */ |
65 |
| - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
66 |
| - |
67 |
| - /** @var $currentCategory \Magento\Catalog\Model\Category */ |
68 |
| - $currentCategory = $objectManager->get(\Magento\Framework\Registry::class)->registry('current_category'); |
69 |
| - $this->assertInstanceOf(\Magento\Catalog\Model\Category::class, $currentCategory); |
| 108 | + /** @var $currentCategory Category */ |
| 109 | + $currentCategory = $this->registry->registry('current_category'); |
| 110 | + $this->assertInstanceOf(Category::class, $currentCategory); |
70 | 111 | $this->assertEquals($categoryId, $currentCategory->getId(), 'Category in registry.');
|
71 | 112 |
|
72 |
| - $lastCategoryId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
73 |
| - \Magento\Catalog\Model\Session::class |
74 |
| - )->getLastVisitedCategoryId(); |
| 113 | + $lastCategoryId = $this->session->getLastVisitedCategoryId(); |
75 | 114 | $this->assertEquals($categoryId, $lastCategoryId, 'Last visited category.');
|
76 | 115 |
|
77 | 116 | /* Layout updates */
|
78 |
| - $handles = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
79 |
| - \Magento\Framework\View\LayoutInterface::class |
80 |
| - )->getUpdate()->getHandles(); |
| 117 | + $handles = $this->layout->getUpdate()->getHandles(); |
81 | 118 | foreach ($expectedHandles as $expectedHandleName) {
|
82 | 119 | $this->assertContains($expectedHandleName, $handles);
|
83 | 120 | }
|
84 | 121 |
|
85 | 122 | $responseBody = $this->getResponse()->getBody();
|
86 |
| - |
87 | 123 | /* Response content */
|
88 | 124 | foreach ($expectedContent as $expectedText) {
|
89 | 125 | $this->assertStringMatchesFormat($expectedText, $responseBody);
|
90 | 126 | }
|
91 | 127 | }
|
92 | 128 |
|
93 |
| - public function testViewActionNoCategoryId() |
| 129 | + /** |
| 130 | + * @return void |
| 131 | + */ |
| 132 | + public function testViewActionNoCategoryId(): void |
94 | 133 | {
|
95 | 134 | $this->dispatch('catalog/category/view/');
|
96 | 135 |
|
97 | 136 | $this->assert404NotFound();
|
98 | 137 | }
|
99 | 138 |
|
100 |
| - public function testViewActionInactiveCategory() |
| 139 | + /** |
| 140 | + * @return void |
| 141 | + */ |
| 142 | + public function testViewActionNotExistingCategory(): void |
101 | 143 | {
|
102 | 144 | $this->dispatch('catalog/category/view/id/8');
|
103 | 145 |
|
104 | 146 | $this->assert404NotFound();
|
105 | 147 | }
|
| 148 | + |
| 149 | + /** |
| 150 | + * Checks that disabled category is not available in storefront |
| 151 | + * |
| 152 | + * @magentoDbIsolation enabled |
| 153 | + * @magentoDataFixture Magento/Catalog/_files/inactive_category.php |
| 154 | + * @return void |
| 155 | + */ |
| 156 | + public function testViewActionDisabledCategory(): void |
| 157 | + { |
| 158 | + $this->dispatch('catalog/category/view/id/111'); |
| 159 | + |
| 160 | + $this->assert404NotFound(); |
| 161 | + } |
106 | 162 | }
|
0 commit comments