Issues (46)

examples/MultipleTests.php (3 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * MultipleTests.php
5
 *
6
 * This file demonstrate how to add multiple tests to the test suite
7
 * and run the tests.
8
 *
9
 * PHP version 7.4
10
 *
11
 * @category Examples
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    1.0
17
 */
18
19
require __DIR__ . '/../vendor/autoload.php';
20
21
use Redbox\Testsuite\Interfaces\ContainerInterface;
22
use Redbox\Testsuite\TestCase;
23
use Redbox\Testsuite\TestSuite;
24
25
/**
26
 * Class TheFirstTestCase
27
 */
28
class TheFirstTestCase extends TestCase
29
{
30
31
    /**
32
     * Tell the TestCase what the
33
     * min reachable score is.
34
     *
35
     * @var int
36
     */
37
    protected int $minscore = 0;
38
39
    /**
40
     * Tell the TestCase what the
41
     * max reachable score is.
42
     *
43
     * @var int
44
     */
45
    protected int $maxscore = 4;
46
47
    /**
48
     * Demo function for answering demo questions.
49
     *
50
     * @param bool $correct For demo only if the answer is true mark correct.
51
     *
52
     * @return void
53
     */
54
    protected function checkAnswer(bool $correct): void
55
    {
56
        if ($correct) {
57
            $this->score->increment(1);
0 ignored issues
show
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

57
            $this->score->/** @scrutinizer ignore-call */ 
58
                          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...
58
        }
59
    }
60
61
    /**
62
     * Run the test.
63
     *
64
     * @param ContainerInterface $container The storage container for the TestSuite.
65
     *
66
     * @return void
67
     */
68
    public function run(ContainerInterface $container)
69
    {
70
        $this->checkAnswer(true);
71
        $this->checkAnswer(true);
72
        $this->checkAnswer(false);
73
    }
74
}
75
76
class TheSecondTestCase extends TestCase
77
{
78
79
    /**
80
     * Tell the TestCase what the
81
     * min reachable score is.
82
     *
83
     * @var int
84
     */
85
    protected int $minscore = 0;
86
87
    /**
88
     * Tell the TestCase what the
89
     * max reachable score is.
90
     *
91
     * @var int
92
     */
93
    protected int $maxscore = 3;
94
95
    /**
96
     * Run the test.
97
     *
98
     * @param ContainerInterface $container The storage container for the TestSuite.
99
     *
100
     * @return void
101
     */
102
    public function run(ContainerInterface $container)
103
    {
104
        $this->score->increment(1);
105
        $this->score->increment(1);
106
        $this->score->increment(1);
107
    }
108
}
109
110
/**
111
 * Instantiate the test.
112
 */
113
$test1 = new TheFirstTestCase();
114
$test2 = new TheSecondTestCase();
115
116
/**
117
 * Create a test suite
118
 */
119
$suite = new TestSuite();
120
$suite->attach([$test1, $test2])
121
    ->run();
122
123
/**
124
 * Score should be
125
 *
126
 * Test score: 2
127
 *   - Answer 1 correct: true
128
 *   - Answer 2 correct: true
129
 *   - Answer 3 correct: false
130
 * ===================+
131
 * Total suite score 2
132
 */
133
134
echo "Total suite score: " . $suite->getScore() . "\n";
135
echo "Percentage complete Test 1: " . $test1->score->percentage() . "\n";
0 ignored issues
show
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

135
echo "Percentage complete Test 1: " . $test1->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...
136
echo "Percentage complete Test 2: " . $test2->score->percentage() . "\n";
0 ignored issues
show
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

136
echo "Percentage complete Test 2: " . $test2->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...
137
138
/**
139
 * Output:
140
 *
141
 * Total suite score: 5
142
 * Percentage complete Test 1: 50
143
 * Percentage complete Test 2: 100
144
 */
145