Completed
Push — master ( 54fb04...ea73dc )
by Julian
9s
created

TestCase   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 10
c 8
b 1
f 1
lcom 1
cbo 0
dl 0
loc 149
ccs 48
cts 48
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A addFailure() 0 4 1
A addError() 0 4 1
A addDefect() 0 7 1
A addSystemOut() 0 14 3
B caseFromNode() 0 25 3
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 = array();
51
52
    /**
53
     * Number of errors in this test case
54
     *
55
     * @var array
56
     */
57
    public $errors = array();
58
59 52
    public function __construct(
60
        $name,
61
        $class,
62
        $file,
63
        $line,
64
        $assertions,
65
        $time
66
    ) {
67 52
        $this->name = $name;
68 52
        $this->class = $class;
69 52
        $this->file = $file;
70 52
        $this->line = $line;
71 52
        $this->assertions = $assertions;
72 52
        $this->time = $time;
73 52
    }
74
75
    /**
76
     * @param string $type
77
     * @param string $text
78
     */
79 46
    public function addFailure($type, $text)
80
    {
81 46
        $this->addDefect('failures', $type, $text);
82 46
    }
83
84
    /**
85
     * @param string $type
86
     * @param string $text
87
     */
88 47
    public function addError($type, $text)
89
    {
90 47
        $this->addDefect('errors', $type, $text);
91 47
    }
92
93
    /**
94
     * Add a defect type (error or failure)
95
     *
96
     * @param string $collName the name of the collection to add to
97
     * @param $type
98
     * @param $text
99
     */
100 47
    protected function addDefect($collName, $type, $text)
101
    {
102 47
        $this->{$collName}[] = array(
103 47
            'type' => $type,
104 47
            'text' => trim($text)
105 47
        );
106 47
    }
107
108
    /**
109
     * Add systemOut result on test (if has failed or have error)
110
     *
111
     * @param mixed $node
112
     * @return mixed
113
     */
114 52
    public static function addSystemOut($node)
115
    {
116 52
        $sys = 'system-out';
117
118 52
        if(!empty($node->failure)) {
119 46
            $node->failure = (string)$node->failure . (string)$node->{$sys};
120 46
        }
121
122 52
        if(!empty($node->error)) {
123 47
            $node->error = (string)$node->error . (string)$node->{$sys};
124 47
        }
125
126 52
        return $node;
127
    }
128
129
    /**
130
     * Factory method that creates a TestCase object
131
     * from a SimpleXMLElement
132
     *
133
     * @param \SimpleXMLElement $node
134
     * @return TestCase
135
     */
136 52
    public static function caseFromNode(\SimpleXMLElement $node)
137
    {
138 52
        $case = new TestCase(
139 52
            (string) $node['name'],
140 52
            (string) $node['class'],
141 52
            (string) $node['file'],
142 52
            (string) $node['line'],
143 52
            (string) $node['assertions'],
144 52
            (string) $node['time']
145 52
        );
146
147 52
        $node       = self::addSystemOut($node);
148 52
        $failures   = $node->xpath('failure');
149 52
        $errors     = $node->xpath('error');
150
151 52
        while (list( , $fail) = each($failures)) {
152 46
            $case->addFailure((string)$fail['type'], (string)$fail);
153 46
        }
154
155 52
        while (list( , $err) = each($errors)) {
156 47
            $case->addError((string)$err['type'], (string)$err);
157 47
        }
158
159 52
        return $case;
160
    }
161
}
162