TreeIteratorTest.php 3.4 KB

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