AfterCreationTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 63
rs 10
c 2
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkAnswer() 0 4 2
A run() 0 4 2
A afterCreation() 0 3 1
1
<?php
2
3
/**
4
 * AfterCreation.php
5
 *
6
 * This file demonstrate is almost identical to SingleTestCase.php and
7
 * will demonstrates the usage of the afterCreation() function
8
 * to prepare data to be used inside the test.
9
 *
10
 * PHP version 7.4
11
 *
12
 * @category Examples
13
 * @package  RedboxTestSuite
14
 * @author   Johnny Mast <[email protected]>
15
 * @license  https://opensource.org/licenses/MIT MIT
16
 * @link     https://github.com/johnnymast/redbox-testsuite
17
 * @since    1.0
18
 */
19
20
require __DIR__ . '/../vendor/autoload.php';
21
22
use Redbox\Testsuite\Interfaces\ContainerInterface;
23
use Redbox\Testsuite\TestCase;
24
use Redbox\Testsuite\TestSuite;
25
26
/**
27
 * Class AfterCreationTest
28
 */
29
class AfterCreationTest extends TestCase
30
{
31
32
    /**
33
     * Tell the TestCase what the
34
     * min reachable score is.
35
     *
36
     * @var int
37
     */
38
    protected int $minscore = 0;
39
40
    /**
41
     * Tell the TestCase what the
42
     * max reachable score is.
43
     *
44
     * @var int
45
     */
46
    protected int $maxscore = 10;
47
48
    /**
49
     * This array will be filled with answers
50
     * inside afterCreation.
51
     *
52
     * @var array
53
     */
54
    protected array $answers = [];
55
56
    /**
57
     * This function is overwritten from Redbox\Testsuite\TestCase
58
     * and is being used to prepare information for the test.
59
     *
60
     * @return void
61
     */
62
    public function afterCreation(): void
63
    {
64
        $this->answers = [true, true, false];
65
    }
66
67
    /**
68
     * Demo function for answering demo questions.
69
     *
70
     * @param bool $correct For demo only if the answer is true mark correct.
71
     *
72
     * @return void
73
     */
74
    protected function checkAnswer(bool $correct): void
75
    {
76
        if ($correct) {
77
            $this->score->increment(1);
0 ignored issues
show
Bug introduced by
The method increment() 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

77
            $this->score->/** @scrutinizer ignore-call */ 
78
                          increment(1);

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...
78
        }
79
    }
80
81
    /**
82
     * Run the test.
83
     *
84
     * @param ContainerInterface $container The storage container for the TestSuite.
85
     *
86
     * @return void
87
     */
88
    public function run(ContainerInterface $container): void
89
    {
90
        foreach ($this->answers as $answer) {
91
            $this->checkAnswer($answer);
92
        }
93
    }
94
}
95
96
/**
97
 * Instantiate the test.
98
 */
99
$test = new AfterCreationTest();
100
101
/**
102
 * Create a test suite and attach the test.
103
 */
104
$suite = new TestSuite();
105
$suite->attach($test)
106
    ->run();
107
108
/**
109
 * Score should be
110
 *
111
 * Test score: 2
112
 *   - Answer 1 correct: true
113
 *   - Answer 2 correct: true
114
 *   - Answer 3 correct: false
115
 * ===================+
116
 * Total suite score 2
117
 */
118
119
echo "Total suite score: " . $suite->getScore() . "\n";
120
echo "Percentage complete: " . $test->score->percentage() . "%\n";
0 ignored issues
show
Bug introduced by
The method percentage() 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

120
echo "Percentage complete: " . $test->score->/** @scrutinizer ignore-call */ percentage() . "%\n";

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...
121