ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BaseElementTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
28
29class BaseElementTest extends TestCase
30{
31 protected function getBaseElement(
32 int|NoID $md_id,
33 string $name,
34 BaseElement ...$sub_elements
35 ): BaseElement {
36 $definition = $this->getDefinition($name);
37 return new class ($md_id, $definition, ...$sub_elements) extends BaseElement {
38 public function __construct(
39 NoID|int $md_id,
40 DefinitionInterface $definition,
41 BaseElement ...$sub_elements
42 ) {
43 parent::__construct($md_id, $definition, ...$sub_elements);
44 }
45 };
46 }
47
48 protected function getDefinition(string $name): DefinitionInterface
49 {
50 return new class ($name) extends NullDefinition {
51 public function __construct(protected string $name)
52 {
53 }
54
55 public function name(): string
56 {
57 return $this->name;
58 }
59 };
60 }
61
62 public function testSubAndSuperElements(): void
63 {
64 $el11 = $this->getBaseElement(11, '1.1');
65 $el21 = $this->getBaseElement(21, '2.1');
66 $el22 = $this->getBaseElement(22, '2.2');
67
68 $el1 = $this->getBaseElement(1, '1', $el11);
69 $el2 = $this->getBaseElement(2, '2', $el21, $el22);
70
71 $root = $this->getBaseElement(NoID::ROOT, 'root', $el1, $el2);
72
73 $this->assertNull($root->getSuperElement());
74 $this->assertSame($root, $el2->getSuperElement());
75
76 $subs = $el2->getSubElements();
77 $this->assertSame($subs->current(), $el21);
78 $subs->next();
79 $this->assertSame($subs->current(), $el22);
80 $subs->next();
81 $this->assertNull($subs->current());
82
83 $this->assertSame($el2, $el21->getSuperElement());
84 $this->assertNull($el21->getSubElements()->current());
85 }
86
87 public function testClone(): void
88 {
89 $el11 = $this->getBaseElement(11, '1.1');
90 $el1 = $this->getBaseElement(1, '1', $el11);
91 $root = $this->getBaseElement(NoID::ROOT, 'root', $el1);
92
93 $cloned_root = clone $root;
94 $cloned_subs = $cloned_root->getSubElements();
95 $cloned_el = $cloned_subs->current();
96 $this->assertEquals($el1, $cloned_el);
97 $this->assertNotSame($el1, $cloned_el);
98 $this->assertSame($cloned_root, $cloned_el->getSuperElement());
99 $cloned_subs->next();
100 $this->assertNull($cloned_subs->current());
101
102 $cloned_subs = $cloned_el->getSubElements();
103 $this->assertEquals($el11, $cloned_subs->current());
104 $this->assertNotSame($el11, $cloned_subs->current());
105 $this->assertSame($cloned_el, $cloned_subs->current()->getSuperElement());
106 $cloned_subs->next();
107 $this->assertNull($cloned_subs->current());
108
109 $cloned_el1 = clone $el1;
110 $this->assertNull($cloned_el1->getSuperElement());
111 }
112
113 public function testRootAsSubElementException(): void
114 {
115 $root = $this->getBaseElement(NoID::ROOT, 'root');
116
117 $this->expectException(\ilMDElementsException::class);
118 $not_root = $this->getBaseElement(13, 'name', $root);
119 }
120
121 public function testMDIDAndIsRoot(): void
122 {
123 $root = $this->getBaseElement(NoID::ROOT, 'root');
124 $not_root = $this->getBaseElement(13, 'name');
125
126 $this->assertSame(NoID::ROOT, $root->getMDID());
127 $this->assertSame(13, $not_root->getMDID());
128
129 $this->assertTrue($root->isRoot());
130 $this->assertFalse($not_root->isRoot());
131 }
132
133 public function testDefinition(): void
134 {
135 $def = $this->getDefinition('name');
136 $el = new class (13, $def) extends BaseElement {
137 public function __construct(
138 NoID|int $md_id,
139 DefinitionInterface $definition
140 ) {
141 parent::__construct($md_id, $definition);
142 }
143 };
144
145 $this->assertSame($def, $el->getDefinition());
146 }
147}
getBaseElement(int|NoID $md_id, string $name, BaseElement ... $sub_elements)
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc