TreeIteratorTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * Tests the printer function with defaults
  26. *
  27. * @return void
  28. */
  29. public function testPrinter() {
  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. * @return void
  55. */
  56. public function testPrinterCustomKeyAndSpacer() {
  57. $items = [
  58. [
  59. 'id' => 1,
  60. 'name' => 'a',
  61. 'stuff' => [
  62. ['id' => 2, 'name' => 'b', 'stuff' => [['id' => 3, 'name' => 'c']]]
  63. ]
  64. ],
  65. ['id' => 4, 'name' => 'd', 'stuff' => [['id' => 5, 'name' => 'e']]]
  66. ];
  67. $items = new NestIterator($items, 'stuff');
  68. $result = (new TreeIterator($items))->printer('id', 'name', '@@')->toArray();
  69. $expected = [
  70. 'a' => '1',
  71. 'b' => '@@2',
  72. 'c' => '@@@@3',
  73. 'd' => '4',
  74. 'e' => '@@5'
  75. ];
  76. $this->assertEquals($expected, $result);
  77. }
  78. /**
  79. * Tests the printer function with a closure extractor
  80. *
  81. * @return void
  82. */
  83. public function testPrinterWithClosure() {
  84. $items = [
  85. [
  86. 'id' => 1,
  87. 'name' => 'a',
  88. 'stuff' => [
  89. ['id' => 2, 'name' => 'b', 'stuff' => [['id' => 3, 'name' => 'c']]]
  90. ]
  91. ],
  92. ['id' => 4, 'name' => 'd', 'stuff' => [['id' => 5, 'name' => 'e']]]
  93. ];
  94. $items = new NestIterator($items, 'stuff');
  95. $result = (new TreeIterator($items))
  96. ->printer(function($element, $key, $iterator) {
  97. return ($iterator->getDepth() + 1 ) . '.' . $key . ' ' . $element['name'];
  98. }, null, null)
  99. ->toArray();
  100. $expected = [
  101. '1.0 a',
  102. '2.0 b',
  103. '3.0 c',
  104. '1.1 d',
  105. '2.0 e'
  106. ];
  107. $this->assertEquals($expected, $result);
  108. }
  109. }