BufferedIteratorTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Collection\Iterator;
  17. use ArrayObject;
  18. use Cake\Collection\Iterator\BufferedIterator;
  19. use Cake\TestSuite\TestCase;
  20. use NoRewindIterator;
  21. /**
  22. * BufferedIterator Test
  23. */
  24. class BufferedIteratorTest extends TestCase
  25. {
  26. /**
  27. * Tests that items are cached once iterated over them
  28. */
  29. public function testBufferItems(): void
  30. {
  31. $items = new ArrayObject([
  32. 'a' => 1,
  33. 'b' => 2,
  34. 'c' => 3,
  35. ]);
  36. $iterator = new BufferedIterator($items);
  37. $expected = (array)$items;
  38. $this->assertSame($expected, $iterator->toArray());
  39. $items['c'] = 5;
  40. $buffered = $iterator->toArray();
  41. $this->assertSame($expected, $buffered);
  42. }
  43. /**
  44. * Tests that items are cached once iterated over them
  45. */
  46. public function testCount(): void
  47. {
  48. $items = new ArrayObject([
  49. 'a' => 1,
  50. 'b' => 2,
  51. 'c' => 3,
  52. ]);
  53. $iterator = new BufferedIterator($items);
  54. $this->assertCount(3, $iterator);
  55. $buffered = $iterator->toArray();
  56. $this->assertSame((array)$items, $buffered);
  57. $iterator = new BufferedIterator(new NoRewindIterator($items->getIterator()));
  58. $this->assertCount(3, $iterator);
  59. $buffered = $iterator->toArray();
  60. $this->assertSame((array)$items, $buffered);
  61. }
  62. /**
  63. * Tests that partial iteration can be reset.
  64. */
  65. public function testBufferPartial(): void
  66. {
  67. $items = new ArrayObject([1, 2, 3]);
  68. $iterator = new BufferedIterator($items);
  69. foreach ($iterator as $key => $value) {
  70. if ($key == 1) {
  71. break;
  72. }
  73. }
  74. $result = [];
  75. foreach ($iterator as $value) {
  76. $result[] = $value;
  77. }
  78. $this->assertEquals([1, 2, 3], $result);
  79. }
  80. /**
  81. * Testing serialize and unserialize features.
  82. */
  83. public function testSerialization(): void
  84. {
  85. $items = new ArrayObject([
  86. 'a' => 1,
  87. 'b' => 2,
  88. 'c' => 3,
  89. ]);
  90. $expected = (array)$items;
  91. $iterator = new BufferedIterator($items);
  92. $serialized = serialize($iterator);
  93. $outcome = unserialize($serialized);
  94. $this->assertEquals($expected, $outcome->toArray());
  95. }
  96. }