Skip to content

Commit a824c4f

Browse files
authored
PHPUnit: added test for Mage_Core_Model_Layout::getBlockSingleton() (#4431)
* added unit tests * fixed error (object to string conversion), better message * deprecated method * updated docblock * removed Mage_Adminhtml_Block_Widget_Grid_Block - PHP Fatal error: Class Mage_Adminhtml_Block_Widget_Grid_Block contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Interface::setColumn, Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Interface::getColumn) * updated test * phpstan - see: removed Mage_Adminhtml_Block_Widget_Grid_Block * updated test * typo
1 parent 916b996 commit a824c4f

File tree

7 files changed

+1869
-35
lines changed

7 files changed

+1869
-35
lines changed

.phpstan.dist.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ parameters:
2828
- app/code/core/Mage/Admin/Model/Acl/Assert/Time.php
2929
- app/code/core/Mage/Api/Model/Acl/Assert/Ip.php
3030
- app/code/core/Mage/Api/Model/Acl/Assert/Time.php
31-
- app/code/core/Mage/Adminhtml/Block/Widget/Grid/Block.php
3231
- app/code/core/Mage/Core/Model/Mysql4/Design/Theme/Collection.php
3332
- lib/Varien/Directory/Collection.php
3433
- lib/Varien/Directory/Factory.php

app/code/core/Mage/Adminhtml/Block/Widget/Grid/Block.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

app/code/core/Mage/Core/Model/Layout.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,12 @@ protected function _generateAction($node, $parent)
353353
}
354354

355355
/**
356+
* @codeCoverageIgnore
356357
* @param string $method
357358
* @param string[] $args
358359
* @throws Mage_Core_Exception
360+
* @deprecated
361+
* @see Mage_Core_Helper_Security::validateAgainstBlockMethodBlacklist()
359362
*/
360363
protected function validateAgainstBlacklist(Mage_Core_Block_Abstract $block, $method, array $args)
361364
{
@@ -503,12 +506,12 @@ protected function _getBlockInstance($block, array $attributes = [])
503506
}
504507
}
505508
if (class_exists($block, false) || mageFindClassFile($block)) {
506-
// phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation
507509
$block = new $block($attributes);
508510
}
509511
}
510512
if (!$block instanceof Mage_Core_Block_Abstract) {
511-
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
513+
$block = is_object($block) ? get_class($block) : $block;
514+
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s (not instance of Mage_Core_Block_Abstract)', $block));
512515
}
513516
return $block;
514517
}
@@ -591,7 +594,7 @@ public function getMessagesBlock()
591594

592595
/**
593596
* @param string $type
594-
* @return Mage_Core_Block_Abstract
597+
* @return Mage_Core_Block_Abstract|object
595598
*/
596599
public function getBlockSingleton($type)
597600
{
@@ -601,7 +604,6 @@ public function getBlockSingleton($type)
601604
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $type));
602605
}
603606

