TreeIteratorTest.php 3.4 KB

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