Skip to content

Commit 3dda4bb

Browse files
committed
Adding tests for BufferedIterator
1 parent 523020c commit 3dda4bb

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 3.0.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace Cake\Test\TestCase\Collection\Iterator;
16+
17+
use ArrayObject;
18+
use Cake\Collection\Iterator\BufferedIterator;
19+
use Cake\TestSuite\TestCase;
20+
use NoRewindIterator;
21+
22+
/**
23+
* BufferedIterator Test
24+
*
25+
*/
26+
class BufferedIteratorTest extends TestCase {
27+
28+
/**
29+
* Tests that items are cached once iterated over them
30+
*
31+
* @return void
32+
*/
33+
public function testBuffer() {
34+
$items = new ArrayObject([
35+
'a' => 1,
36+
'b' => 2,
37+
'c' => 3
38+
]);
39+
$iterator = new BufferedIterator($items);
40+
$expected = (array)$items;
41+
$this->assertSame($expected, $iterator->toArray());
42+
43+
$items['c'] = 5;
44+
$buffered = $iterator->toArray();
45+
$this->assertSame($expected, $buffered);
46+
}
47+
48+
/**
49+
* Tests that items are cached once iterated over them
50+
*
51+
* @return void
52+
*/
53+
public function testCount() {
54+
$items = new ArrayObject([
55+
'a' => 1,
56+
'b' => 2,
57+
'c' => 3
58+
]);
59+
$iterator = new BufferedIterator($items);
60+
$this->assertCount(3, $iterator);
61+
$buffered = $iterator->toArray();
62+
$this->assertSame((array)$items, $buffered);
63+
64+
$iterator = new BufferedIterator(new NoRewindIterator($items->getIterator()));
65+
$this->assertCount(3, $iterator);
66+
$buffered = $iterator->toArray();
67+
$this->assertSame((array)$items, $buffered);
68+
}
69+
70+
}

0 commit comments

Comments
 (0)