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