2
0

IteratorTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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-2008 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  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->assertType('LimitIterator', $actual);
  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->assertType('LimitIterator', $actual);
  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->assertType('Zend_Paginator_Exception', $e);
  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. }