Skip to content

Commit

Permalink
Merge pull request #7686 from kenjis/fix-view-cells-same-short-name
Browse files Browse the repository at this point in the history
fix: [ViewCells] when there are cells with the same short name, only the first cell is loaded
  • Loading branch information
kenjis authored Jul 11, 2023
2 parents f30542c + c193ce7 commit d778ec3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion system/View/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ protected function determineClass(string $library): array
}

// locate and return an instance of the cell
$object = Factories::cells($class);
// @TODO extend Factories to be able to load classes with the same short name.
$object = class_exists($class) ? new $class() : Factories::cells($class);

if (! is_object($object)) {
throw ViewException::forInvalidCellClass($class);
Expand Down
26 changes: 26 additions & 0 deletions tests/_support/View/OtherCells/SampleClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\View\OtherCells;

/**
* Two classes with the same short name.
*
* - Tests\Support\View\SampleClass
* - Tests\Support\View\OtherCells\SampleClass
*/
class SampleClass
{
public function hello()
{
return 'Good-bye!';
}
}
11 changes: 11 additions & 0 deletions tests/system/View/CellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ public function testDisplayRendersWithNamespacedClass()
$this->assertSame($expected, $this->cell->render('\Tests\Support\View\SampleClass::hello'));
}

public function testDisplayRendersTwoCellsWithSameShortName()
{
$output = $this->cell->render('\Tests\Support\View\SampleClass::hello');

$this->assertSame('Hello', $output);

$output = $this->cell->render('\Tests\Support\View\OtherCells\SampleClass::hello');

$this->assertSame('Good-bye!', $output);
}

public function testDisplayRendersWithValidParamString()
{
$params = 'one=two,three=four';
Expand Down

0 comments on commit d778ec3

Please sign in to comment.