Passed
Push — master ( 8ba091...d47090 )
by Johnny
06:24
created

TestSuite::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
/**
4
 * TestSuite.php
5
 *
6
 * This middleware will intercept traffic from visitors to
7
 * your website.
8
 *
9
 * PHP version 7.2
10
 *
11
 * @category Core
12
 * @package  RedboxTestSuite
13
 * @author   Johnny Mast <[email protected]>
14
 * @license  https://opensource.org/licenses/MIT MIT
15
 * @link     https://github.com/johnnymast/redbox-testsuite
16
 * @since    GIT:1.0
17
 */
18
19
namespace Redbox\Testsuite;
20
21
class TestSuite
22
{
23
    /**
24
     * Storage to contain the
25
     * tests.
26
     *
27
     * @var \SplObjectStorage
28
     */
29
    protected ?\SplObjectStorage $tests = null;
30
    
31
    /**
32
     * Test Score counter.
33
     *
34
     * @var int|double
35
     */
36
    protected $score = 0;
37
    
38
    /**
39
     * TestSuite constructor.
40
     */
41 18
    public function __construct()
42
    {
43 18
        $this->tests = new \SplObjectStorage;
44 18
    }
45
    
46
    /**
47
     * Reset the score and rewind
48
     * the tests storage.
49
     *
50
     * @return void
51
     */
52 10
    public function reset()
53
    {
54 10
        $this->score = 0;
55 10
    }
56
    
57
    /**
58
     * Attach a Test or an array of
59
     * Tests to the TestSuite.
60
     *
61
     * @param $info The Test to attach.
0 ignored issues
show
Bug introduced by
The type Redbox\Testsuite\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62
     *
63
     * @return void
64
     */
65 18
    public function attach($info)
66
    {
67 18
        if (is_array($info) === true) {
68 8
            foreach ($info as $test) {
69 8
                $this->attach($test);
70
            }
71
        } else {
72 18
            $test = $info;
73
            
74
            // TODO: Allow array of class names and instantiate here
75
            /*
76
            if (is_string($info) === true && class_exists($info) === true) {
77
                $test = new $info();
78
            }
79
            */
80
            
81 18
            if (is_subclass_of($test, Test::class) === false) {
82 2
                throw new \InvalidArgumentException('Test does not extend Test abstract class.');
83
            }
84
            
85 16
            $this->tests->attach($test);
0 ignored issues
show
Bug introduced by
The method attach() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
            $this->tests->/** @scrutinizer ignore-call */ 
86
                          attach($test);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        }
87 16
    }
88
    
89
    /**
90
     * Detach a given test from the
91
     * TestSuite.
92
     *
93
     * @param  Test  $test  The test to detach.
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 1 spaces but found 2
Loading history...
94
     *
95
     * @return void
96
     */
97 2
    public function detach(Test $test)
98
    {
99 2
        $this->tests->detach($test);
100 2
    }
101
    
102
    /**
103
     * Check to see if the TestSuite has a given
104
     * test inside.
105
     *
106
     * @param  Test  $test  The Test to check for.
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 1 spaces but found 2
Loading history...
107
     *
108
     * @return bool
109
     */
110 6
    public function has(Test $test)
111
    {
112 6
        return $this->tests->contains($test);
113
    }
114
    
115
    /**
116
     * Run the tests
117
     *
118
     * @return int The number of tests that ran.
119
     */
120 10
    public function run(): int
121
    {
122 10
        $tests_run = 0;
123
        
124
        /**
125
         * Reset the test results
126
         */
127 10
        $this->reset();
128
        
129 10
        foreach ($this->tests as $test) {
130 10
            $test->run();
131 10
            $this->score += $test->score->getScore();
132 10
            $tests_run++;
133
        }
134
        
135 10
        return $tests_run;
136
    }
137
    
138
    /**
139
     * Return the test suite score.
140
     *
141
     * @return mixed
142
     */
143 6
    public function getScore()
144
    {
145 6
        return $this->score;
146
    }
147
}
148
149
150