1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* SingleTestCase.php |
5
|
|
|
* |
6
|
|
|
* This file demonstrate how to add a test to the test suite |
7
|
|
|
* and run the test. |
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 SingleTestCase |
27
|
|
|
*/ |
28
|
|
|
class SingleTestCase 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); |
|
|
|
|
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
|
|
|
/** |
77
|
|
|
* Instantiate the test. |
78
|
|
|
*/ |
79
|
|
|
$test = new SingleTestCase(); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a test suite and attach the test. |
83
|
|
|
*/ |
84
|
|
|
$suite = new TestSuite(); |
85
|
|
|
$suite->attach($test) |
86
|
|
|
->run(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Score should be |
90
|
|
|
* |
91
|
|
|
* Test score: 2 |
92
|
|
|
* - Answer 1 correct: true |
93
|
|
|
* - Answer 2 correct: true |
94
|
|
|
* - Answer 3 correct: false |
95
|
|
|
* ===================+ |
96
|
|
|
* Total suite score 2 |
97
|
|
|
*/ |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
echo "Total suite score: " . $suite->getScore() . "\n"; |
101
|
|
|
echo "Percentage complete: " . $test->score->percentage() . "%\n"; |
|
|
|
|
102
|
|
|
|
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.