BufferedIteratorTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Collection\Iterator;
  16. use ArrayObject;
  17. use Cake\Collection\Iterator\BufferedIterator;
  18. use Cake\TestSuite\TestCase;
  19. use NoRewindIterator;
  20. /**
  21. * BufferedIterator Test
  22. */
  23. class BufferedIteratorTest extends TestCase
  24. {
  25. /**
  26. * Tests that items are cached once iterated over them
  27. *
  28. * @return void
  29. */
  30. public function testBuffer()
  31. {
  32. $items = new ArrayObject([
  33. 'a' => 1,
  34. 'b' => 2,
  35. 'c' => 3,
  36. ]);
  37. $iterator = new BufferedIterator($items);
  38. $expected = (array)$items;
  39. $this->assertSame($expected, $iterator->toArray());
  40. $items['c'] = 5;
  41. $buffered = $iterator->toArray();
  42. $this->assertSame($expected, $buffered);
  43. }
  44. /**
  45. * Tests that items are cached once iterated over them
  46. *
  47. * @return void
  48. */
  49. public function testCount()
  50. {
  51. $items = new ArrayObject([
  52. 'a' => 1,
  53. 'b' => 2,
  54. 'c' => 3,
  55. ]);
  56. $iterator = new BufferedIterator($items);
  57. $this->assertCount(3, $iterator);
  58. $buffered = $iterator->toArray();
  59. $this->assertSame((array)$items, $buffered);
  60. $iterator = new BufferedIterator(new NoRewindIterator($items->getIterator()));
  61. $this->assertCount(3, $iterator);
  62. $buffered = $iterator->toArray();
  63. $this->assertSame((array)$items, $buffered);
  64. }
  65. }