Passed
Push — master ( 045f45...2c73e4 )
by Michiel
08:31 queued 11s
created

BatchTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 130
ccs 28
cts 34
cp 0.8235
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A filterTests() 0 5 2
A setExclude() 0 3 1
A elements() 0 19 3
A setName() 0 3 1
A __construct() 0 3 1
A isTestCase() 0 4 2
A getFilenames() 0 16 3
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
/**
21
 * Scans a list of files given by the fileset attribute, extracts valid test cases
22
 *
23
 * @author Michiel Rook <[email protected]>
24
 *
25
 * @package phing.tasks.ext.phpunit
26
 *
27
 * @since 2.1.0
28
 */
29
class BatchTest
30
{
31
    use ClasspathAware;
32
    use FileSetAware;
33
34
    /**
35
     * the reference to the project
36
     */
37
    private $project = null;
38
39
    /**
40
     * names of classes to exclude
41
     */
42
    private $excludeClasses = [];
43
44
    /**
45
     * name of the batchtest/suite
46
     */
47
    protected $name = "Phing Batchtest";
48
49
    /**
50
     * Create a new batchtest instance
51
     *
52
     * @param Project $project the project it depends on.
53
     */
54 4
    public function __construct(Project $project)
55
    {
56 4
        $this->project = $project;
57 4
    }
58
59
    /**
60
     * Sets the name of the batchtest/suite
61
     *
62
     * @param $name
63
     */
64
    public function setName($name)
65
    {
66
        $this->name = $name;
67
    }
68
69
    /**
70
     * Sets the classes to exclude.
71
     *
72
     * @param string $exclude
73
     *
74
     * @return void
75
     */
76
    public function setExclude($exclude)
77
    {
78
        $this->excludeClasses = explode(" ", $exclude);
79
    }
80
81
    /**
82
     * Iterate over all filesets and return the filename of all files.
83
     *
84
     * @return array an array of filenames
85
     */
86 4
    private function getFilenames()
87
    {
88 4
        $filenames = [];
89
90 4
        foreach ($this->filesets as $fileset) {
91 4
            $ds = $fileset->getDirectoryScanner($this->project);
92 4
            $ds->scan();
93
94 4
            $files = $ds->getIncludedFiles();
95
96 4
            foreach ($files as $file) {
97 4
                $filenames[] = $ds->getBaseDir() . "/" . $file;
98
            }
99
        }
100
101 4
        return $filenames;
102
    }
103
104
    /**
105
     * Checks wheter $input is a PHPUnit Test.
106
     *
107
     * @param $input
108
     *
109
     * @return bool
110
     */
111 4
    private function isTestCase($input)
112
    {
113 4
        return is_subclass_of($input, '\PHPUnit\Framework\TestCase') ||
114 4
            is_subclass_of($input, '\PHPUnit\Framework\TestSuite');
115
    }
116
117
    /**
118
     * Filters an array of classes, removes all classes that are not test cases or test suites,
119
     * or classes that are declared abstract.
120
     *
121
     * @param object $input
122
     *
123
     * @return bool
124
     * @throws ReflectionException
125
     */
126 4
    private function filterTests($input)
127
    {
128 4
        $reflect = new ReflectionClass($input);
129
130 4
        return $this->isTestCase($input) && (!$reflect->isAbstract());
131
    }
132
133
    /**
134
     * Returns an array of test cases and test suites that are declared
135
     * by the files included by the filesets
136
     *
137
     * @return array an array of tests.
138
     * @throws Exception
139
     */
140 4
    public function elements()
141
    {
142 4
        $filenames = $this->getFilenames();
143
144 4
        $declaredClasses = [];
145
146 4
        foreach ($filenames as $filename) {
147 4
            $definedClasses = PHPUnitUtil::getDefinedClasses($filename, $this->classpath);
148
149 4
            foreach ($definedClasses as $definedClass) {
150 4
                $this->project->log("(PHPUnit) Adding $definedClass (from $filename) to tests.", Project::MSG_DEBUG);
151
            }
152
153 4
            $declaredClasses = array_merge($declaredClasses, $definedClasses);
154
        }
155
156 4
        $elements = array_filter($declaredClasses, [$this, "filterTests"]);
157
158 4
        return $elements;
159
    }
160
}
161