TreeBehaviorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 CakePHP(tm) v 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Model\Behavior;
  16. use Cake\Collection\Collection;
  17. use Cake\Event\Event;
  18. use Cake\Model\Behavior\TranslateBehavior;
  19. use Cake\ORM\Entity;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Translate behavior test case
  24. */
  25. class TreeBehaviorTest extends TestCase {
  26. /**
  27. * fixtures
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.number_tree'
  33. ];
  34. public function setUp() {
  35. $this->table = TableRegistry::get('NumberTrees');
  36. $this->table->addBehavior('Tree');
  37. }
  38. public function tearDown() {
  39. parent::tearDown();
  40. TableRegistry::clear();
  41. }
  42. /**
  43. * Tests the find('path') method
  44. *
  45. * @return void
  46. */
  47. public function testFindPath() {
  48. $nodes = $this->table->find('path', ['for' => 9]);
  49. $this->assertEquals([1, 6, 9], $nodes->extract('id')->toArray());
  50. $nodes = $this->table->find('path', ['for' => 10]);
  51. $this->assertEquals([1, 6, 10], $nodes->extract('id')->toArray());
  52. $nodes = $this->table->find('path', ['for' => 5]);
  53. $this->assertEquals([1, 2, 5], $nodes->extract('id')->toArray());
  54. $nodes = $this->table->find('path', ['for' => 1]);
  55. $this->assertEquals([1], $nodes->extract('id')->toArray());
  56. $nodes = $this->table->find('path', ['for' => 11]);
  57. $this->assertEquals([11], $nodes->extract('id')->toArray());
  58. }
  59. /**
  60. * Tests the childCount()
  61. *
  62. * @return void
  63. */
  64. public function testChildCount() {
  65. // direct childs for the root node
  66. $countDirect = $this->table->childCount(1, true);
  67. $this->assertEquals($countDirect, 2);
  68. // counts all the childs of root
  69. $count = $this->table->childCount(1, false);
  70. $this->assertEquals($count, 9);
  71. // counts direct childs
  72. $count = $this->table->childCount(2, false);
  73. $this->assertEquals($count, 3);
  74. // count childs for a middle-node
  75. $count = $this->table->childCount(6, false);
  76. $this->assertEquals($count, 4);
  77. // count leaf childs
  78. $count = $this->table->childCount(10, false);
  79. $this->assertEquals($count, 0);
  80. }
  81. }