basePath = dirname(__FILE__) . '/_files/modules'; $this->helper = new Zend_View_Helper_PartialLoop(); Zend_Controller_Front::getInstance()->resetInstance(); } /** * Tears down the fixture, for example, close a network connection. * This method is called after a test is executed. * * @return void */ public function tearDown() { unset($this->helper); } /** * @return void */ public function testPartialLoopIteratesOverArray() { $data = array( array('message' => 'foo'), array('message' => 'bar'), array('message' => 'baz'), array('message' => 'bat') ); $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); $result = $this->helper->partialLoop('partialLoop.phtml', $data); foreach ($data as $item) { $string = 'This is an iteration: ' . $item['message']; $this->assertContains($string, $result); } } /** * @return void */ public function testPartialLoopIteratesOverIterator() { $data = array( array('message' => 'foo'), array('message' => 'bar'), array('message' => 'baz'), array('message' => 'bat') ); $o = new Zend_View_Helper_PartialLoop_IteratorTest($data); $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); $result = $this->helper->partialLoop('partialLoop.phtml', $o); foreach ($data as $item) { $string = 'This is an iteration: ' . $item['message']; $this->assertContains($string, $result); } } /** * @return void */ public function testPartialLoopIteratesOverRecursiveIterator() { $rIterator = new Zend_View_Helper_PartialLoop_RecursiveIteratorTest(); for ($i = 0; $i < 5; ++$i) { $data = array( 'message' => 'foo' . $i, ); $rIterator->addItem(new Zend_View_Helper_PartialLoop_IteratorTest($data)); } $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); $result = $this->helper->partialLoop('partialLoop.phtml', $rIterator); foreach ($rIterator as $item) { foreach ($item as $key => $value) { $this->assertContains($value, $result, var_export($value, 1)); } } } /** * @return void */ public function testPartialLoopThrowsExceptionWithBadIterator() { $data = array( array('message' => 'foo'), array('message' => 'bar'), array('message' => 'baz'), array('message' => 'bat') ); $o = new Zend_View_Helper_PartialLoop_BogusIteratorTest($data); $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); try { $result = $this->helper->partialLoop('partialLoop.phtml', $o); $this->fail('PartialLoop should only work with arrays and iterators'); } catch (Exception $e) { } } /** * @return void */ public function testPartialLoopFindsModule() { Zend_Controller_Front::getInstance()->addModuleDirectory($this->basePath); $data = array( array('message' => 'foo'), array('message' => 'bar'), array('message' => 'baz'), array('message' => 'bat') ); $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); $result = $this->helper->partialLoop('partialLoop.phtml', 'foo', $data); foreach ($data as $item) { $string = 'This is an iteration in the foo module: ' . $item['message']; $this->assertContains($string, $result); } } public function testPassingNoArgsReturnsHelperInstance() { $test = $this->helper->partialLoop(); $this->assertSame($this->helper, $test); } public function testShouldAllowIteratingOverTraversableObjects() { $data = array( array('message' => 'foo'), array('message' => 'bar'), array('message' => 'baz'), array('message' => 'bat') ); $o = new ArrayObject($data); $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); $result = $this->helper->partialLoop('partialLoop.phtml', $o); foreach ($data as $item) { $string = 'This is an iteration: ' . $item['message']; $this->assertContains($string, $result); } } public function testShouldAllowIteratingOverObjectsImplementingToArray() { $data = array( array('message' => 'foo'), array('message' => 'bar'), array('message' => 'baz'), array('message' => 'bat') ); $o = new Zend_View_Helper_PartialLoop_ToArrayTest($data); $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); $result = $this->helper->partialLoop('partialLoop.phtml', $o); foreach ($data as $item) { $string = 'This is an iteration: ' . $item['message']; $this->assertContains($string, $result, $result); } } /** * @see ZF-3350 * @see ZF-3352 */ public function testShouldNotCastToArrayIfObjectIsTraversable() { $data = array( new Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer(array('message' => 'foo')), new Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer(array('message' => 'bar')), new Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer(array('message' => 'baz')), new Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer(array('message' => 'bat')), ); $o = new Zend_View_Helper_PartialLoop_IteratorWithToArrayTest($data); $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); $this->helper->setObjectKey('obj'); $result = $this->helper->partialLoop('partialLoopObject.phtml', $o); foreach ($data as $item) { $string = 'This is an iteration: ' . $item->message; $this->assertContains($string, $result, $result); } } /** * @see ZF-3083 */ public function testEmptyArrayPassedToPartialLoopShouldNotThrowException() { $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); try { $result = $this->helper->partialLoop('partialLoop.phtml', array()); } catch (Exception $e) { $this->fail('Empty array should not cause partialLoop to throw exception'); } try { $result = $this->helper->partialLoop('partialLoop.phtml', null, array()); } catch (Exception $e) { $this->fail('Empty array should not cause partialLoop to throw exception'); } } /** * @see ZF-2737 * @link http://framework.zend.com/issues/browse/ZF-2737 */ public function testPartialLoopIncramentsPartialCounter() { $data = array( array('message' => 'foo'), array('message' => 'bar'), array('message' => 'baz'), array('message' => 'bat') ); $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); $result = $this->helper->partialLoop('partialLoopCouter.phtml', $data); foreach ($data as $key=>$item) { $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1); $this->assertContains($string, $result); } } /** * @see ZF-5174 * @link http://framework.zend.com/issues/browse/ZF-5174 */ public function testPartialLoopPartialCounterResets() { $data = array( array('message' => 'foo'), array('message' => 'bar'), array('message' => 'baz'), array('message' => 'bat') ); $view = new Zend_View(array( 'scriptPath' => $this->basePath . '/default/views/scripts' )); $this->helper->setView($view); $result = $this->helper->partialLoop('partialLoopCouter.phtml', $data); foreach ($data as $key=>$item) { $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1); $this->assertContains($string, $result); } $result = $this->helper->partialLoop('partialLoopCouter.phtml', $data); foreach ($data as $key=>$item) { $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1); $this->assertContains($string, $result); } } } class Zend_View_Helper_PartialLoop_IteratorTest implements Iterator { public $items; public function __construct(array $array) { $this->items = $array; } public function current() { return current($this->items); } public function key() { return key($this->items); } public function next() { return next($this->items); } public function rewind() { return reset($this->items); } public function valid() { return (current($this->items) !== false); } public function toArray() { return $this->items; } } class Zend_View_Helper_PartialLoop_RecursiveIteratorTest implements Iterator { public $items; public function __construct() { $this->items = array(); } public function addItem(Iterator $iterator) { $this->items[] = $iterator; return $this; } public function current() { return current($this->items); } public function key() { return key($this->items); } public function next() { return next($this->items); } public function rewind() { return reset($this->items); } public function valid() { return (current($this->items) !== false); } } class Zend_View_Helper_PartialLoop_BogusIteratorTest { } class Zend_View_Helper_PartialLoop_ToArrayTest { public function __construct(array $data) { $this->data = $data; } public function toArray() { return $this->data; } } class Zend_View_Helper_PartialLoop_IteratorWithToArrayTest implements Iterator { public $items; public function __construct(array $array) { $this->items = $array; } public function toArray() { return $this->items; } public function current() { return current($this->items); } public function key() { return key($this->items); } public function next() { return next($this->items); } public function rewind() { return reset($this->items); } public function valid() { return (current($this->items) !== false); } } class Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer { protected $_info; public function __construct(array $info) { foreach ($info as $key => $value) { $this->$key = $value; } $this->_info = $info; } public function toArray() { return $this->_info; } } // Call Zend_View_Helper_PartialLoopTest::main() if this source file is executed directly. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_PartialLoopTest::main") { Zend_View_Helper_PartialLoopTest::main(); }