ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
MetricTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2019 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
6 
15 
16 class MetricTest extends TestCase
17 {
21  public function testConstructMetric($stability, $type, $value, $description, $success)
22  {
23  if (!$success) {
24  $this->expectException(\InvalidArgumentException::class);
25  }
26  $metric = new Metrics\Metric($stability, $type, $value, $description);
27  $this->assertEquals($stability, $metric->getStability());
28  $this->assertEquals($type, $metric->getType());
29  $this->assertEquals($value, $metric->getValue());
30  $this->assertEquals($description, $metric->getDescription());
31  }
32 
33  public function metricProvider()
34  {
39 
45  $collection = Metrics\Metric::TYPE_COLLECTION;
46 
47  $other_metric = new Metrics\Metric($volatile, $bool, true);
48 
49  return [
50  "invalid_stability" => ["no_stability", $bool, true, "", false],
51  "invalid_type" => [$config, "no_type", true, "", false],
52 
53  "bool" => [$config, $bool, true, "A boolean", true],
54  "counter" => [$stable, $counter, 23, "A counter", true],
55  "gauge1" => [$volatile, $gauge, 42, "A gauge", true],
56  "gauge2" => [$volatile, $gauge, 13.37, "A gauge", true],
57  "timestamp" => [$config, $timestamp, new \DateTimeImmutable(), "A timestamp", true],
58  "text" => [$stable, $text, "some text", "A text", true],
59  "collection" => [$volatile, $collection, ["other" => $other_metric], "A collection", true],
60 
61  "no_bool1" => [$config, $bool, 1, "", false],
62  "no_bool2" => [$config, $bool, "foo", "", false],
63  "no_bool3" => [$config, $bool, new \DateTimeImmutable(), "", false],
64  "no_bool4" => [$config, $bool, [], "", false],
65 
66  "no_counter1" => [$stable, $counter, false, "", false],
67  "no_counter2" => [$stable, $counter, 3.1, "", false],
68  "no_counter3" => [$stable, $counter, "foo", "", false],
69  "no_counter4" => [$stable, $counter, new \DateTimeImmutable(), "", false],
70  "no_counter5" => [$stable, $counter, [], "", false],
71 
72  "no_gauge1" => [$volatile, $gauge, true, "", false],
73  "no_gauge2" => [$volatile, $gauge, "foo", "", false],
74  "no_gauge3" => [$volatile, $gauge, new \DateTimeImmutable(), "", false],
75  "no_gauge4" => [$volatile, $gauge, [], "", false],
76 
77  "no_timestamp1" => [$config, $timestamp, false, "", false],
78  "no_timestamp2" => [$config, $timestamp, 1, "", false],
79  "no_timestamp3" => [$config, $timestamp, "foo", "", false],
80  "no_timestamp4" => [$config, $timestamp, [], "", false],
81 
82  "no_text1" => [$stable, $text, true, "", false],
83  "no_text2" => [$stable, $text, 1, "", false],
84  "no_text3" => [$stable, $text, new \DateTimeImmutable(), "", false],
85  "no_text4" => [$stable, $text, [], "", false],
86 
87  "no_collection1" => [$volatile, $collection, false, "", false],
88  "no_collection2" => [$volatile, $collection, 1, "", false],
89  "no_collection3" => [$volatile, $collection, new \DateTimeImmutable(), "", false],
90  "no_collection4" => [$volatile, $collection, "foo", "", false],
91  "no_collection5" => [$volatile, $collection, ["a"], "", false],
92 
93  "mixed_collection" => [$mixed, $collection, [], "", true],
94  "no_mixed_bool" => [$mixed, $bool, true, "", false],
95  "no_mixed_counter" => [$mixed, $counter, 1, "", false],
96  "no_mixed_gauge" => [$mixed, $gauge, 1.0, "", false],
97  "no_mixed_timestamp" => [$mixed, $timestamp, new \DateTimeImmutable(), "", false],
98  "no_mixed_text" => [$mixed, $text, "", "", false],
99  ];
100  }
101 
105  public function testToYAML(M $metric, string $expected)
106  {
107  $this->assertEquals($expected, $metric->toYAML());
108  }
109 
110  public function typedMetricsProvider()
111  {
112  return [
113  "bool_true" => [new M(M::STABILITY_STABLE, M::TYPE_BOOL, true), "true"],
114  "bool_false" => [new M(M::STABILITY_STABLE, M::TYPE_BOOL, false), "false"],
115  "counter_0" => [new M(M::STABILITY_STABLE, M::TYPE_COUNTER, 0), "0"],
116  "counter_1337" => [new M(M::STABILITY_STABLE, M::TYPE_COUNTER, 1337), "1337"],
117  "gauge_23" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, 23), "23"],
118  "gauge_42_0" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, 42.0), "42.000"],
119  "gauge_42_001" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, 42.001), "42.001"],
120  "timestamp" => [new M(M::STABILITY_STABLE, M::TYPE_TIMESTAMP, new \DateTimeImmutable("1985-05-04T13:37:00+01:00")), "1985-05-04T13:37:00+0100"],
121  "text" => [new M(M::STABILITY_STABLE, M::TYPE_TEXT, "some text"), "some text"],
122  "text_with_nl" => [new M(M::STABILITY_STABLE, M::TYPE_TEXT, "some\ntext"), ">\nsome\ntext"],
123  ];
124  }
125 
126  public function testIndentation()
127  {
128  $metrics = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
129  "a" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
130  "h" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_h"),
131  "c" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
132  "d" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
133  "e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_e"),
134  "f" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_f")
135  ]),
136  "g" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_g")
137  ]),
138  "i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_i\na_i")
139  ]),
140  "b" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
141  "j" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "b_j")
142  ]),
143  "k" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "k")
144  ]);
145 
146  $expected = <<<METRIC
147 a:
148  h: a_h
149  c:
150  d:
151  e: a_c_d_e
152  f: a_c_d_f
153  g: a_c_g
154  i: >
155  a_i
156  a_i
157 b:
158  j: b_j
159 k: k
160 METRIC;
161 
162  $this->assertEquals($expected, $metrics->toYAML());
163  }
164 
165  public function testExtractBySeverity()
166  {
167  $metrics = new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
168  "a" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
169  "h" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_h"),
170  "c" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
171  "d" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
172  "e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_e"),
173  "f" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "a_c_d_f")
174  ]),
175  "g" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_c_g")
176  ]),
177  "i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_i\na_i")
178  ]),
179  "b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
180  "j" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "b_j")
181  ]),
182  "k" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "k")
183  ]);
184 
185  $expected_extracted = new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, [
186  "a" => new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, [
187  "h" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_h"),
188  "c" => new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, [
189  "g" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_c_g")
190  ]),
191  ]),
192  "k" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "k")
193  ]);
194  $expected_rest = new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
195  "a" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
196  "c" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
197  "d" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
198  "e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_e"),
199  "f" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "a_c_d_f")
200  ])
201  ]),
202  "i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_i\na_i")
203  ]),
204  "b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
205  "j" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "b_j")
206  ])
207  ]);
208 
209  list($extracted, $rest) = $metrics->extractByStability(M::STABILITY_CONFIG);
210 
211  $this->assertEquals($expected_extracted, $extracted);
212  $this->assertEquals($expected_rest, $rest);
213  }
214 
218  public function testToArrayWithFlatValues(M $metric, string $expected)
219  {
220  $this->assertEquals($expected, $metric->toArray());
221  }
222 
223  public function testToArrayWithDeepOne()
224  {
225  $metric = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
226  "bool_true" => new M(M::STABILITY_STABLE, M::TYPE_BOOL, true)
227  ]);
228 
229  $this->assertEquals(["bool_true" => "true"] , $metric->toArray());
230  }
231 
232  public function testToArrayWithDeepTwo()
233  {
234  $metric = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
235  "db" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
236  "bool_true" => new M(M::STABILITY_STABLE, M::TYPE_BOOL, true)
237  ])
238  ]);
239 
240  $this->assertEquals(["db" => ["bool_true" => "true"]] , $metric->toArray());
241  }
242 
243  public function testToUIReport()
244  {
245  $factory = $this->createMock(Factory::class);
246  $listing_f = new LF();
247  $panel_f = new PF($listing_f);
248  $signal = new SignalGenerator();
249  $legacy_f = new \ILIAS\UI\Implementation\Component\Legacy\Factory($signal);
250  $legacy = $legacy_f->legacy("<pre>string</pre>");
251 
252  $factory
253  ->expects($this->once())
254  ->method("legacy")
255  ->with("<pre>" . "string" . "</pre>")
256  ->willReturn($legacy)
257  ;
258 
259  $factory
260  ->expects($this->exactly(2))
261  ->method("panel")
262  ->willReturn($panel_f)
263  ;
264 
265  $metric = new M(M::STABILITY_STABLE, M::TYPE_TEXT, "string", "");
266 
267  $result = $metric->toUIReport($factory, "Status");
268 
269  $this->assertInstanceOf(Report::class, $result);
270  }
271 }
$rest
Definition: goto.php:48
$result
$type
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
const TYPE_BOOL
The type of the metric tells what to expect of the values.
Definition: Metric.php:37
testToYAML(M $metric, string $expected)
typedMetricsProvider
Definition: MetricTest.php:105
A metric is something we can measure about the system.
Definition: Metric.php:17
$success
Definition: Utf8Test.php:86
testToArrayWithFlatValues(M $metric, string $expected)
typedMetricsProvider
Definition: MetricTest.php:218
testConstructMetric($stability, $type, $value, $description, $success)
metricProvider
Definition: MetricTest.php:21
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
const STABILITY_CONFIG
The stability of a metric tells how often we expect changes in the metric.
Definition: Metric.php:24
$factory
Definition: metadata.php:58