| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Collection\Iterator;
- use Cake\Collection\Iterator\InsertIterator;
- use Cake\TestSuite\TestCase;
- /**
- * InsertIterator Test
- */
- class InsertIteratorTest extends TestCase
- {
- /**
- * Test insert simple path
- *
- * @return void
- */
- public function testInsertSimplePath()
- {
- $items = [
- 'a' => ['name' => 'Derp'],
- 'b' => ['name' => 'Derpina'],
- ];
- $values = [20, 21];
- $iterator = new InsertIterator($items, 'age', $values);
- $result = $iterator->toArray();
- $expected = [
- 'a' => ['name' => 'Derp', 'age' => 20],
- 'b' => ['name' => 'Derpina', 'age' => 21],
- ];
- $this->assertSame($expected, $result);
- }
- /**
- * Test insert deep path
- *
- * @return void
- */
- public function testInsertDeepPath()
- {
- $items = [
- 'a' => ['name' => 'Derp', 'a' => ['deep' => ['thing' => 1]]],
- 'b' => ['name' => 'Derpina', 'a' => ['deep' => ['thing' => 2]]],
- ];
- $values = new \ArrayIterator([20, 21]);
- $iterator = new InsertIterator($items, 'a.deep.path', $values);
- $result = $iterator->toArray();
- $expected = [
- 'a' => ['name' => 'Derp', 'a' => ['deep' => ['thing' => 1, 'path' => 20]]],
- 'b' => ['name' => 'Derpina', 'a' => ['deep' => ['thing' => 2, 'path' => 21]]],
- ];
- $this->assertSame($expected, $result);
- }
- /**
- * Test that missing properties in the path will skip inserting
- *
- * @return void
- */
- public function testInsertDeepPathMissingStep()
- {
- $items = [
- 'a' => ['name' => 'Derp', 'a' => ['deep' => ['thing' => 1]]],
- 'b' => ['name' => 'Derpina', 'a' => ['nested' => 2]],
- ];
- $values = [20, 21];
- $iterator = new InsertIterator($items, 'a.deep.path', $values);
- $result = $iterator->toArray();
- $expected = [
- 'a' => ['name' => 'Derp', 'a' => ['deep' => ['thing' => 1, 'path' => 20]]],
- 'b' => ['name' => 'Derpina', 'a' => ['nested' => 2]],
- ];
- $this->assertSame($expected, $result);
- }
- /**
- * Tests that the iterator will insert values as long as there still exist
- * some in the values array
- *
- * @return void
- */
- public function testInsertTargetCountBigger()
- {
- $items = [
- 'a' => ['name' => 'Derp'],
- 'b' => ['name' => 'Derpina'],
- ];
- $values = [20];
- $iterator = new InsertIterator($items, 'age', $values);
- $result = $iterator->toArray();
- $expected = [
- 'a' => ['name' => 'Derp', 'age' => 20],
- 'b' => ['name' => 'Derpina'],
- ];
- $this->assertSame($expected, $result);
- }
- /**
- * Tests that the iterator will insert values as long as there still exist
- * some in the values array
- *
- * @return void
- */
- public function testInsertSourceBigger()
- {
- $items = [
- 'a' => ['name' => 'Derp'],
- 'b' => ['name' => 'Derpina'],
- ];
- $values = [20, 21, 23];
- $iterator = new InsertIterator($items, 'age', $values);
- $result = $iterator->toArray();
- $expected = [
- 'a' => ['name' => 'Derp', 'age' => 20],
- 'b' => ['name' => 'Derpina', 'age' => 21],
- ];
- $this->assertSame($expected, $result);
- }
- /**
- * Tests the iterator can be rewound
- *
- * @return void
- */
- public function testRewind()
- {
- $items = [
- 'a' => ['name' => 'Derp'],
- 'b' => ['name' => 'Derpina'],
- ];
- $values = [20, 21];
- $iterator = new InsertIterator($items, 'age', $values);
- $iterator->next();
- $this->assertEquals(['name' => 'Derpina', 'age' => 21], $iterator->current());
- $iterator->rewind();
- $result = $iterator->toArray();
- $expected = [
- 'a' => ['name' => 'Derp', 'age' => 20],
- 'b' => ['name' => 'Derpina', 'age' => 21],
- ];
- $this->assertSame($expected, $result);
- }
- }
|