ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
MetricTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
31 
32 class MetricTest extends TestCase
33 {
34  #[\PHPUnit\Framework\Attributes\DataProvider('metricProvider')]
35  public function testConstructMetric(string $stability, string $type, $value, string $description, bool $success): void
36  {
37  if (!$success) {
38  $this->expectException(\InvalidArgumentException::class);
39  }
40  $metric = new Metrics\Metric($stability, $type, $value, $description);
41  $this->assertEquals($stability, $metric->getStability());
42  $this->assertEquals($type, $metric->getType());
43  $this->assertEquals($value, $metric->getValue());
44  $this->assertEquals($description, $metric->getDescription());
45  }
46 
47  public static function metricProvider(): array
48  {
53 
59  $collection = Metrics\Metric::TYPE_COLLECTION;
60 
61  $other_metric = new Metrics\Metric($volatile, $bool, true);
62 
63  return [
64  "invalid_stability" => ["no_stability", $bool, true, "", false],
65  "invalid_type" => [$config, "no_type", true, "", false],
66 
67  "bool" => [$config, $bool, true, "A boolean", true],
68  "counter" => [$stable, $counter, 23, "A counter", true],
69  "gauge1" => [$volatile, $gauge, 42, "A gauge", true],
70  "gauge2" => [$volatile, $gauge, 13.37, "A gauge", true],
71  "timestamp" => [$config, $timestamp, new \DateTimeImmutable(), "A timestamp", true],
72  "text" => [$stable, $text, "some text", "A text", true],
73  "collection" => [$volatile, $collection, ["other" => $other_metric], "A collection", true],
74 
75  "no_bool1" => [$config, $bool, 1, "", false],
76  "no_bool2" => [$config, $bool, "foo", "", false],
77  "no_bool3" => [$config, $bool, new \DateTimeImmutable(), "", false],
78  "no_bool4" => [$config, $bool, [], "", false],
79 
80  "no_counter1" => [$stable, $counter, false, "", false],
81  "no_counter2" => [$stable, $counter, 3.1, "", false],
82  "no_counter3" => [$stable, $counter, "foo", "", false],
83  "no_counter4" => [$stable, $counter, new \DateTimeImmutable(), "", false],
84  "no_counter5" => [$stable, $counter, [], "", false],
85 
86  "no_gauge1" => [$volatile, $gauge, true, "", false],
87  "no_gauge2" => [$volatile, $gauge, "foo", "", false],
88  "no_gauge3" => [$volatile, $gauge, new \DateTimeImmutable(), "", false],
89  "no_gauge4" => [$volatile, $gauge, [], "", false],
90 
91  "no_timestamp1" => [$config, $timestamp, false, "", false],
92  "no_timestamp2" => [$config, $timestamp, 1, "", false],
93  "no_timestamp3" => [$config, $timestamp, "foo", "", false],
94  "no_timestamp4" => [$config, $timestamp, [], "", false],
95 
96  "no_text1" => [$stable, $text, true, "", false],
97  "no_text2" => [$stable, $text, 1, "", false],
98  "no_text3" => [$stable, $text, new \DateTimeImmutable(), "", false],
99  "no_text4" => [$stable, $text, [], "", false],
100 
101  "no_collection1" => [$volatile, $collection, false, "", false],
102  "no_collection2" => [$volatile, $collection, 1, "", false],
103  "no_collection3" => [$volatile, $collection, new \DateTimeImmutable(), "", false],
104  "no_collection4" => [$volatile, $collection, "foo", "", false],
105  "no_collection5" => [$volatile, $collection, ["a"], "", false],
106 
107  "mixed_collection" => [$mixed, $collection, [], "", true],
108  "no_mixed_bool" => [$mixed, $bool, true, "", false],
109  "no_mixed_counter" => [$mixed, $counter, 1, "", false],
110  "no_mixed_gauge" => [$mixed, $gauge, 1.0, "", false],
111  "no_mixed_timestamp" => [$mixed, $timestamp, new \DateTimeImmutable(), "", false],
112  "no_mixed_text" => [$mixed, $text, "", "", false],
113  ];
114  }
115 
116  #[\PHPUnit\Framework\Attributes\DataProvider('typedMetricsProvider')]
117  public function testToYAML(M $metric, string $expected): void
118  {
119  $this->assertEquals($expected, $metric->toYAML());
120  }
121 
122  public static function typedMetricsProvider(): array
123  {
124  return [
125  "bool_true" => [new M(M::STABILITY_STABLE, M::TYPE_BOOL, true), "true"],
126  "bool_false" => [new M(M::STABILITY_STABLE, M::TYPE_BOOL, false), "false"],
127  "counter_0" => [new M(M::STABILITY_STABLE, M::TYPE_COUNTER, 0), "0"],
128  "counter_1337" => [new M(M::STABILITY_STABLE, M::TYPE_COUNTER, 1337), "1337"],
129  "gauge_23" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, 23), "23"],
130  "gauge_42_0" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, 42.0), "42.000"],
131  "gauge_42_001" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, 42.001), "42.001"],
132  "timestamp" => [new M(M::STABILITY_STABLE, M::TYPE_TIMESTAMP, new \DateTimeImmutable("1985-05-04T13:37:00+01:00")), "1985-05-04T13:37:00+0100"],
133  "text" => [new M(M::STABILITY_STABLE, M::TYPE_TEXT, "some text"), "some text"],
134  "text_with_nl" => [new M(M::STABILITY_STABLE, M::TYPE_TEXT, "some\ntext"), ">\nsome\ntext"],
135  ];
136  }
137 
138  public function testIndentation(): void
139  {
140  $metrics = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
141  "a" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
142  "h" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_h"),
143  "c" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
144  "d" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
145  "e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_e"),
146  "f" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_f")
147  ]),
148  "g" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_g")
149  ]),
150  "i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_i\na_i")
151  ]),
152  "b" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
153  "j" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "b_j")
154  ]),
155  "k" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "k")
156  ]);
157 
158  $expected = <<<METRIC
159 a:
160  h: a_h
161  c:
162  d:
163  e: a_c_d_e
164  f: a_c_d_f
165  g: a_c_g
166  i: >
167  a_i
168  a_i
169 b:
170  j: b_j
171 k: k
172 METRIC;
173 
174  $this->assertEquals($expected, $metrics->toYAML());
175  }
176 
177  public function testExtractBySeverity(): void
178  {
179  $metrics = new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
180  "a" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
181  "h" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_h"),
182  "c" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
183  "d" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
184  "e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_e"),
185  "f" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "a_c_d_f")
186  ]),
187  "g" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_c_g")
188  ]),
189  "i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_i\na_i")
190  ]),
191  "b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
192  "j" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "b_j")
193  ]),
194  "k" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "k")
195  ]);
196 
197  $expected_extracted = new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, [
198  "a" => new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, [
199  "h" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_h"),
200  "c" => new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, [
201  "g" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_c_g")
202  ]),
203  ]),
204  "k" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "k")
205  ]);
206  $expected_rest = new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
207  "a" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
208  "c" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
209  "d" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
210  "e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_e"),
211  "f" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "a_c_d_f")
212  ])
213  ]),
214  "i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_i\na_i")
215  ]),
216  "b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
217  "j" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "b_j")
218  ])
219  ]);
220 
221  list($extracted, $rest) = $metrics->extractByStability(M::STABILITY_CONFIG);
222 
223  $this->assertEquals($expected_extracted, $extracted);
224  $this->assertEquals($expected_rest, $rest);
225  }
226 
227  #[\PHPUnit\Framework\Attributes\DataProvider('typedMetricsProvider')]
228  public function testToArrayWithFlatValues(M $metric, string $expected): void
229  {
230  $this->assertEquals($expected, $metric->toArray());
231  }
232 
233  public function testToArrayWithDeepOne(): void
234  {
235  $metric = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
236  "bool_true" => new M(M::STABILITY_STABLE, M::TYPE_BOOL, true)
237  ]);
238 
239  $this->assertEquals(["bool_true" => "true"], $metric->toArray());
240  }
241 
242  public function testToArrayWithDeepTwo(): void
243  {
244  $metric = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
245  "db" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
246  "bool_true" => new M(M::STABILITY_STABLE, M::TYPE_BOOL, true)
247  ])
248  ]);
249 
250  $this->assertEquals(["db" => ["bool_true" => "true"]], $metric->toArray());
251  }
252 
253  public function testToUIReport(): void
254  {
255  $factory = $this->createMock(Factory::class);
256  $listing_f = new LF();
257  $panel_f = new PF(
258  $listing_f,
259  new \ILIAS\UI\Implementation\Component\Panel\Secondary\Factory(),
260  );
261  $signal = new SignalGenerator();
262  $legacy_f = $this->createMock(\ILIAS\UI\Implementation\Component\Legacy\Factory::class);
263  $legacy = $legacy_f->content("<pre>string</pre>");
264 
265  $legacy_f
266  ->expects($this->once())
267  ->method("content")
268  ->willReturn($legacy);
269 
270  $factory
271  ->expects($this->once())
272  ->method("legacy")
273  ->willReturn($legacy_f)
274  ;
275 
276  $factory
277  ->expects($this->exactly(2))
278  ->method("panel")
279  ->willReturn($panel_f)
280  ;
281 
282  $metric = new M(M::STABILITY_STABLE, M::TYPE_TEXT, "string", "");
283 
284  $result = $metric->toUIReport($factory, "Status");
285 
286  $this->assertInstanceOf(Report::class, $result);
287  }
288 }
testConstructMetric(string $stability, string $type, $value, string $description, bool $success)
Definition: MetricTest.php:35
Interface Observer Contains several chained tasks and infos about them.
const TYPE_BOOL
The type of the metric tells what to expect of the values.
Definition: Metric.php:53
testToYAML(M $metric, string $expected)
Definition: MetricTest.php:117
A metric is something we can measure about the system.
Definition: Metric.php:33
testToArrayWithFlatValues(M $metric, string $expected)
Definition: MetricTest.php:228
Builds data types.
Definition: Factory.php:35
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Factory.php:21
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
const STABILITY_CONFIG
The stability of a metric tells how often we expect changes in the metric.
Definition: Metric.php:40