Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7 - placeholder view helper return value should be given also with null input #153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! :)

*/
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);
}
}