604-
// phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation
605607
$helper = new $className();
606608
if ($helper) {
607609
if ($helper instanceof Mage_Core_Block_Abstract) {

app/locale/en_US/Mage_Core.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
"Invalid URL scheme.","Invalid URL scheme."
176176
"Invalid base url type","Invalid base url type"
177177
"Invalid block type: %s","Invalid block type: %s"
178+
"Invalid block type: %s (not instance of Mage_Core_Block_Abstract)","Invalid block type: %s (not instance of Mage_Core_Block_Abstract)"
178179
"Invalid block: %s","Invalid block: %s"
179180
"Invalid connection","Invalid connection"
180181
"Invalid date","Invalid date"
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/**
4+
* OpenMage
5+
*
6+
* This source file is subject to the Open Software License (OSL 3.0)
7+
* that is bundled with this package in the file LICENSE.txt.
8+
* It is also available at https://opensource.org/license/osl-3-0-php
9+
*
10+
* @category OpenMage
11+
* @package OpenMage_Tests
12+
* @copyright Copyright (c) 2024 The OpenMage Contributors (https://www.openmage.org)
13+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
14+
*/
15+
16+
declare(strict_types=1);
17+
18+
namespace OpenMage\Tests\Unit\Mage\Core\Model;
19+
20+
use Generator;
21+
use Mage;
22+
use Mage_Core_Model_Layout;
23+
use OpenMage\Tests\Unit\Traits\PhpStormMetaData\BlocksTrait;
24+
use PHPUnit\Framework\TestCase;
25+
26+
class LayoutTest extends TestCase
27+
{
28+
use BlocksTrait;
29+
30+
public Mage_Core_Model_Layout $subject;
31+
32+
public function setUp(): void
33+
{
34+
Mage::app();
35+
$this->subject = Mage::getModel('core/layout');
36+
}
37+
38+
/**
39+
* @dataProvider provideCreateBlock
40+
* @group Mage_Core
41+
* @group Mage_Core_Model
42+
*/
43+
public function testCreateBlock($expectedResult, bool $willReturnBlock, string $type, ?string $name, array $attributes): void
44+
{
45+
$result = $this->subject->createBlock($type, $name, $attributes);
46+
47+
if ($willReturnBlock) {
48+
$this->assertInstanceOf($expectedResult, $result);
49+
} else {
50+
$this->assertFalse($result);
51+
}
52+
}
53+
54+
public function provideCreateBlock(): Generator
55+
{
56+
yield 'instance of Mage_Core_Block_Abstract' => [
57+
\Mage_Cms_Block_Block::class,
58+
true,
59+
'cms/block',
60+
null,
61+
[],
62+
];
63+
yield 'not instance of Mage_Core_Block_Abstract' => [
64+
false,
65+
false,
66+
'rule/conditions',
67+
null,
68+
[],
69+
];
70+
}
71+
72+
/**
73+
* @covers Mage_Core_Model_Layout::getBlockSingleton()
74+
* @dataProvider provideGetBlockSingleton
75+
* @group Mage_Core
76+
* @group Mage_Core_Model
77+
* @group pr4411
78+
*/
79+
public function testGetBlockSingleton($expectedResult, bool $isAbstractBlock, string $type): void
80+
{
81+
$result = $this->subject->getBlockSingleton($type);
82+
83+
$this->assertInstanceOf($expectedResult, $result);
84+
85+
if ($isAbstractBlock) {
86+
$this->assertInstanceOf(\Mage_Core_Block_Abstract::class, $result);
87+
} else {
88+
$this->assertNotInstanceOf(\Mage_Core_Block_Abstract::class, $result);
89+
}
90+
}
91+
92+
public function provideGetBlockSingleton(): Generator
93+
{
94+
$notInstanceOfMageCoreBlockAbstract = $this->getBlockClassesNotInstanceOfMageCoreBlockAbstract();
95+
96+
$ignoredClasses = array_merge(
97+
$this->getAbstractBlockClasses(),
98+
$this->getBlockClassesToMock(),
99+
$this->getBlockClassesWithErrors(),
100+
$this->getBlockClassesWithSessions(),
101+
);
102+
103+
#$allBlocks = $this->getAllBlockClasses();
104+
$allBlocks = [
105+
'adminhtml/api_buttons' => \Mage_Adminhtml_Block_Api_Buttons::class,
106+
'adminhtml/catalog_category_helper_pricestep' => \Mage_Adminhtml_Block_Catalog_Category_Helper_Pricestep::class,
107+
];
108+
109+
foreach ($allBlocks as $alias => $className) {
110+
if (!in_array($className, $ignoredClasses)) {
111+
yield $className => [
112+
$className,
113+
!in_array($className, $notInstanceOfMageCoreBlockAbstract),
114+
$alias,
115+
];
116+
}
117+
}
118+
}
119+
120+
/**
121+
* @covers Mage_Core_Model_Layout::getBlockSingleton()
122+
* @group Mage_Core
123+
* @group Mage_Core_Model
124+
*/
125+
public function testGetBlockSingletonError(): void
126+
{
127+
$this->expectException(\Error::class);
128+
$this->expectExceptionMessage("Class 'Mage_Invalid_Block_Type' not found");
129+
130+
$this->subject->getBlockSingleton('invalid/type');
131+
}
132+
}

tests/unit/Mage/Core/Model/WebsiteTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function setUp(): void
3737

3838
/**
3939
* @group Mage_Core
40+
* @group Mage_Core_Model
4041
*/
4142
public function testLoad(): void
4243
{
@@ -46,6 +47,7 @@ public function testLoad(): void
4647

4748
/**
4849
* @group Mage_Core
50+
* @group Mage_Core_Model
4951
*/
5052
public function testLoadConfig(): void
5153
{
@@ -55,6 +57,7 @@ public function testLoadConfig(): void
5557

5658
/**
5759
* @group Mage_Core
60+
* @group Mage_Core_Model
5861
*/
5962
public function testGetStoreCollection(): void
6063
{
@@ -63,6 +66,7 @@ public function testGetStoreCollection(): void
6366

6467
/**
6568
* @group Mage_Core
69+
* @group Mage_Core_Model
6670
*/
6771
public function testGetGroupCollection(): void
6872
{
@@ -71,6 +75,7 @@ public function testGetGroupCollection(): void
7175

7276
/**
7377
* @group Mage_Core
78+
* @group Mage_Core_Model
7479
*/
7580
public function testGetStores(): void
7681
{
@@ -79,6 +84,7 @@ public function testGetStores(): void
7984

8085
/**
8186
* @group Mage_Core
87+
* @group Mage_Core_Model
8288
*/
8389
public function testGetStoreIds(): void
8490
{
@@ -87,6 +93,7 @@ public function testGetStoreIds(): void
8793

8894
/**
8995
* @group Mage_Core
96+
* @group Mage_Core_Model
9097
*/
9198
public function testGetStoreCodes(): void
9299
{
@@ -95,6 +102,7 @@ public function testGetStoreCodes(): void
95102

96103
/**
97104
* @group Mage_Core
105+
* @group Mage_Core_Model
98106
*/
99107
public function testGetStoresCount(): void
100108
{
@@ -103,6 +111,7 @@ public function testGetStoresCount(): void
103111

104112
/**
105113
* @group Mage_Core
114+
* @group Mage_Core_Model
106115
*/
107116
public function testGetGroups(): void
108117
{
@@ -111,6 +120,7 @@ public function testGetGroups(): void
111120

112121
/**
113122
* @group Mage_Core
123+
* @group Mage_Core_Model
114124
*/
115125
public function testGetGroupIds(): void
116126
{
@@ -119,6 +129,7 @@ public function testGetGroupIds(): void
119129

120130
/**
121131
* @group Mage_Core
132+
* @group Mage_Core_Model
122133
*/
123134
public function testGetGroupsCount(): void
124135
{
@@ -127,6 +138,7 @@ public function testGetGroupsCount(): void
127138

128139
/**
129140
* @group Mage_Core
141+
* @group Mage_Core_Model
130142
*/
131143
public function testGetBaseCurrency(): void
132144
{
@@ -136,6 +148,7 @@ public function testGetBaseCurrency(): void
136148

137149
// /**
138150
// * @group Mage_Core
151+
// * @group Mage_Core_Model
139152
// */
140153
// public function testGetDefaultStore(): void
141154
// {
@@ -154,6 +167,7 @@ public function testGetDefaultStoresSelect(): void
154167

155168
/**
156169
* @group Mage_Core
170+
* @group Mage_Core_Model
157171
*/
158172
public function testIsReadOnly(): void
159173
{

0 commit comments

Comments
 (0)