ReplaceIteratorTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 Cake\Collection\Iterator\ReplaceIterator;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * ReplaceIterator Test
  21. */
  22. class ReplaceIteratorTest extends TestCase
  23. {
  24. /**
  25. * Tests that the iterator works correctly
  26. *
  27. * @return void
  28. */
  29. public function testReplace()
  30. {
  31. $items = new \ArrayIterator([1, 2, 3]);
  32. $callable = $this->getMockBuilder(\stdClass::class)
  33. ->setMethods(['__invoke'])
  34. ->getMock();
  35. $callable->expects($this->at(0))
  36. ->method('__invoke')
  37. ->with(1, 0, $items)
  38. ->will($this->returnValue(1));
  39. $callable->expects($this->at(1))
  40. ->method('__invoke')
  41. ->with(2, 1, $items)
  42. ->will($this->returnValue(4));
  43. $callable->expects($this->at(2))
  44. ->method('__invoke')
  45. ->with(3, 2, $items)
  46. ->will($this->returnValue(9));
  47. $map = new ReplaceIterator($items, $callable);
  48. $this->assertEquals([1, 4, 9], iterator_to_array($map));
  49. }
  50. }