Skip to content

Commit

Permalink
Added a test to render with a custom element helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
waltertamboer committed Jul 31, 2012
1 parent 8b2d389 commit 1e4b2c7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/Zend/Form/TestAsset/CustomViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Form
*/

namespace ZendTest\Form\TestAsset;

use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\AbstractHelper;
use Zend\Form\View\Helper\FormElement;

class CustomViewHelper extends AbstractHelper
{
/**
* @var FormElement
*/
protected $elementHelper;

public function __invoke(ElementInterface $element)
{
$elementHelper = $this->getElementHelper();

$name = preg_replace('/[^a-z0-9_-]+/', '', $element->getName());

$result = '<div id="custom' . $name . '">' . $elementHelper($element) . '</div>';

return $result;
}

/**
* Retrieve the FormElement helper
*
* @return FormElement
*/
protected function getElementHelper()
{
if ($this->elementHelper) {
return $this->elementHelper;
}

if (method_exists($this->view, 'plugin')) {
$this->elementHelper = $this->view->plugin('form_element');
}

if (!$this->elementHelper instanceof FormElement) {
$this->elementHelper = new FormElement();
}

return $this->elementHelper;
}
}
17 changes: 17 additions & 0 deletions tests/Zend/Form/View/Helper/FormCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Zend\View\Helper\Doctype;
use Zend\View\Renderer\PhpRenderer;
use ZendTest\Form\TestAsset\FormCollection;
use ZendTest\Form\TestAsset\CustomViewHelper;

/**
* @category Zend
Expand Down Expand Up @@ -97,4 +98,20 @@ public function testCorrectlyIndexNestedElementsInCollection()
$this->assertContains('fieldsets[1][field]', $markup);
$this->assertContains('fieldsets[1][nested_fieldset][anotherField]', $markup);
}

public function testRenderWithDefaultHelper()
{
$form = $this->getForm();

$collection = $form->get('colors');
$collection->setShouldCreateTemplate(false);

$elementHelper = new CustomViewHelper();
$elementHelper->setView($this->renderer);

$markup = $this->helper->setElementHelper($elementHelper)->render($collection);

$this->assertContains('id="customcolors0"', $markup);
$this->assertContains('id="customcolors1"', $markup);
}
}

0 comments on commit 1e4b2c7

Please sign in to comment.