Completed
Pull Request — master (#204)
by
unknown
02:07
created

TestCase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
f 0
lcom 1
cbo 0
dl 0
loc 123
ccs 38
cts 38
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addFailure() 0 4 1
A addError() 0 4 1
A __construct() 0 15 1
A addDefect() 0 7 1
A caseFromNode() 0 20 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 51
    public function __construct(
60
        $name,
61
        $class,
62
        $file,
63
        $line,
64
        $assertions,
65
        $time
66
    ) {
67 51
        $this->name = $name;
68 51
        $this->class = $class;
69 51
        $this->file = $file;
70 51
        $this->line = $line;
71 51
        $this->assertions = $assertions;
72 51
        $this->time = $time;
73 51
    }
74
75
    /**
76
     * @param string $type
77
     * @param string $text
78
     */
79 45
    public function addFailure($type, $text)
80
    {
81 45
        $this->addDefect('failures', $type, $text);
82 45
    }
83
84
    /**
85
     * @param string $type
86
     * @param string $text
87
     */
88 46
    public function addError($type, $text)
89
    {
90 46
        $this->addDefect('errors', $type, $text);
91 46
    }
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 46
    protected function addDefect($collName, $type, $text)
101
    {
102 46
        $this->{$collName}[] = array(
103 46
            'type' => $type,
104 46
            'text' => trim($text)
105 46
        );
106 46
    }
107
108
    /**
109
     * Factory method that creates a TestCase object
110
     * from a SimpleXMLElement
111
     *
112
     * @param \SimpleXMLElement $node
113
     * @return TestCase
114
     */
115 51
    public static function caseFromNode(\SimpleXMLElement $node)
116
    {
117 51
        $case = new TestCase(
118 51
            (string) $node['name'],
119 51
            (string) $node['class'],
120 51
            (string) $node['file'],
121 51
            (string) $node['line'],
122 51
            (string) $node['assertions'],
123 51
            (string) $node['time']
124 51
        );
125 51
        $failures = $node->xpath('failure');
126 51
        $errors = $node->xpath('error');
127 51
        while (list( , $fail) = each($failures)) {
128 45
            $case->addFailure((string)$fail['type'], (string)$fail);
129 45
        }
130 51
        while (list( , $err) = each($errors)) {
131 46
            $case->addError((string)$err['type'], (string)$err);
132 46
        }
133 51
        return $case;
134
    }
135
}
136