TreeIteratorTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\NestIterator;
  18. use Cake\Collection\Iterator\TreeIterator;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * TreeIterator Test
  22. */
  23. class TreeIteratorTest extends TestCase
  24. {
  25. /**
  26. * Tests the printer function with defaults
  27. */
  28. public function testPrinter(): void
  29. {
  30. $items = [
  31. [
  32. 'id' => 1,
  33. 'name' => 'a',
  34. 'stuff' => [
  35. ['id' => 2, 'name' => 'b', 'stuff' => [['id' => 3, 'name' => 'c']]],
  36. ],
  37. ],
  38. ['id' => 4, 'name' => 'd', 'stuff' => [['id' => 5, 'name' => 'e']]],
  39. ];
  40. $items = new NestIterator($items, 'stuff');
  41. $result = (new TreeIterator($items))->printer('name')->toArray();
  42. $expected = [
  43. 'a',
  44. '__b',
  45. '____c',
  46. 'd',
  47. '__e',
  48. ];
  49. $this->assertEquals($expected, $result);
  50. }
  51. /**
  52. * Tests the printer function with a custom key extractor and spacer
  53. */
  54. public function testPrinterCustomKeyAndSpacer(): void
  55. {
  56. $items = [
  57. [
  58. 'id' => 1,
  59. 'name' => 'a',
  60. 'stuff' => [
  61. ['id' => 2, 'name' => 'b', 'stuff' => [['id' => 3, 'name' => 'c']]],
  62. ],
  63. ],
  64. ['id' => 4, 'name' => 'd', 'stuff' => [['id' => 5, 'name' => 'e']]],
  65. ];
  66. $items = new NestIterator($items, 'stuff');
  67. $result = (new TreeIterator($items))->printer('id', 'name', '@@')->toArray();
  68. $expected = [
  69. 'a' => '1',
  70. 'b' => '@@2',
  71. 'c' => '@@@@3',
  72. 'd' => '4',
  73. 'e' => '@@5',
  74. ];
  75. $this->assertEquals($expected, $result);
  76. }
  77. /**
  78. * Tests the printer function with a closure extractor
  79. */
  80. public function testPrinterWithClosure(): void
  81. {
  82. $items = [
  83. [
  84. 'id' => 1,
  85. 'name' => 'a',
  86. 'stuff' => [
  87. ['id' => 2, 'name' => 'b', 'stuff' => [['id' => 3, 'name' => 'c']]],
  88. ],
  89. ],
  90. ['id' => 4, 'name' => 'd', 'stuff' => [['id' => 5, 'name' => 'e']]],
  91. ];
  92. $items = new NestIterator($items, 'stuff');
  93. $result = (new TreeIterator($items))
  94. ->printer(function ($element, $key, $iterator) {
  95. return ($iterator->getDepth() + 1 ) . '.' . $key . ' ' . $element['name'];
  96. }, null, '')
  97. ->toArray();
  98. $expected = [
  99. '1.0 a',
  100. '2.0 b',
  101. '3.0 c',
  102. '1.1 d',
  103. '2.0 e',
  104. ];
  105. $this->assertEquals($expected, $result);
  106. }
  107. }