IteratorTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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-2009 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. require_once 'PHPUnit/Framework/TestCase.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Paginator
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Paginator
  37. */
  38. class Zend_Paginator_Adapter_IteratorTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * @var Zend_Paginator_Adapter_Iterator
  42. */
  43. private $_adapter;
  44. /**
  45. * Prepares the environment before running a test.
  46. */
  47. protected function setUp ()
  48. {
  49. parent::setUp();
  50. $iterator = new ArrayIterator(range(1, 101));
  51. $this->_adapter = new Zend_Paginator_Adapter_Iterator($iterator);
  52. }
  53. /**
  54. * Cleans up the environment after running a test.
  55. */
  56. protected function tearDown ()
  57. {
  58. $this->_adapter = null;
  59. parent::tearDown();
  60. }
  61. public function testGetsItemsAtOffsetZero()
  62. {
  63. $actual = $this->_adapter->getItems(0, 10);
  64. $this->assertType('LimitIterator', $actual);
  65. $i = 1;
  66. foreach ($actual as $item) {
  67. $this->assertEquals($i, $item);
  68. $i++;
  69. }
  70. }
  71. public function testGetsItemsAtOffsetTen()
  72. {
  73. $actual = $this->_adapter->getItems(10, 10);
  74. $this->assertType('LimitIterator', $actual);
  75. $i = 11;
  76. foreach ($actual as $item) {
  77. $this->assertEquals($i, $item);
  78. $i++;
  79. }
  80. }
  81. public function testReturnsCorrectCount()
  82. {
  83. $this->assertEquals(101, $this->_adapter->count());
  84. }
  85. public function testThrowsExceptionIfNotCountable()
  86. {
  87. $iterator = new LimitIterator(new ArrayIterator(range(1, 101)));
  88. try {
  89. new Zend_Paginator_Adapter_Iterator($iterator);
  90. } catch (Exception $e) {
  91. $this->assertType('Zend_Paginator_Exception', $e);
  92. $this->assertEquals('Iterator must implement Countable', $e->getMessage());
  93. }
  94. }
  95. /**
  96. * @group ZF-4151
  97. */
  98. public function testDoesNotThrowOutOfBoundsExceptionIfIteratorIsEmpty()
  99. {
  100. $this->_paginator = Zend_Paginator::factory(new ArrayIterator(array()));
  101. $items = $this->_paginator->getCurrentItems();
  102. try {
  103. foreach ($items as $item);
  104. } catch (OutOfBoundsException $e) {
  105. $this->fail('Empty iterator caused in an OutOfBoundsException');
  106. }
  107. }
  108. /**
  109. * @group ZF-8084
  110. */
  111. public function testGetItemsSerializable() {
  112. $items = $this->_adapter->getItems(0, 1);
  113. $innerIterator = $items->getInnerIterator();
  114. $items = unserialize(serialize($items));
  115. $this->assertTrue( ($items->getInnerIterator() == $innerIterator), 'getItems has to be serializable to use caching');
  116. }
  117. }