|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ParaTest\Logging; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class MetaProvider. |
|
9
|
|
|
* |
|
10
|
|
|
* Adds __call behavior to a logging object |
|
11
|
|
|
* for aggregating totals and messages |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class MetaProvider |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* This pattern is used to see whether a missing |
|
17
|
|
|
* method is a "total" method or not. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $totalMethod = '/^getTotal([\w]+)$/'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This pattern is used to add message retrieval for a given |
|
25
|
|
|
* type - i.e getFailures() or getErrors(). |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $messageMethod = '/^get((Failure|Error)s)$/'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Simplify aggregation of totals or messages. |
|
33
|
|
|
* |
|
34
|
|
|
* @param mixed $method |
|
35
|
|
|
* @param mixed $args |
|
36
|
|
|
*/ |
|
37
|
22 |
|
public function __call(string $method, array $args) |
|
38
|
|
|
{ |
|
39
|
22 |
|
if (preg_match(self::$totalMethod, $method, $matches) && $property = strtolower($matches[1])) { |
|
40
|
14 |
|
return $this->getNumericValue($property); |
|
41
|
|
|
} |
|
42
|
10 |
|
if (preg_match(self::$messageMethod, $method, $matches) && $type = strtolower($matches[1])) { |
|
43
|
10 |
|
return $this->getMessages($type); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Return a value as a float or integer. |
|
49
|
|
|
* |
|
50
|
|
|
* @param $property |
|
51
|
|
|
* |
|
52
|
|
|
* @return float|int |
|
53
|
|
|
*/ |
|
54
|
14 |
|
protected function getNumericValue(string $property) |
|
55
|
|
|
{ |
|
56
|
14 |
|
return ($property === 'time') |
|
57
|
2 |
|
? (float) ($this->suites[0]->$property) |
|
|
|
|
|
|
58
|
14 |
|
: (int) ($this->suites[0]->$property); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return messages for a given type. |
|
63
|
|
|
* |
|
64
|
|
|
* @param $type |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
10 |
|
protected function getMessages(string $type): array |
|
69
|
|
|
{ |
|
70
|
10 |
|
$messages = []; |
|
71
|
10 |
|
$suites = $this->isSingle ? $this->suites : $this->suites[0]->suites; |
|
|
|
|
|
|
72
|
10 |
|
foreach ($suites as $suite) { |
|
73
|
|
|
$messages = array_merge($messages, array_reduce($suite->cases, function ($result, $case) use ($type) { |
|
74
|
10 |
|
return array_merge($result, array_reduce($case->$type, function ($msgs, $msg) { |
|
75
|
10 |
|
$msgs[] = $msg['text']; |
|
76
|
|
|
|
|
77
|
10 |
|
return $msgs; |
|
78
|
10 |
|
}, [])); |
|
79
|
|
|
}, [])); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $messages; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: