ReplaceIteratorTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Collection\Iterator;
  16. use Cake\Collection\Iterator\ReplaceIterator;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * ReplaceIterator Test
  20. */
  21. class ReplaceIteratorTest extends TestCase
  22. {
  23. /**
  24. * Tests that the iterator works correctly
  25. *
  26. * @return void
  27. */
  28. public function testReplace()
  29. {
  30. $items = new \ArrayIterator([1, 2, 3]);
  31. $callable = $this->getMockBuilder(\StdClass::class)
  32. ->setMethods(['__invoke'])
  33. ->getMock();
  34. $callable->expects($this->at(0))
  35. ->method('__invoke')
  36. ->with(1, 0, $items)
  37. ->will($this->returnValue(1));
  38. $callable->expects($this->at(1))
  39. ->method('__invoke')
  40. ->with(2, 1, $items)
  41. ->will($this->returnValue(4));
  42. $callable->expects($this->at(2))
  43. ->method('__invoke')
  44. ->with(3, 2, $items)
  45. ->will($this->returnValue(9));
  46. $map = new ReplaceIterator($items, $callable);
  47. $this->assertEquals([1, 4, 9], iterator_to_array($map));
  48. }
  49. }