ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CreatorTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
26use ILIAS\MetaData\Paths\NullFactory as NullPathFactory;
29use ILIAS\MetaData\Paths\NullBuilder as NullPathBuilder;
37
38class CreatorTest extends TestCase
39{
40 protected function getCreator(): Creator
41 {
42 $manipulator = new class () extends NullManipulator {
43 public function prepareCreateOrUpdate(
44 SetInterface $set,
46 string ...$values
47 ): SetInterface {
48 $set = clone $set;
49 $set->prepared_changes[] = [
50 'path' => $path->toString(),
51 'values' => $values
52 ];
53 return $set;
54 }
55
56 public function prepareDelete(SetInterface $set, PathInterface $path): SetInterface
57 {
58 $set = clone $set;
59 $set->prepared_changes[] = ['delete should not be prepared!'];
60 return $set;
61 }
62
63 public function prepareForceCreate(
64 SetInterface $set,
66 string ...$values
67 ): SetInterface {
68 $set = clone $set;
69 $set->prepared_changes[] = ['force create should not be prepared!'];
70 return $set;
71 }
72 };
73
74 $builder = new class () extends NullPathBuilder {
75 protected string $path_string = '~start~';
76
77 public function withNextStep(string $name, bool $add_as_first = false): PathBuilder
78 {
79 $builder = clone $this;
80 if ($add_as_first) {
81 $name .= '[added as first]';
82 }
83 $builder->path_string .= '%' . $name;
84 return $builder;
85 }
86
87 public function withAdditionalFilterAtCurrentStep(FilterType $type, string ...$values): PathBuilder
88 {
89 $builder = clone $this;
90 $builder->path_string .= '{' . $type->value . ':' . implode('><', $values) . '}';
91 return $builder;
92 }
93
94 public function get(): PathInterface
95 {
96 return new class ($this->path_string) extends NullPath {
97 public function __construct(protected string $path_string)
98 {
99 }
100
101 public function toString(): string
102 {
103 return $this->path_string;
104 }
105 };
106 }
107 };
108
109 $path_factory = new class ($builder) extends NullPathFactory {
110 public function __construct(protected PathBuilder $builder)
111 {
112 }
113
114 public function custom(): PathBuilder
115 {
116 return $this->builder;
117 }
118 };
119
120 $scaffold_provider = new class () extends NullScaffoldProvider {
121 public function set(): SetInterface
122 {
123 return new class () extends NullSet {
124 public array $prepared_changes = [];
125 };
126 }
127 };
128
129 return new Creator($manipulator, $path_factory, $scaffold_provider);
130 }
131
132 public function testCreateSet(): void
133 {
134 $creator = $this->getCreator();
135
136 $set = $creator->createSet('some title');
137
138 $expected_title_changes = [
139 'path' => '~start~%general%title%string',
140 'values' => ['some title']
141 ];
142 $prepared_changes = $set->prepared_changes;
143 $this->assertCount(1, $prepared_changes);
144 $this->assertContains($expected_title_changes, $prepared_changes);
145 }
146
147 public function testCreateSetWithEmptyTitle(): void
148 {
149 $creator = $this->getCreator();
150
151 $set = $creator->createSet('');
152
153 $expected_title_changes = [
154 'path' => '~start~%general%title%string',
155 'values' => ['some title']
156 ];
157 $prepared_changes = $set->prepared_changes;
158 $this->assertCount(1, $prepared_changes);
159 $this->assertSame('~start~%general%title%string', $prepared_changes[0]['path']);
160 $this->assertCount(1, $prepared_changes[0]['values']);
161 $this->assertNotSame('', $prepared_changes[0]['values'][0]);
162 }
163
164 public function testCreateSetWithLanguage(): void
165 {
166 $creator = $this->getCreator();
167
168 $set = $creator->createSet('some title', '', 'tg');
169
170 $expected_title_changes = [
171 'path' => '~start~%general%title%string',
172 'values' => ['some title']
173 ];
174 $expected_title_lang_changes = [
175 'path' => '~start~%general%title%language',
176 'values' => ['tg']
177 ];
178 $expected_lang_changes = [
179 'path' => '~start~%general%language',
180 'values' => ['tg']
181 ];
182 $prepared_changes = $set->prepared_changes;
183 $this->assertCount(3, $prepared_changes);
184 $this->assertContains($expected_title_changes, $prepared_changes);
185 $this->assertContains($expected_title_lang_changes, $prepared_changes);
186 $this->assertContains($expected_lang_changes, $prepared_changes);
187 }
188
189 public function testCreateSetWithDescription(): void
190 {
191 $creator = $this->getCreator();
192
193 $set = $creator->createSet('some title', 'some description');
194
195 $expected_title_changes = [
196 'path' => '~start~%general%title%string',
197 'values' => ['some title']
198 ];
199 $expected_description_changes = [
200 'path' => '~start~%general%description%string',
201 'values' => ['some description']
202 ];
203 $prepared_changes = $set->prepared_changes;
204 $this->assertCount(2, $prepared_changes);
205 $this->assertContains($expected_title_changes, $prepared_changes);
206 $this->assertContains($expected_description_changes, $prepared_changes);
207 }
208
210 {
211 $creator = $this->getCreator();
212
213 $set = $creator->createSet('some title', 'some description', 'tg');
214
215 $expected_title_changes = [
216 'path' => '~start~%general%title%string',
217 'values' => ['some title']
218 ];
219 $expected_title_lang_changes = [
220 'path' => '~start~%general%title%language',
221 'values' => ['tg']
222 ];
223 $expected_lang_changes = [
224 'path' => '~start~%general%language',
225 'values' => ['tg']
226 ];
227 $expected_description_changes = [
228 'path' => '~start~%general%description%string',
229 'values' => ['some description']
230 ];
231 $expected_description_lang_changes = [
232 'path' => '~start~%general%description%language',
233 'values' => ['tg']
234 ];
235 $prepared_changes = $set->prepared_changes;
236 $this->assertCount(5, $prepared_changes);
237 $this->assertContains($expected_title_changes, $prepared_changes);
238 $this->assertContains($expected_title_lang_changes, $prepared_changes);
239 $this->assertContains($expected_lang_changes, $prepared_changes);
240 $this->assertContains($expected_description_changes, $prepared_changes);
241 $this->assertContains($expected_description_lang_changes, $prepared_changes);
242 }
243}
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
$path
Definition: ltiservices.php:30
FilterType
Values should always be all lowercase.
Definition: FilterType.php:27