Skip to content

Commit

Permalink
Fixes zendframework#4221 : Set shared false for view_helpers should c…
Browse files Browse the repository at this point in the history
…reate new instance
  • Loading branch information
samsonasik committed Nov 22, 2014
1 parent 1e6d321 commit 743b184
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/Zend/View/Renderer/PhpRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function __call($method, $argv)
$this->__pluginCache[$method] = $this->plugin($method);
}
if (is_callable($this->__pluginCache[$method])) {
return call_user_func_array($this->__pluginCache[$method], $argv);
return call_user_func_array($this->plugin($method), $argv);
}
return $this->__pluginCache[$method];
}
Expand Down
21 changes: 21 additions & 0 deletions tests/ZendTest/View/PhpRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,25 @@ public function testIfViewModelComposesVariablesInstanceThenRendererUsesIt()
$test = $this->renderer->render($model);
$this->assertContains('BAR-BAZ-BAT', $test);
}

/**
* @group ZF2-4221
*/
public function testSharedInstanceHelper()
{
$helpers = $this->renderer->getHelperPluginManager();
$helpers->setInvokableClass('sharedinstance', 'ZendTest\View\TestAsset\SharedInstance');

$helpers->setShared('sharedinstance',false);
// new instance always created when shared = false
$this->assertEquals(1, $this->renderer->sharedinstance());
$this->assertEquals(1, $this->renderer->sharedinstance());
$this->assertEquals(1, $this->renderer->sharedinstance());

$helpers->setShared('sharedinstance',true);
// use shared instance when shared = true
$this->assertEquals(1, $this->renderer->sharedinstance());
$this->assertEquals(2, $this->renderer->sharedinstance());
$this->assertEquals(3, $this->renderer->sharedinstance());
}
}
28 changes: 28 additions & 0 deletions tests/ZendTest/View/TestAsset/SharedInstance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\View\TestAsset;

use Zend\View\Helper\AbstractHelper as Helper;

class SharedInstance extends Helper
{
protected $count = 0;

/**
* Invokable functor
*
* @return int
*/
public function __invoke()
{
$this->count++;
return $this->count;
}
}

0 comments on commit 743b184

Please sign in to comment.