ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BuilderTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
25use ILIAS\MetaData\Paths\NullBuilder as NullInternalBuilder;
30
31class BuilderTest extends TestCase
32{
37 protected function getBuilder(): Builder
38 {
39 $internal_builder = new class ('') extends NullInternalBuilder {
40 public function __construct(public string $path)
41 {
42 }
43
44 public function withNextStep(string $name, bool $add_as_first = false): BuilderInterface
45 {
46 return new self($this->path . ':' . $name);
47 }
48
49 public function withNextStepToSuperElement(bool $add_as_first = false): BuilderInterface
50 {
51 return new self($this->path . ':^');
52 }
53
54 public function withAdditionalFilterAtCurrentStep(
55 FilterType $type,
56 string ...$values
58 if ($this->path === '') {
59 throw new \ilMDPathException('failed');
60 }
61
62 $filter = '{' . $type->value . ';' . implode(',', $values) . '}';
63 return new self($this->path . $filter);
64 }
65
66 public function get(): PathInterface
67 {
68 if (str_contains($this->path, ':INVALID')) {
69 throw new \ilMDPathException('failed');
70 }
71
72 return new class ($this->path) extends NullPath {
73 public function __construct(public string $path_string)
74 {
75 }
76 };
77 }
78 };
79
80 return new class ($internal_builder) extends Builder {
81 public function __construct(BuilderInterface $internal_builder)
82 {
83 parent::__construct($internal_builder);
84 }
85
86 public function exposePath(): string
87 {
88 return $this->internal_builder->path;
89 }
90 };
91 }
92
93 public function testWithNextStep(): void
94 {
95 $builder = $this->getBuilder();
96 $builder1 = $builder->withNextStep('step');
97
98 $this->assertSame(
99 '',
100 $builder->exposePath()
101 );
102 $this->assertSame(
103 ':step',
104 $builder1->exposePath()
105 );
106 }
107
108 public function testWithNextStepToSuperElement(): void
109 {
110 $builder = $this->getBuilder();
111 $builder1 = $builder->withNextStepToSuperElement();
112
113 $this->assertSame(
114 '',
115 $builder->exposePath()
116 );
117 $this->assertSame(
118 ':^',
119 $builder1->exposePath()
120 );
121 }
122
124 {
125 $builder = $this->getBuilder();
126 $builder1 = $builder->withNextStep('step')
127 ->withAdditionalFilterAtCurrentStep(FilterType::DATA, 'v1', 'v2');
128
129 $this->assertSame(
130 '',
131 $builder->exposePath()
132 );
133 $this->assertSame(
134 ':step{data;v1,v2}',
135 $builder1->exposePath()
136 );
137 }
138
140 {
141 $builder = $this->getBuilder();
142
143 $this->expectException(\ilMDServicesException::class);
144 $builder->withAdditionalFilterAtCurrentStep(FilterType::DATA);
145 }
146
147 public function testGet(): void
148 {
149 $builder = $this->getBuilder()
150 ->withNextStep('step1')
151 ->withNextStepToSuperElement()
152 ->withNextStep('step2')
153 ->withAdditionalFilterAtCurrentStep(FilterType::MDID, '12');
154 $builder3 = $builder
155 ->withNextStep('step3')
156 ->withNextStepToSuperElement();
157
158 $this->assertSame(
159 ':step1:^:step2{id;12}',
160 $builder->get()->path_string
161 );
162 $this->assertSame(
163 ':step1:^:step2{id;12}:step3:^',
164 $builder3->get()->path_string
165 );
166 }
167
168 public function testGetException(): void
169 {
170 $builder = $this->getBuilder()->withNextStep('INVALID');
171
172 $this->expectException(\ilMDServicesException::class);
173 $builder->get();
174 }
175}
getBuilder()
Builder will throw an exception on get if the path contains a step with name INVALID.
Definition: BuilderTest.php:37
__construct(PathFactory $path_factory)
Definition: Paths.php:31
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
FilterType
Values should always be all lowercase.
Definition: FilterType.php:27