PaginationControlTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_View_Helper_PaginationControlTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_PaginationControlTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/View.php';
  28. require_once 'Zend/Paginator.php';
  29. require_once 'Zend/View/Helper/PaginationControl.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_View
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_View
  37. * @group Zend_View_Helper
  38. */
  39. class Zend_View_Helper_PaginationControlTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * @var Zend_View_Helper_PaginationControl
  43. */
  44. private $_viewHelper;
  45. private $_paginator;
  46. /**
  47. * Runs the test methods of this class.
  48. *
  49. * @access public
  50. * @static
  51. */
  52. public static function main()
  53. {
  54. require_once "PHPUnit/TextUI/TestRunner.php";
  55. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_PaginationControlTest");
  56. PHPUnit_TextUI_TestRunner::run($suite);
  57. }
  58. /**
  59. * Sets up the fixture, for example, open a network connection.
  60. * This method is called before a test is executed.
  61. *
  62. * @access protected
  63. */
  64. protected function setUp()
  65. {
  66. $view = new Zend_View();
  67. $view->addBasePath(dirname(__FILE__) . '/_files');
  68. Zend_View_Helper_PaginationControl::setDefaultViewPartial(null);
  69. $this->_viewHelper = new Zend_View_Helper_PaginationControl();
  70. $this->_viewHelper->setView($view);
  71. $this->_paginator = Zend_Paginator::factory(range(1, 101));
  72. }
  73. public function tearDown()
  74. {
  75. unset($this->_viewHelper);
  76. unset($this->_paginator);
  77. }
  78. public function testGetsAndSetsView()
  79. {
  80. $view = new Zend_View();
  81. $helper = new Zend_View_Helper_PaginationControl();
  82. $this->assertNull($helper->view);
  83. $helper->setView($view);
  84. $this->assertType('Zend_View_Interface', $helper->view);
  85. }
  86. public function testGetsAndSetsDefaultViewPartial()
  87. {
  88. $this->assertNull(Zend_View_Helper_PaginationControl::getDefaultViewPartial());
  89. Zend_View_Helper_PaginationControl::setDefaultViewPartial('partial');
  90. $this->assertEquals('partial', Zend_View_Helper_PaginationControl::getDefaultViewPartial());
  91. Zend_View_Helper_PaginationControl::setDefaultViewPartial(null);
  92. }
  93. public function testUsesDefaultViewPartialIfNoneSupplied()
  94. {
  95. Zend_View_Helper_PaginationControl::setDefaultViewPartial('testPagination.phtml');
  96. $output = $this->_viewHelper->paginationControl($this->_paginator);
  97. $this->assertContains('pagination control', $output, $output);
  98. Zend_View_Helper_PaginationControl::setDefaultViewPartial(null);
  99. }
  100. public function testThrowsExceptionIfNoViewPartialFound()
  101. {
  102. try {
  103. $this->_viewHelper->paginationControl($this->_paginator);
  104. } catch (Exception $e) {
  105. $this->assertType('Zend_View_Exception', $e);
  106. $this->assertEquals('No view partial provided and no default set', $e->getMessage());
  107. }
  108. }
  109. /**
  110. * @group ZF-4037
  111. */
  112. public function testUsesDefaultScrollingStyleIfNoneSupplied()
  113. {
  114. // First we'll make sure the base case works
  115. $output = $this->_viewHelper->paginationControl($this->_paginator, 'All', 'testPagination.phtml');
  116. $this->assertContains('page count (11) equals pages in range (11)', $output, $output);
  117. Zend_Paginator::setDefaultScrollingStyle('All');
  118. $output = $this->_viewHelper->paginationControl($this->_paginator, null, 'testPagination.phtml');
  119. $this->assertContains('page count (11) equals pages in range (11)', $output, $output);
  120. Zend_View_Helper_PaginationControl::setDefaultViewPartial('testPagination.phtml');
  121. $output = $this->_viewHelper->paginationControl($this->_paginator);
  122. $this->assertContains('page count (11) equals pages in range (11)', $output, $output);
  123. }
  124. /**
  125. * @group ZF-4153
  126. */
  127. public function testUsesPaginatorFromViewIfNoneSupplied()
  128. {
  129. $this->_viewHelper->view->paginator = $this->_paginator;
  130. Zend_View_Helper_PaginationControl::setDefaultViewPartial('testPagination.phtml');
  131. try {
  132. $output = $this->_viewHelper->paginationControl();
  133. } catch (Zend_View_Exception $e) {
  134. $this->fail('Could not find paginator in the view instance');
  135. }
  136. $this->assertContains('pagination control', $output, $output);
  137. }
  138. /**
  139. * @group ZF-4153
  140. */
  141. public function testThrowsExceptionIfNoPaginatorFound()
  142. {
  143. Zend_View_Helper_PaginationControl::setDefaultViewPartial('testPagination.phtml');
  144. try {
  145. $output = $this->_viewHelper->paginationControl();
  146. } catch (Exception $e) {
  147. $this->assertType('Zend_View_Exception', $e);
  148. $this->assertEquals('No paginator instance provided or incorrect type', $e->getMessage());
  149. }
  150. }
  151. /**
  152. * @group ZF-4233
  153. */
  154. public function testAcceptsViewPartialInOtherModule()
  155. {
  156. try {
  157. $this->_viewHelper->paginationControl($this->_paginator, null, array('partial.phtml', 'test'));
  158. } catch (Exception $e) {
  159. /* We don't care whether or not the module exists--we just want to
  160. * make sure it gets to Zend_View_Helper_Partial and it's recognized
  161. * as a module. */
  162. $this->assertType('Zend_View_Helper_Partial_Exception', $e);
  163. $this->assertEquals('Cannot render partial; module does not exist', $e->getMessage());
  164. }
  165. }
  166. /**
  167. * @group ZF-4328
  168. */
  169. public function testUsesPaginatorFromViewOnlyIfNoneSupplied()
  170. {
  171. $this->_viewHelper->view->paginator = $this->_paginator;
  172. $paginator = Zend_Paginator::factory(range(1, 30));
  173. Zend_View_Helper_PaginationControl::setDefaultViewPartial('testPagination.phtml');
  174. $output = $this->_viewHelper->paginationControl($paginator);
  175. $this->assertContains('page count (3)', $output, $output);
  176. }
  177. /**
  178. * @group ZF-4878
  179. */
  180. public function testCanUseObjectForScrollingStyle()
  181. {
  182. $all = new Zend_Paginator_ScrollingStyle_All();
  183. try {
  184. $output = $this->_viewHelper->paginationControl($this->_paginator, $all, 'testPagination.phtml');
  185. } catch (Exception $e) {
  186. $this->fail('Could not use object for sliding style');
  187. }
  188. $this->assertContains('page count (11) equals pages in range (11)', $output, $output);
  189. }
  190. }
  191. // Call Zend_View_Helper_PaginationControlTest::main() if this source file is executed directly.
  192. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_PaginationControlTest::main") {
  193. Zend_View_Helper_PaginationControlTest::main();
  194. }