Skip to content

Commit

Permalink
Merge pull request #153 from gsteel/fix-7-placeholder-view-helper-ret…
Browse files Browse the repository at this point in the history
…urn-value

Fix #7 - placeholder view helper return value should be given also with `null` input
  • Loading branch information
Ocramius committed Mar 24, 2022
2 parents 63d6668 + 3439bb6 commit 3b5a161
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/Helper/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ class Placeholder extends AbstractHelper
*
* @param string $name
* @throws InvalidArgumentException
* @return AbstractContainer
* @return AbstractContainer|self
* @psalm-template T of string|null
* @psalm-param T $name
* @psalm-return (T is null ? self : AbstractContainer)
*/
public function __invoke($name = null)
{
if ($name === null) {
throw new InvalidArgumentException(
'Placeholder: missing argument. $name is required by placeholder($name)'
);
return $this;
}

$name = (string) $name;
Expand Down
29 changes: 25 additions & 4 deletions test/Helper/PlaceholderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@

namespace LaminasTest\View\Helper;

use Laminas\View\Helper;
use Laminas\View\Helper\Placeholder;
use Laminas\View\Helper\Placeholder\Container\AbstractContainer;
use Laminas\View\Renderer\PhpRenderer as View;
use PHPUnit\Framework\TestCase;

class PlaceholderTest extends TestCase
{
/** @var Helper\Placeholder */
public $placeholder;
public Placeholder $placeholder;

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp(): void
{
$this->placeholder = new Helper\Placeholder();
$this->placeholder = new Placeholder();
}

public function testSetView(): void
Expand All @@ -30,12 +29,26 @@ public function testSetView(): void
$this->assertSame($view, $this->placeholder->getView());
}

public function testContainerExists(): void
{
$this->placeholder->__invoke('foo');
$containerExists = $this->placeholder->__invoke()->containerExists('foo');

$this->assertTrue($containerExists);
}

public function testPlaceholderRetrievesContainer(): void
{
$container = $this->placeholder->__invoke('foo');
$this->assertInstanceOf(AbstractContainer::class, $container);
}

public function testPlaceholderRetrievesItself(): void
{
$container = $this->placeholder->__invoke();
$this->assertSame($container, $this->placeholder);
}

public function testPlaceholderRetrievesSameContainerOnSubsequentCalls(): void
{
$container1 = $this->placeholder->__invoke('foo');
Expand Down Expand Up @@ -67,4 +80,12 @@ public function testClearContainersRemovesAllContainers(): void
$this->assertFalse($this->placeholder->containerExists('foo'));
$this->assertFalse($this->placeholder->containerExists('bar'));
}

public function testGetContainerRetrievesTheCorrectContainer(): void
{
$container1 = $this->placeholder->__invoke('foo');
$container2 = $this->placeholder->__invoke()->getContainer('foo');

$this->assertSame($container1, $container2);
}
}

0 comments on commit 3b5a161

Please sign in to comment.