ExtractIteratorTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 ArrayObject;
  18. use Cake\Collection\Iterator\ExtractIterator;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * ExtractIterator Test
  22. */
  23. class ExtractIteratorTest extends TestCase
  24. {
  25. /**
  26. * Tests it is possible to extract a column in the first level of an array
  27. */
  28. public function testExtractFromArrayShallow(): void
  29. {
  30. $items = [
  31. ['a' => 1, 'b' => 2],
  32. ['a' => 3, 'b' => 4],
  33. ];
  34. $extractor = new ExtractIterator($items, 'a');
  35. $this->assertEquals([1, 3], iterator_to_array($extractor));
  36. $extractor = new ExtractIterator($items, 'b');
  37. $this->assertEquals([2, 4], iterator_to_array($extractor));
  38. $extractor = new ExtractIterator($items, 'c');
  39. $this->assertEquals([null, null], iterator_to_array($extractor));
  40. }
  41. /**
  42. * Tests it is possible to extract a column in the first level of an object
  43. */
  44. public function testExtractFromObjectShallow(): void
  45. {
  46. $items = [
  47. new ArrayObject(['a' => 1, 'b' => 2]),
  48. new ArrayObject(['a' => 3, 'b' => 4]),
  49. ];
  50. $extractor = new ExtractIterator($items, 'a');
  51. $this->assertEquals([1, 3], iterator_to_array($extractor));
  52. $extractor = new ExtractIterator($items, 'b');
  53. $this->assertEquals([2, 4], iterator_to_array($extractor));
  54. $extractor = new ExtractIterator($items, 'c');
  55. $this->assertEquals([null, null], iterator_to_array($extractor));
  56. }
  57. /**
  58. * Tests it is possible to extract a column deeply nested in the structure
  59. */
  60. public function testExtractFromArrayDeep(): void
  61. {
  62. $items = [
  63. ['a' => ['b' => ['c' => 10]], 'b' => 2],
  64. ['a' => ['b' => ['d' => 15]], 'b' => 4],
  65. ['a' => ['x' => ['z' => 20]], 'b' => 4],
  66. ['a' => ['b' => ['c' => 25]], 'b' => 2],
  67. ];
  68. $extractor = new ExtractIterator($items, 'a.b.c');
  69. $this->assertEquals([10, null, null, 25], iterator_to_array($extractor));
  70. }
  71. /**
  72. * Tests that it is possible to pass a callable as the extractor.
  73. */
  74. public function testExtractWithCallable(): void
  75. {
  76. $items = [
  77. ['a' => 1, 'b' => 2],
  78. ['a' => 3, 'b' => 4],
  79. ];
  80. $extractor = new ExtractIterator($items, function ($item) {
  81. return $item['b'];
  82. });
  83. $this->assertEquals([2, 4], iterator_to_array($extractor));
  84. }
  85. }