ExtractIteratorTest.php 3.1 KB

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