PaginationControlTest.php 6.7 KB

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