|
| 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 | + foreach ($this->getAllBlockClasses() as $alias => $className) { |
| 104 | + if (!in_array($className, $ignoredClasses)) { |
| 105 | + yield $className => [ |
| 106 | + $className, |
| 107 | + !in_array($className, $notInstanceOfMageCoreBlockAbstract), |
| 108 | + $alias, |
| 109 | + ]; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @covers Mage_Core_Model_Layout::getBlockSingleton() |
| 116 | + * @group Mage_Core |
| 117 | + * @group Mage_Core_Model |
| 118 | + */ |
| 119 | + public function testGetBlockSingletonError(): void |
| 120 | + { |
| 121 | + $this->expectException(\Error::class); |
| 122 | + $this->expectExceptionMessage("Class 'Mage_Invalid_Block_Type' not found"); |
| 123 | + |
| 124 | + $this->subject->getBlockSingleton('invalid/type'); |
| 125 | + } |
| 126 | +} |
0 commit comments