PaginationControlTest.php 7.6 KB

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