ReplaceIteratorTest.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 ArrayIterator;
  18. use Cake\Collection\Iterator\ReplaceIterator;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * ReplaceIterator Test
  22. */
  23. class ReplaceIteratorTest extends TestCase
  24. {
  25. /**
  26. * Tests that the iterator works correctly
  27. */
  28. public function testReplace(): void
  29. {
  30. $items = new ArrayIterator([1, 2, 3]);
  31. $callable = function ($value, $key, $itemsArg) use ($items) {
  32. $this->assertSame($items, $itemsArg);
  33. $this->assertContains($value, $items);
  34. $this->assertContains($key, [0, 1, 2]);
  35. return $value > 1 ? $value * $value : $value;
  36. };
  37. $map = new ReplaceIterator($items, $callable);
  38. $this->assertEquals([1, 4, 9], iterator_to_array($map));
  39. }
  40. }