|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Results.php |
|
5
|
|
|
* |
|
6
|
|
|
* This file demonstrate how the getAnswers function can help you |
|
7
|
|
|
* interpret the answers given bij 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
|
|
|
$questions = [ |
|
26
|
|
|
[ |
|
27
|
|
|
'text' => '5+5 =', |
|
28
|
|
|
'answer' => 10, |
|
29
|
|
|
'score_good' => 1, |
|
30
|
|
|
'score_wrong' => 0, |
|
31
|
|
|
'answer_wrong' => 'You are Wrong 5+5=10', |
|
32
|
|
|
'answer_correct' => 'You are right 5+5=10', |
|
33
|
|
|
], |
|
34
|
|
|
[ |
|
35
|
|
|
'text' => 'Is this script running on php?', |
|
36
|
|
|
'answer' => 'yes', |
|
37
|
|
|
'score_good' => 1, |
|
38
|
|
|
'score_wrong' => 0, |
|
39
|
|
|
'answer_wrong' => 'You are wrong this script is running on php.', |
|
40
|
|
|
'answer_correct' => 'You are right this script is created in php.', |
|
41
|
|
|
], |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create a table for cli output. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $answers The answers given in the test. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
function formatOutput(array $answers): string |
|
52
|
|
|
{ |
|
53
|
|
|
$table_header = []; |
|
54
|
|
|
$table_rows = []; |
|
55
|
|
|
$longestLine = 0; |
|
56
|
|
|
|
|
57
|
|
|
foreach ($answers as $answer) { |
|
58
|
|
|
$row = sprintf( |
|
59
|
|
|
'| %s | %s | %s |', |
|
60
|
|
|
str_pad($answer['increment'], 10, " "), |
|
61
|
|
|
str_pad($answer['answer'], 10, " "), |
|
62
|
|
|
str_pad($answer['motivation'], 50, " "), |
|
63
|
|
|
str_pad($answer['score'], 5, " "), |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
if (strlen($row) > $longestLine) { |
|
67
|
|
|
$longestLine = strlen($row); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$table_rows[] = $row; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$table_header [] = str_repeat('_', $longestLine); |
|
74
|
|
|
$table_header[] = sprintf( |
|
75
|
|
|
'| %s | %s | %s |', |
|
76
|
|
|
str_pad('Increment', 10, " "), |
|
77
|
|
|
str_pad('Answer', 10, " "), |
|
78
|
|
|
str_pad('Motivation', 50, " "), |
|
79
|
|
|
str_pad('Score', 5, " ") |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$table_header [] = str_repeat('-', $longestLine); |
|
83
|
|
|
$table_footer = [str_repeat('-', $longestLine), "\n"]; |
|
84
|
|
|
|
|
85
|
|
|
$table = [ |
|
86
|
|
|
...$table_header, |
|
87
|
|
|
...$table_rows, |
|
88
|
|
|
...$table_footer, |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
return implode("\n", $table); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Class RealTest |
|
96
|
|
|
*/ |
|
97
|
|
|
class RealTest extends TestCase |
|
98
|
|
|
{ |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Tell the TestCase what the |
|
102
|
|
|
* min reachable score is. |
|
103
|
|
|
* |
|
104
|
|
|
* @var int |
|
105
|
|
|
*/ |
|
106
|
|
|
protected int $minscore = 0; |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Tell the TestCase what the |
|
110
|
|
|
* max reachable score is. |
|
111
|
|
|
* |
|
112
|
|
|
* @var int |
|
113
|
|
|
*/ |
|
114
|
|
|
protected int $maxscore = 2; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Read commandline input. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $prompt The prompt to display to the user. |
|
120
|
|
|
* @param string $answer The suggested answer. |
|
121
|
|
|
* |
|
122
|
|
|
* @return false|string |
|
123
|
|
|
*/ |
|
124
|
|
|
private function readline($prompt = '', $answer = ''): string |
|
125
|
|
|
{ |
|
126
|
|
|
return readline($prompt.' ('.$answer.'): '); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* As the user the question and process the result. |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $question Array containing infromation about this question. |
|
133
|
|
|
* |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
|
|
private function askQuestion($question): void |
|
137
|
|
|
{ |
|
138
|
|
|
$text = $question['text']; |
|
139
|
|
|
$answer = $question['answer']; |
|
140
|
|
|
|
|
141
|
|
|
$input = $this->readline($text, $answer); |
|
142
|
|
|
|
|
143
|
|
|
if ($input == $answer) { |
|
144
|
|
|
$this->score->increment($question['score_good'], $question['answer_correct'], $input); |
|
|
|
|
|
|
145
|
|
|
} else { |
|
146
|
|
|
$this->score->increment($question['score_wrong'], $question['answer_wrong'], $input); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Entry point for the test to start running. |
|
152
|
|
|
* |
|
153
|
|
|
* @param ContainerInterface $container The storage container holding the questions. |
|
154
|
|
|
* |
|
155
|
|
|
* @return void |
|
156
|
|
|
*/ |
|
157
|
|
|
public function run(ContainerInterface $container) |
|
158
|
|
|
{ |
|
159
|
|
|
$questions = $container->get('questions'); |
|
160
|
|
|
foreach ($questions as $question) { |
|
161
|
|
|
$this->askQuestion($question); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$test = new RealTest(); |
|
167
|
|
|
$suite = new TestSuite(); |
|
168
|
|
|
$suite->getContainer()->set('questions', $questions); |
|
169
|
|
|
$suite->attach($test) |
|
170
|
|
|
->run(); |
|
171
|
|
|
|
|
172
|
|
|
$answers = current($suite->getAnswers()); |
|
173
|
|
|
|
|
174
|
|
|
echo formatOutput($answers); |
|
175
|
|
|
|
|
176
|
|
|
echo "Total suite score: ".$suite->getScore()."\n"; |
|
177
|
|
|
echo "Percentage complete: ".$test->score->percentage()."%\n"; |
|
|
|
|
|
|
178
|
|
|
|
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.