TestCase::addDefect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
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 55
    public function __construct(
72
        string $name,
73
        string $class,
74
        string $file,
75
        int $line,
76
        int $assertions,
77
        string $time
78
    ) {
79 55
        $this->name = $name;
80 55
        $this->class = $class;
81 55
        $this->file = $file;
82 55
        $this->line = $line;
83 55
        $this->assertions = $assertions;
84 55
        $this->time = $time;
85 55
    }
86
87
    /**
88
     * @param string $type
89
     * @param string $text
90
     */
91 49
    public function addFailure(string $type, string $text)
92
    {
93 49
        $this->addDefect('failures', $type, $text);
94 49
    }
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 50
    protected function addDefect(string $collName, string $type, string $text)
122
    {
123 50
        $this->{$collName}[] = [
124 50
            'type' => $type,
125 50
            'text' => trim($text),
126
        ];
127 50
    }
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 55
    public static function addSystemOut(\SimpleXMLElement $node): \SimpleXMLElement
137
    {
138 55
        $sys = 'system-out';
139
140 55
        if (!empty($node->failure)) {
141 49
            $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 55
        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 55
        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 55
    public static function caseFromNode(\SimpleXMLElement $node): self
160
    {
161 55
        $case = new self(
162 55
            (string) $node['name'],
163 55
            (string) $node['class'],
164 55
            (string) $node['file'],
165 55
            (int) $node['line'],
166 55
            (int) $node['assertions'],
167 55
            (string) $node['time']
168
        );
169
170 55
        $node = self::addSystemOut($node);
171 55
        $failures = $node->xpath('failure');
172 55
        $skipped = $node->xpath('skipped');
173 55
        $errors = $node->xpath('error');
174
175 55
        foreach ($failures as $fail) {
176 49
            $case->addFailure((string) $fail['type'], (string) $fail);
177
        }
178
179 55
        foreach ($errors as $err) {
180 49
            $case->addError((string) $err['type'], (string) $err);
181
        }
182
183 55
        foreach ($skipped as $skip) {
184
            $case->addSkipped((string) $skip['type'], (string) $skip);
185
        }
186
187 55
        return $case;
188
    }
189
}
190