Completed
Push — master ( 8c06ff...a2f0f3 )
by Julian
02:34
created

TestCase::caseFromNode()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4.0023

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 18
cts 19
cp 0.9474
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 8
nop 1
crap 4.0023
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ParaTest\Logging\JUnit;
6
7
/**
8
 * Class TestCase.
9
 *
10
 * A simple data structure for tracking
11
 * the results of a testcase node in a
12
 * JUnit xml document
13
 */
14
class TestCase
15
{
16
    /**
17
     * @var string
18
     */
19
    public $name;
20
21
    /**
22
     * @var string
23
     */
24
    public $class;
25
26
    /**
27
     * @var string
28
     */
29
    public $file;
30
31
    /**
32
     * @var int
33
     */
34
    public $line;
35
36
    /**
37
     * @var int
38
     */
39
    public $assertions;
40
41
    /**
42
     * @var string|float (a stringified float, from phpunit XML output)
43
     */
44
    public $time;
45
46
    /**
47
     * List of failures in this test case.
48
     *
49
     * @var array
50
     */
51
    public $failures = [];
52
53
    /**
54
     * List of errors in this test case.
55
     *
56
     * @var array
57
     */
58
    public $errors = [];
59
60
    /** @var array */
61
    public $skipped = [];
62
63
    /**
64
     * @param string $name
65
     * @param string $class
66
     * @param string $file
67
     * @param int    $line
68
     * @param int    $assertions
69
     * @param string $time
70
     */
71 54
    public function __construct(
72
        string $name,
73
        string $class,
74
        string $file,
75
        int $line,
76
        int $assertions,
77
        string $time
78
    ) {
79 54
        $this->name = $name;
80 54
        $this->class = $class;
81 54
        $this->file = $file;
82 54
        $this->line = $line;
83 54
        $this->assertions = $assertions;
84 54
        $this->time = $time;
85 54
    }
86
87
    /**
88
     * @param string $type
89
     * @param string $text
90
     */
91 48
    public function addFailure(string $type, string $text)
92
    {
93 48
        $this->addDefect('failures', $type, $text);
94 48
    }
95
96
    /**
97
     * @param string $type
98
     * @param string $text
99
     */
100 49
    public function addError(string $type, string $text)
101
    {
102 49
        $this->addDefect('errors', $type, $text);
103 49
    }
104
105
    /**
106
     * @param string $type
107
     * @param string $text
108
     */
109
    public function addSkipped(string $type, string $text)
110
    {
111
        $this->addDefect('skipped', $type, $text);
112
    }
113
114
    /**
115
     * Add a defect type (error or failure).
116
     *
117
     * @param string $collName the name of the collection to add to
118
     * @param $type
119
     * @param $text
120
     */
121 49
    protected function addDefect(string $collName, string $type, string $text)
122
    {
123 49
        $this->{$collName}[] = [
124 49
            'type' => $type,
125 49
            'text' => trim($text),
126
        ];
127 49
    }
128
129
    /**
130
     * Add systemOut result on test (if has failed or have error).
131
     *
132
     * @param mixed $node
133
     *
134
     * @return mixed
135
     */
136 54
    public static function addSystemOut(\SimpleXMLElement $node): \SimpleXMLElement
137
    {
138 54
        $sys = 'system-out';
139
140 54
        if (!empty($node->failure)) {
141 48
            $node->failure = (string) $node->failure . (string) $node->{$sys};
0 ignored issues
show
Bug introduced by
The property failure does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
142
        }
143
144 54
        if (!empty($node->error)) {
145 49
            $node->error = (string) $node->error . (string) $node->{$sys};
0 ignored issues
show
Bug introduced by
The property error does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
146
        }
147
148 54
        return $node;
149
    }
150
151
    /**
152
     * Factory method that creates a TestCase object
153
     * from a SimpleXMLElement.
154
     *
155
     * @param \SimpleXMLElement $node
156
     *
157
     * @return TestCase
158
     */
159 54
    public static function caseFromNode(\SimpleXMLElement $node): self
160
    {
161 54
        $case = new self(
162 54
            (string) $node['name'],
163 54
            (string) $node['class'],
164 54
            (string) $node['file'],
165 54
            (int) $node['line'],
166 54
            (int) $node['assertions'],
167 54
            (string) $node['time']
168
        );
169
170 54
        $node = self::addSystemOut($node);
171 54
        $failures = $node->xpath('failure');
172 54
        $skipped = $node->xpath('skipped');
173 54
        $errors = $node->xpath('error');
174
175 54
        foreach ($failures as $fail) {
176 48
            $case->addFailure((string) $fail['type'], (string) $fail);
177
        }
178
179 54
        foreach ($errors as $err) {
180 49
            $case->addError((string) $err['type'], (string) $err);
181
        }
182
183 54
        foreach ($skipped as $skip) {
184
            $case->addSkipped((string) $skip['type'], (string) $skip);
185
        }
186
187 54
        return $case;
188
    }
189
}
190