Completed
Push — master ( 5d8f2a...248c39 )
by Julian
13s
created

TestCase::addSkipped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
namespace ParaTest\Logging\JUnit;
3
4
/**
5
 * Class TestCase
6
 *
7
 * A simple data structure for tracking
8
 * the results of a testcase node in a
9
 * JUnit xml document
10
 *
11
 * @package ParaTest\Logging\JUnit
12
 */
13
class TestCase
14
{
15
    /**
16
     * @var string
17
     */
18
    public $name;
19
20
    /**
21
     * @var string
22
     */
23
    public $class;
24
25
    /**
26
     * @var string
27
     */
28
    public $file;
29
30
    /**
31
     * @var string
32
     */
33
    public $line;
34
35
    /**
36
     * @var int
37
     */
38
    public $assertions;
39
40
    /**
41
     * @var string
42
     */
43
    public $time;
44
45
    /**
46
     * Number of failures in this test case
47
     *
48
     * @var array
49
     */
50
    public $failures = [];
51
52
    /**
53
     * Number of errors in this test case
54
     * TODO: Not a number?
55
     *
56
     * @var array
57
     */
58
    public $errors = [];
59
60
    public $skipped = [];
61
62 54
    public function __construct(
63
        $name,
64
        $class,
65
        $file,
66
        $line,
67
        $assertions,
68
        $time
69
    ) {
70 54
        $this->name = $name;
71 54
        $this->class = $class;
72 54
        $this->file = $file;
73 54
        $this->line = $line;
74 54
        $this->assertions = $assertions;
75 54
        $this->time = $time;
76 54
    }
77
78
    /**
79
     * @param string $type
80
     * @param string $text
81
     */
82 48
    public function addFailure($type, $text)
83
    {
84 48
        $this->addDefect('failures', $type, $text);
85 48
    }
86
87
    /**
88
     * @param string $type
89
     * @param string $text
90
     */
91 49
    public function addError($type, $text)
92
    {
93 49
        $this->addDefect('errors', $type, $text);
94 49
    }
95
96
    /**
97
     * @param string $type
98
     * @param string $text
99
     */
100
    public function addSkipped($type, $text)
101
    {
102
        $this->addDefect('skipped', $type, $text);
103
    }
104
105
    /**
106
     * Add a defect type (error or failure)
107
     *
108
     * @param string $collName the name of the collection to add to
109
     * @param $type
110
     * @param $text
111
     */
112 49
    protected function addDefect($collName, $type, $text)
113
    {
114 49
        $this->{$collName}[] = [
115 49
            'type' => $type,
116 49
            'text' => trim($text)
117
        ];
118 49
    }
119
120
    /**
121
     * Add systemOut result on test (if has failed or have error)
122
     *
123
     * @param mixed $node
124
     * @return mixed
125
     */
126 54
    public static function addSystemOut($node)
127
    {
128 54
        $sys = 'system-out';
129
130 54
        if(!empty($node->failure)) {
131 48
            $node->failure = (string)$node->failure . (string)$node->{$sys};
132
        }
133
134 54
        if(!empty($node->error)) {
135 49
            $node->error = (string)$node->error . (string)$node->{$sys};
136
        }
137
138 54
        return $node;
139
    }
140
141
    /**
142
     * Factory method that creates a TestCase object
143
     * from a SimpleXMLElement
144
     *
145
     * @param \SimpleXMLElement $node
146
     * @return TestCase
147
     */
148 54
    public static function caseFromNode(\SimpleXMLElement $node)
149
    {
150 54
        $case = new TestCase(
151 54
            (string) $node['name'],
152 54
            (string) $node['class'],
153 54
            (string) $node['file'],
154 54
            (string) $node['line'],
155 54
            (string) $node['assertions'],
156 54
            (string) $node['time']
157
        );
158
159 54
        $node       = self::addSystemOut($node);
160 54
        $failures   = $node->xpath('failure');
161 54
        $skipped    = $node->xpath('skipped');
162 54
        $errors     = $node->xpath('error');
163
164
        // TODO: each will be deprecated in 7.2, change to foreach
165 54
        while (list( , $fail) = each($failures)) {
166 48
            $case->addFailure((string)$fail['type'], (string)$fail);
167
        }
168
169 54
        while (list( , $err) = each($errors)) {
170 49
            $case->addError((string)$err['type'], (string)$err);
171
        }
172
173 54
        while (list( , $err) = each($skipped)) {
174
            $case->addSkipped((string)$err['type'], (string)$err);
175
        }
176
177 54
        return $case;
178
    }
179
}
180