IteratorTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_Paginator
  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. /**
  23. * @see Zend_Paginator_Adapter_Iterator
  24. */
  25. require_once 'Zend/Paginator/Adapter/Iterator.php';
  26. /**
  27. * @see PHPUnit_Framework_TestCase
  28. */
  29. /**
  30. * @category Zend
  31. * @package Zend_Paginator
  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_Paginator
  36. */
  37. class Zend_Paginator_Adapter_IteratorTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * @var Zend_Paginator_Adapter_Iterator
  41. */
  42. private $_adapter;
  43. /**
  44. * Prepares the environment before running a test.
  45. */
  46. protected function setUp ()
  47. {
  48. parent::setUp();
  49. $iterator = new ArrayIterator(range(1, 101));
  50. $this->_adapter = new Zend_Paginator_Adapter_Iterator($iterator);
  51. }
  52. /**
  53. * Cleans up the environment after running a test.
  54. */
  55. protected function tearDown ()
  56. {
  57. $this->_adapter = null;
  58. parent::tearDown();
  59. }
  60. public function testGetsItemsAtOffsetZero()
  61. {
  62. $actual = $this->_adapter->getItems(0, 10);
  63. $this->assertTrue($actual instanceof LimitIterator);
  64. $i = 1;
  65. foreach ($actual as $item) {
  66. $this->assertEquals($i, $item);
  67. $i++;
  68. }
  69. }
  70. public function testGetsItemsAtOffsetTen()
  71. {
  72. $actual = $this->_adapter->getItems(10, 10);
  73. $this->assertTrue($actual instanceof LimitIterator);
  74. $i = 11;
  75. foreach ($actual as $item) {
  76. $this->assertEquals($i, $item);
  77. $i++;
  78. }
  79. }
  80. public function testReturnsCorrectCount()
  81. {
  82. $this->assertEquals(101, $this->_adapter->count());
  83. }
  84. public function testThrowsExceptionIfNotCountable()
  85. {
  86. $iterator = new LimitIterator(new ArrayIterator(range(1, 101)));
  87. try {
  88. new Zend_Paginator_Adapter_Iterator($iterator);
  89. } catch (Exception $e) {
  90. $this->assertTrue($e instanceof Zend_Paginator_Exception);
  91. $this->assertEquals('Iterator must implement Countable', $e->getMessage());
  92. }
  93. }
  94. /**
  95. * @group ZF-4151
  96. */
  97. public function testDoesNotThrowOutOfBoundsExceptionIfIteratorIsEmpty()
  98. {
  99. $this->_paginator = Zend_Paginator::factory(new ArrayIterator(array()));
  100. $items = $this->_paginator->getCurrentItems();
  101. try {
  102. foreach ($items as $item);
  103. } catch (OutOfBoundsException $e) {
  104. $this->fail('Empty iterator caused in an OutOfBoundsException');
  105. }
  106. }
  107. /**
  108. * @group ZF-8084
  109. */
  110. public function testGetItemsSerializable() {
  111. $items = $this->_adapter->getItems(0, 1);
  112. $innerIterator = $items->getInnerIterator();
  113. $items = unserialize(serialize($items));
  114. $this->assertTrue( ($items->getInnerIterator() == $innerIterator), 'getItems has to be serializable to use caching');
  115. }
  116. /**
  117. * @group ZF-4151
  118. */
  119. public function testEmptySet() {
  120. $iterator = new ArrayIterator(array());
  121. $this->_adapter = new Zend_Paginator_Adapter_Iterator($iterator);
  122. $actual = $this->_adapter->getItems(0, 10);
  123. $this->assertEquals(array(), $actual);
  124. }
  125. }