| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_View
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- // Call Zend_View_Helper_PartialLoopTest::main() if this source file is executed directly.
- if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_PartialLoopTest::main");
- }
- /** Zend_View_Helper_PartialLoop */
- require_once 'Zend/View/Helper/PartialLoop.php';
- /** Zend_View */
- require_once 'Zend/View.php';
- /** Zend_Controller_Front */
- require_once 'Zend/Controller/Front.php';
- /**
- * Test class for Zend_View_Helper_PartialLoop.
- *
- * @category Zend
- * @package Zend_View
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_View
- * @group Zend_View_Helper
- */
- class Zend_View_Helper_PartialLoopTest extends PHPUnit_Framework_TestCase
- {
- /**
- * @var Zend_View_Helper_PartialLoop
- */
- public $helper;
- /**
- * @var string
- */
- public $basePath;
- /**
- * Runs the test methods of this class.
- *
- * @return void
- */
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_PartialLoopTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- /**
- * Sets up the fixture, for example, open a network connection.
- * This method is called before a test is executed.
- *
- * @return void
- */
- public function setUp()
- {
- $this->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);
- }
- }
- /**
- * @group ZF-3350
- * @group 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);
- }
- }
- /**
- * @group 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');
- }
- }
- /**
- * @group 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);
- }
- }
- /**
- * @group 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);
- }
- }
- /**
- * @see ZF-7157
- */
- public function testPartialLoopSetsTotalCount()
- {
- $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 = 'Total count: ' . count($data);
- $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();
- }
|