~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/phpunit/php-code-coverage/src/CodeCoverage/Report/XML.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * This file is part of the PHP_CodeCoverage package.
 
4
 *
 
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 
6
 *
 
7
 * For the full copyright and license information, please view the LICENSE
 
8
 * file that was distributed with this source code.
 
9
 */
 
10
 
 
11
/**
 
12
 * @category   PHP
 
13
 * @package    CodeCoverage
 
14
 * @author     Arne Blankerts <arne@blankerts.de>
 
15
 * @copyright  Sebastian Bergmann <sebastian@phpunit.de>
 
16
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
17
 * @link       http://github.com/sebastianbergmann/php-code-coverage
 
18
 * @since      Class available since Release 2.0.0
 
19
 */
 
20
class PHP_CodeCoverage_Report_XML
 
21
{
 
22
    /**
 
23
     * @var string
 
24
     */
 
25
    private $target;
 
26
 
 
27
    /**
 
28
     * @var PHP_CodeCoverage_Report_XML_Project
 
29
     */
 
30
    private $project;
 
31
 
 
32
    public function process(PHP_CodeCoverage $coverage, $target)
 
33
    {
 
34
        if (substr($target, -1, 1) != DIRECTORY_SEPARATOR) {
 
35
            $target .= DIRECTORY_SEPARATOR;
 
36
        }
 
37
 
 
38
        $this->target = $target;
 
39
        $this->initTargetDirectory($target);
 
40
 
 
41
        $report = $coverage->getReport();
 
42
 
 
43
        $this->project = new PHP_CodeCoverage_Report_XML_Project(
 
44
            $coverage->getReport()->getName()
 
45
        );
 
46
 
 
47
        $this->processTests($coverage->getTests());
 
48
        $this->processDirectory($report, $this->project);
 
49
 
 
50
        $index = $this->project->asDom();
 
51
        $index->formatOutput = true;
 
52
        $index->preserveWhiteSpace = false;
 
53
        $index->save($target . '/index.xml');
 
54
    }
 
55
 
 
56
    private function initTargetDirectory($dir)
 
57
    {
 
58
        if (file_exists($dir)) {
 
59
            if (!is_dir($dir)) {
 
60
                throw new PHP_CodeCoverage_Exception(
 
61
                    "'$dir' exists but is not a directory."
 
62
                );
 
63
            }
 
64
 
 
65
            if (!is_writable($dir)) {
 
66
                throw new PHP_CodeCoverage_Exception(
 
67
                    "'$dir' exists but is not writable."
 
68
                );
 
69
            }
 
70
        } elseif (!@mkdir($dir, 0777, true)) {
 
71
            throw new PHP_CodeCoverage_Exception(
 
72
                "'$dir' could not be created."
 
73
            );
 
74
        }
 
75
    }
 
76
 
 
77
    private function processDirectory(PHP_CodeCoverage_Report_Node_Directory $directory, PHP_CodeCoverage_Report_XML_Node $context)
 
78
    {
 
79
        $dirObject = $context->addDirectory($directory->getName());
 
80
 
 
81
        $this->setTotals($directory, $dirObject->getTotals());
 
82
 
 
83
        foreach ($directory as $node) {
 
84
            if ($node instanceof PHP_CodeCoverage_Report_Node_Directory) {
 
85
                $this->processDirectory($node, $dirObject);
 
86
                continue;
 
87
            }
 
88
 
 
89
            if ($node instanceof PHP_CodeCoverage_Report_Node_File) {
 
90
                $this->processFile($node, $dirObject);
 
91
                continue;
 
92
            }
 
93
 
 
94
            throw new PHP_CodeCoverage_Exception(
 
95
                'Unknown node type for XML report'
 
96
            );
 
97
        }
 
98
    }
 
99
 
 
100
    private function processFile(PHP_CodeCoverage_Report_Node_File $file, PHP_CodeCoverage_Report_XML_Directory $context)
 
101
    {
 
102
        $fileObject = $context->addFile(
 
103
            $file->getName(),
 
104
            $file->getId() . '.xml'
 
105
        );
 
106
 
 
107
        $this->setTotals($file, $fileObject->getTotals());
 
108
 
 
109
        $fileReport = new PHP_CodeCoverage_Report_XML_File_Report(
 
110
            $file->getName()
 
111
        );
 
112
 
 
113
        $this->setTotals($file, $fileReport->getTotals());
 
114
 
 
115
        foreach ($file->getClassesAndTraits() as $unit) {
 
116
            $this->processUnit($unit, $fileReport);
 
117
        }
 
118
 
 
119
        foreach ($file->getFunctions() as $function) {
 
120
            $this->processFunction($function, $fileReport);
 
121
        }
 
122
 
 
123
        foreach ($file->getCoverageData() as $line => $tests) {
 
124
            if (!is_array($tests) || count($tests) == 0) {
 
125
                continue;
 
126
            }
 
127
 
 
128
            $coverage = $fileReport->getLineCoverage($line);
 
129
 
 
130
            foreach ($tests as $test) {
 
131
                $coverage->addTest($test);
 
132
            }
 
133
 
 
134
            $coverage->finalize();
 
135
        }
 
136
 
 
137
        $this->initTargetDirectory(
 
138
            $this->target . dirname($file->getId()) . '/'
 
139
        );
 
140
 
 
141
        $fileDom = $fileReport->asDom();
 
142
        $fileDom->formatOutput = true;
 
143
        $fileDom->preserveWhiteSpace = false;
 
144
        $fileDom->save($this->target . $file->getId() . '.xml');
 
145
    }
 
146
 
 
147
    private function processUnit($unit, PHP_CodeCoverage_Report_XML_File_Report $report)
 
148
    {
 
149
        if (isset($unit['className'])) {
 
150
            $unitObject = $report->getClassObject($unit['className']);
 
151
        } else {
 
152
            $unitObject = $report->getTraitObject($unit['traitName']);
 
153
        }
 
154
 
 
155
        $unitObject->setLines(
 
156
            $unit['startLine'],
 
157
            $unit['executableLines'],
 
158
            $unit['executedLines']
 
159
        );
 
160
 
 
161
        $unitObject->setCrap($unit['crap']);
 
162
 
 
163
        $unitObject->setPackage(
 
164
            $unit['package']['fullPackage'],
 
165
            $unit['package']['package'],
 
166
            $unit['package']['subpackage'],
 
167
            $unit['package']['category']
 
168
        );
 
169
 
 
170
        $unitObject->setNamespace($unit['package']['namespace']);
 
171
 
 
172
        foreach ($unit['methods'] as $method) {
 
173
            $methodObject = $unitObject->addMethod($method['methodName']);
 
174
            $methodObject->setSignature($method['signature']);
 
175
            $methodObject->setLines($method['startLine'], $method['endLine']);
 
176
            $methodObject->setCrap($method['crap']);
 
177
            $methodObject->setTotals(
 
178
                $method['executableLines'],
 
179
                $method['executedLines'],
 
180
                $method['coverage']
 
181
            );
 
182
        }
 
183
    }
 
184
 
 
185
    private function processFunction($function, PHP_CodeCoverage_Report_XML_File_Report $report)
 
186
    {
 
187
        $functionObject = $report->getFunctionObject($function['functionName']);
 
188
 
 
189
        $functionObject->setSignature($function['signature']);
 
190
        $functionObject->setLines($function['startLine']);
 
191
        $functionObject->setCrap($function['crap']);
 
192
        $functionObject->setTotals($function['executableLines'], $function['executedLines'], $function['coverage']);
 
193
    }
 
194
 
 
195
    private function processTests(array $tests)
 
196
    {
 
197
        $testsObject = $this->project->getTests();
 
198
 
 
199
        foreach ($tests as $test => $result) {
 
200
            if ($test == 'UNCOVERED_FILES_FROM_WHITELIST') {
 
201
                continue;
 
202
            }
 
203
 
 
204
            $testsObject->addTest($test, $result);
 
205
        }
 
206
    }
 
207
 
 
208
    private function setTotals(PHP_CodeCoverage_Report_Node $node, PHP_CodeCoverage_Report_XML_Totals $totals)
 
209
    {
 
210
        $loc = $node->getLinesOfCode();
 
211
 
 
212
        $totals->setNumLines(
 
213
            $loc['loc'],
 
214
            $loc['cloc'],
 
215
            $loc['ncloc'],
 
216
            $node->getNumExecutableLines(),
 
217
            $node->getNumExecutedLines()
 
218
        );
 
219
 
 
220
        $totals->setNumClasses(
 
221
            $node->getNumClasses(),
 
222
            $node->getNumTestedClasses()
 
223
        );
 
224
 
 
225
        $totals->setNumTraits(
 
226
            $node->getNumTraits(),
 
227
            $node->getNumTestedTraits()
 
228
        );
 
229
 
 
230
        $totals->setNumMethods(
 
231
            $node->getNumMethods(),
 
232
            $node->getNumTestedMethods()
 
233
        );
 
234
 
 
235
        $totals->setNumFunctions(
 
236
            $node->getNumFunctions(),
 
237
            $node->getNumTestedFunctions()
 
238
        );
 
239
    }
 
240
}