‪TYPO3CMS  ‪main
TreeNodeTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪TreeNodeTest extends UnitTestCase
27 {
28  #[Test]
29  public function ‪serializeFixture(): void
30  {
31  $expected = trim(file_get_contents(__DIR__ . '/Fixtures/serialized.txt'));
32  $fixture = new ‪TreeNode();
33  $fixture->setId('Root');
34  $nodeCollection = new ‪TreeNodeCollection();
35  for ($i = 0; $i < 10; ++$i) {
36  $node = new ‪TreeNode();
37  $node->setId($i);
38  $node->setParentNode($fixture);
39  $subNodeCollection = new ‪TreeNodeCollection();
40  for ($j = 0; $j < 5; ++$j) {
41  $subNode = new ‪TreeRepresentationNode();
42  $subNode->setId($j);
43  $subNode->setLabel('SubTest');
44  $subNode->setType('Type');
45  $subNode->setClass('Class');
46  $subNode->setIcon('Icon');
47  $subNode->setCallbackAction('Callback Action');
48  $subNode->setParentNode($node);
49  $subNodeCollection->append($subNode);
50  }
51  $node->setChildNodes($subNodeCollection);
52  $nodeCollection->append($node);
53  }
54  $fixture->setChildNodes($nodeCollection);
55  $serializedString = trim(serialize($fixture));
56  self::assertSame($expected, $serializedString);
57  }
58 
59  #[Test]
60  public function ‪deserializeFixture(): void
61  {
62  $source = trim(file_get_contents(__DIR__ . '/Fixtures/serialized.txt'));
63  $node = unserialize($source);
64  $serializedString = serialize($node);
65  self::assertSame($source, $serializedString);
66  }
67 
68  #[Test]
69  public function ‪compareNodes(): void
70  {
71  $node = new ‪TreeNode(['id' => '15']);
72  $otherNode = new ‪TreeNode(['id' => '5']);
73  $otherNode->setId('25');
74  $compareResult = $node->compareTo($otherNode);
75  self::assertSame(-1, $compareResult);
76  $otherNode->setId('15');
77  $compareResult = $node->compareTo($otherNode);
78  self::assertSame(0, $compareResult);
79  }
80 }
‪TYPO3\CMS\Backend\Tests\Unit\Tree\TreeNodeTest\deserializeFixture
‪deserializeFixture()
Definition: TreeNodeTest.php:60
‪TYPO3\CMS\Backend\Tree\TreeRepresentationNode
Definition: TreeRepresentationNode.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Tree\TreeNodeTest\compareNodes
‪compareNodes()
Definition: TreeNodeTest.php:69
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection
Definition: TreeNodeCollection.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode
Definition: TreeNode.php:25
‪TYPO3\CMS\Backend\Tests\Unit\Tree
Definition: SortedTreeNodeCollectionTest.php:18
‪TYPO3\CMS\Backend\Tests\Unit\Tree\TreeNodeTest\serializeFixture
‪serializeFixture()
Definition: TreeNodeTest.php:29
‪TYPO3\CMS\Backend\Tests\Unit\Tree\TreeNodeTest
Definition: TreeNodeTest.php:27