Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b3736bb

Browse files
committedMay 17, 2024
Reorganise event hiearchy
1 parent 9cdd0ec commit b3736bb

13 files changed

+248
-102
lines changed
 

‎src/Event/CgiExecuteEvent.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,32 @@
44

55
namespace PhpSchool\PhpWorkshop\Event;
66

7+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
8+
use PhpSchool\PhpWorkshop\Input\Input;
79
use Psr\Http\Message\RequestInterface;
810

911
/**
1012
* An event to represent events which occur throughout the verification and running process in
1113
* `\PhpSchool\PhpWorkshop\ExerciseRunner\CgiRunner`.
1214
*/
13-
class CgiExecuteEvent extends Event
15+
class CgiExecuteEvent extends CgiExerciseRunnerEvent
1416
{
15-
/**
16-
* @var RequestInterface
17-
*/
18-
private $request;
17+
private RequestInterface $request;
1918

2019
/**
2120
* @param string $name The event name.
2221
* @param RequestInterface $request The request that will be performed.
2322
* @param array<mixed> $parameters The event parameters.
2423
*/
25-
public function __construct(string $name, RequestInterface $request, array $parameters = [])
26-
{
24+
public function __construct(
25+
string $name,
26+
ExerciseInterface $exercise,
27+
Input $input,
28+
RequestInterface $request,
29+
array $parameters = []
30+
) {
2731
$parameters['request'] = $request;
28-
parent::__construct($name, $parameters);
32+
parent::__construct($name, $exercise, $input, $parameters);
2933
$this->request = $request;
3034
}
3135

‎src/Event/CgiExerciseRunnerEvent.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Event;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
6+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
7+
use PhpSchool\PhpWorkshop\Input\Input;
8+
9+
class CgiExerciseRunnerEvent extends ExerciseRunnerEvent
10+
{
11+
}

‎src/Event/CliExecuteEvent.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,35 @@
44

55
namespace PhpSchool\PhpWorkshop\Event;
66

7+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
8+
use PhpSchool\PhpWorkshop\Input\Input;
79
use PhpSchool\PhpWorkshop\Utils\ArrayObject;
810

911
/**
1012
* An event to represent events which occur throughout the verification and running process in
1113
* `\PhpSchool\PhpWorkshop\ExerciseRunner\CliRunner`.
1214
*/
13-
class CliExecuteEvent extends Event
15+
class CliExecuteEvent extends CliExerciseRunnerEvent
1416
{
1517
/**
1618
* @var ArrayObject<int, string>
1719
*/
18-
private $args;
20+
private ArrayObject $args;
1921

2022
/**
2123
* @param string $name The event name.
2224
* @param ArrayObject<int, string> $args The arguments that should be/have been passed to the program.
2325
* @param array<mixed> $parameters The event parameters.
2426
*/
25-
public function __construct(string $name, ArrayObject $args, array $parameters = [])
26-
{
27+
public function __construct(
28+
string $name,
29+
ExerciseInterface $exercise,
30+
Input $input,
31+
ArrayObject $args,
32+
array $parameters = []
33+
) {
2734
$parameters['args'] = $args;
28-
parent::__construct($name, $parameters);
35+
parent::__construct($name, $exercise, $input, $parameters);
2936
$this->args = $args;
3037
}
3138

‎src/Event/CliExerciseRunnerEvent.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Event;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
6+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
7+
use PhpSchool\PhpWorkshop\Input\Input;
8+
9+
class CliExerciseRunnerEvent extends ExerciseRunnerEvent
10+
{
11+
}

‎src/Event/Event.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@
1111
*/
1212
class Event implements EventInterface
1313
{
14-
/**
15-
* @var string
16-
*/
17-
private $name;
14+
private string $name;
1815

1916
/**
2017
* @var array<mixed>
2118
*/
22-
protected $parameters;
19+
protected array $parameters;
2320

2421
/**
2522
* @param string $name The event name.
@@ -52,13 +49,13 @@ public function getParameters(): array
5249
}
5350

5451
/**
55-
* Get a parameter by it's name.
52+
* Get a parameter by its name.
5653
*
5754
* @param string $name The name of the parameter.
5855
* @return mixed The value.
5956
* @throws InvalidArgumentException If the parameter by name does not exist.
6057
*/
61-
public function getParameter(string $name)
58+
public function getParameter(string $name): mixed
6259
{
6360
if (!array_key_exists($name, $this->parameters)) {
6461
throw new InvalidArgumentException(sprintf('Parameter: "%s" does not exist', $name));

‎src/Event/EventDispatcher.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,20 @@ class EventDispatcher
1616
/**
1717
* @var array<string, array<callable>>
1818
*/
19-
private $listeners = [];
19+
private array $listeners = [];
2020

2121
/**
2222
* @var ResultAggregator
2323
*/
24-
private $resultAggregator;
24+
private ResultAggregator $resultAggregator;
2525

26-
/**
27-
* @param ResultAggregator $resultAggregator
28-
*/
2926
public function __construct(ResultAggregator $resultAggregator)
3027
{
3128
$this->resultAggregator = $resultAggregator;
3229
}
3330

3431
/**
3532
* Dispatch an event. Can be any event object which implements `PhpSchool\PhpWorkshop\Event\EventInterface`.
36-
*
37-
* @param EventInterface $event
38-
* @return EventInterface
3933
*/
4034
public function dispatch(EventInterface $event): EventInterface
4135
{
@@ -103,9 +97,6 @@ public function removeListener(string $eventName, callable $callback): void
10397
* Insert a verifier callback which will execute at the given event name much like normal listeners.
10498
* A verifier should return an object which implements `PhpSchool\PhpWorkshop\Result\FailureInterface`
10599
* or `PhpSchool\PhpWorkshop\Result\SuccessInterface`. This result object will be added to the result aggregator.
106-
*
107-
* @param string $eventName
108-
* @param callable $verifier
109100
*/
110101
public function insertVerifier(string $eventName, callable $verifier): void
111102
{

‎src/Event/EventInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public function getName(): string;
2626
public function getParameters(): array;
2727

2828
/**
29-
* Get a parameter by it's name.
29+
* Get a parameter by its name.
3030
*
3131
* @param string $name The name of the parameter.
3232
* @return mixed The value.
3333
* @throws InvalidArgumentException If the parameter by name does not exist.
3434
*/
35-
public function getParameter(string $name);
35+
public function getParameter(string $name): mixed;
3636
}

‎src/ExerciseRunner/CgiRunner.php

Lines changed: 81 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
1111
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
1212
use PhpSchool\PhpWorkshop\Event\CgiExecuteEvent;
13+
use PhpSchool\PhpWorkshop\Event\CgiExerciseRunnerEvent;
1314
use PhpSchool\PhpWorkshop\Event\Event;
1415
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
1516
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
@@ -85,33 +86,84 @@ public function getRequiredChecks(): array
8586
}
8687

8788
/**
88-
* @param RequestInterface $request
89-
* @param string $fileName
90-
* @return CgiResultInterface
89+
* Verifies a solution by invoking PHP via the `php-cgi` binary, populating all the super globals with
90+
* the information from the request objects returned from the exercise. The exercise can return multiple
91+
* requests so the solution will be invoked for however many requests there are.
92+
*
93+
* Events dispatched (for each request):
94+
*
95+
* * cgi.verify.reference-execute.pre
96+
* * cgi.verify.reference.executing
97+
* * cgi.verify.reference-execute.fail (if the reference solution fails to execute)
98+
* * cgi.verify.student-execute.pre
99+
* * cgi.verify.student.executing
100+
* * cgi.verify.student-execute.fail (if the student's solution fails to execute)
101+
*
102+
* @param Input $input The command line arguments passed to the command.
103+
* @return CgiResult The result of the check.
91104
*/
92-
private function checkRequest(RequestInterface $request, string $fileName): CgiResultInterface
105+
public function verify(Input $input): ResultInterface
106+
{
107+
$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.verify.start', $this->exercise, $input));
108+
$result = new CgiResult(
109+
array_map(
110+
function (RequestInterface $request) use ($input) {
111+
return $this->doVerify($request, $input);
112+
},
113+
$this->exercise->getRequests()
114+
)
115+
);
116+
$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.verify.finish', $this->exercise, $input));
117+
return $result;
118+
}
119+
120+
private function doVerify(RequestInterface $request, Input $input): CgiResultInterface
93121
{
94122
try {
95123
/** @var CgiExecuteEvent $event */
96124
$event = $this->eventDispatcher->dispatch(
97-
new CgiExecuteEvent('cgi.verify.reference-execute.pre', $request)
125+
new CgiExecuteEvent('cgi.verify.reference-execute.pre', $this->exercise, $input, $request)
98126
);
99127
$solutionResponse = $this->executePhpFile(
128+
$input,
100129
$this->exercise->getSolution()->getEntryPoint()->getAbsolutePath(),
101130
$event->getRequest(),
102131
'reference'
103132
);
104133
} catch (CodeExecutionException $e) {
105-
$this->eventDispatcher->dispatch(new Event('cgi.verify.reference-execute.fail', ['exception' => $e]));
134+
$this->eventDispatcher->dispatch(
135+
new CgiExecuteEvent(
136+
'cgi.verify.reference-execute.fail',
137+
$this->exercise,
138+
$input,
139+
$request,
140+
['exception' => $e]
141+
)
142+
);
106143
throw new SolutionExecutionException($e->getMessage());
107144
}
108145

109146
try {
110147
/** @var CgiExecuteEvent $event */
111-
$event = $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.verify.student-execute.pre', $request));
112-
$userResponse = $this->executePhpFile($fileName, $event->getRequest(), 'student');
148+
$event = $this->eventDispatcher->dispatch(
149+
new CgiExecuteEvent('cgi.verify.student-execute.pre', $this->exercise, $input, $request)
150+
);
151+
$userResponse = $this->executePhpFile(
152+
$input,
153+
$input->getRequiredArgument('program'),
154+
$event->getRequest(),
155+
'student'
156+
);
113157
} catch (CodeExecutionException $e) {
114-
$this->eventDispatcher->dispatch(new Event('cgi.verify.student-execute.fail', ['exception' => $e]));
158+
$this->eventDispatcher->dispatch(
159+
new CgiExecuteEvent(
160+
'cgi.verify.student-execute.fail',
161+
$this->exercise,
162+
$input,
163+
$request,
164+
['exception' => $e]
165+
)
166+
);
115167
return GenericFailure::fromRequestAndCodeExecutionFailure($request, $e);
116168
}
117169

@@ -146,12 +198,18 @@ private function getHeaders(ResponseInterface $response): array
146198
* @param string $type
147199
* @return ResponseInterface
148200
*/
149-
private function executePhpFile(string $fileName, RequestInterface $request, string $type): ResponseInterface
150-
{
201+
private function executePhpFile(
202+
Input $input,
203+
string $fileName,
204+
RequestInterface $request,
205+
string $type
206+
): ResponseInterface {
151207
$process = $this->getPhpProcess(dirname($fileName), basename($fileName), $request);
152208

153209
$process->start();
154-
$this->eventDispatcher->dispatch(new CgiExecuteEvent(sprintf('cgi.verify.%s.executing', $type), $request));
210+
$this->eventDispatcher->dispatch(
211+
new CgiExecuteEvent(sprintf('cgi.verify.%s.executing', $type), $this->exercise, $input, $request)
212+
);
155213
$process->wait();
156214

157215
if (!$process->isSuccessful()) {
@@ -206,38 +264,6 @@ private function getPhpProcess(string $workingDirectory, string $fileName, Reque
206264
return $this->processFactory->create($processInput);
207265
}
208266

209-
/**
210-
* Verifies a solution by invoking PHP via the `php-cgi` binary, populating all the super globals with
211-
* the information from the request objects returned from the exercise. The exercise can return multiple
212-
* requests so the solution will be invoked for however many requests there are.
213-
*
214-
* Events dispatched (for each request):
215-
*
216-
* * cgi.verify.reference-execute.pre
217-
* * cgi.verify.reference.executing
218-
* * cgi.verify.reference-execute.fail (if the reference solution fails to execute)
219-
* * cgi.verify.student-execute.pre
220-
* * cgi.verify.student.executing
221-
* * cgi.verify.student-execute.fail (if the student's solution fails to execute)
222-
*
223-
* @param Input $input The command line arguments passed to the command.
224-
* @return CgiResult The result of the check.
225-
*/
226-
public function verify(Input $input): ResultInterface
227-
{
228-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.verify.start', $this->exercise, $input));
229-
$result = new CgiResult(
230-
array_map(
231-
function (RequestInterface $request) use ($input) {
232-
return $this->checkRequest($request, $input->getRequiredArgument('program'));
233-
},
234-
$this->exercise->getRequests()
235-
)
236-
);
237-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.verify.finish', $this->exercise, $input));
238-
return $result;
239-
}
240-
241267
/**
242268
* Runs a student's solution by invoking PHP via the `php-cgi` binary, populating all the super globals with
243269
* the information from the request objects returned from the exercise. The exercise can return multiple
@@ -257,12 +283,12 @@ function (RequestInterface $request) use ($input) {
257283
*/
258284
public function run(Input $input, OutputInterface $output): bool
259285
{
260-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.start', $this->exercise, $input));
286+
$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.run.start', $this->exercise, $input));
261287
$success = true;
262288
foreach ($this->exercise->getRequests() as $i => $request) {
263289
/** @var CgiExecuteEvent $event */
264290
$event = $this->eventDispatcher->dispatch(
265-
new CgiExecuteEvent('cgi.run.student-execute.pre', $request)
291+
new CgiExecuteEvent('cgi.run.student-execute.pre', $this->exercise, $input, $request)
266292
);
267293
$process = $this->getPhpProcess(
268294
dirname($input->getRequiredArgument('program')),
@@ -272,7 +298,13 @@ public function run(Input $input, OutputInterface $output): bool
272298

273299
$process->start();
274300
$this->eventDispatcher->dispatch(
275-
new CgiExecuteEvent('cgi.run.student.executing', $request, ['output' => $output])
301+
new CgiExecuteEvent(
302+
'cgi.run.student.executing',
303+
$this->exercise,
304+
$input,
305+
$request,
306+
['output' => $output]
307+
)
276308
);
277309
$process->wait(function ($outputType, $outputBuffer) use ($output) {
278310
$output->write($outputBuffer);
@@ -286,10 +318,10 @@ public function run(Input $input, OutputInterface $output): bool
286318
$output->lineBreak();
287319

288320
$this->eventDispatcher->dispatch(
289-
new CgiExecuteEvent('cgi.run.student-execute.post', $request)
321+
new CgiExecuteEvent('cgi.run.student-execute.post', $this->exercise, $input, $request)
290322
);
291323
}
292-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.finish', $this->exercise, $input));
324+
$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.run.finish', $this->exercise, $input));
293325
return $success;
294326
}
295327
}

‎src/ExerciseRunner/CliRunner.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
1010
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
1111
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
12+
use PhpSchool\PhpWorkshop\Event\CliExerciseRunnerEvent;
1213
use PhpSchool\PhpWorkshop\Event\Event;
1314
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
1415
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
@@ -100,7 +101,7 @@ public function getRequiredChecks(): array
100101
*/
101102
public function verify(Input $input): ResultInterface
102103
{
103-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.verify.start', $this->exercise, $input));
104+
$this->eventDispatcher->dispatch(new CliExerciseRunnerEvent('cli.verify.start', $this->exercise, $input));
104105
$result = new CliResult(
105106
array_map(
106107
function (array $args) use ($input) {
@@ -109,7 +110,7 @@ function (array $args) use ($input) {
109110
$this->preserveOldArgFormat($this->exercise->getArgs())
110111
)
111112
);
112-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.verify.finish', $this->exercise, $input));
113+
$this->eventDispatcher->dispatch(new CliExerciseRunnerEvent('cli.verify.finish', $this->exercise, $input));
113114
return $result;
114115
}
115116

@@ -142,23 +143,49 @@ private function doVerify(array $args, Input $input): CliResultInterface
142143

143144
try {
144145
/** @var CliExecuteEvent $event */
145-
$event = $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.verify.reference-execute.pre', $args));
146+
$event = $this->eventDispatcher->dispatch(
147+
new CliExecuteEvent('cli.verify.reference-execute.pre', $this->exercise, $input, $args)
148+
);
146149
$solutionOutput = $this->executePhpFile(
150+
$input,
147151
$this->exercise->getSolution()->getEntryPoint()->getAbsolutePath(),
148152
$event->getArgs(),
149153
'reference'
150154
);
151155
} catch (CodeExecutionException $e) {
152-
$this->eventDispatcher->dispatch(new Event('cli.verify.reference-execute.fail', ['exception' => $e]));
156+
$this->eventDispatcher->dispatch(
157+
new CliExecuteEvent(
158+
'cli.verify.reference-execute.fail',
159+
$this->exercise,
160+
$input,
161+
$args,
162+
['exception' => $e]
163+
)
164+
);
153165
throw new SolutionExecutionException($e->getMessage());
154166
}
155167

156168
try {
157169
/** @var CliExecuteEvent $event */
158-
$event = $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.verify.student-execute.pre', $args));
159-
$userOutput = $this->executePhpFile($input->getRequiredArgument('program'), $event->getArgs(), 'student');
170+
$event = $this->eventDispatcher->dispatch(
171+
new CliExecuteEvent('cli.verify.student-execute.pre', $this->exercise, $input, $args)
172+
);
173+
$userOutput = $this->executePhpFile(
174+
$input,
175+
$input->getRequiredArgument('program'),
176+
$event->getArgs(),
177+
'student'
178+
);
160179
} catch (CodeExecutionException $e) {
161-
$this->eventDispatcher->dispatch(new Event('cli.verify.student-execute.fail', ['exception' => $e]));
180+
$this->eventDispatcher->dispatch(
181+
new CliExecuteEvent(
182+
'cli.verify.student-execute.fail',
183+
$this->exercise,
184+
$input,
185+
$args,
186+
['exception' => $e]
187+
)
188+
);
162189
return GenericFailure::fromArgsAndCodeExecutionFailure($args, $e);
163190
}
164191
if ($solutionOutput === $userOutput) {
@@ -186,12 +213,12 @@ private function doVerify(array $args, Input $input): CliResultInterface
186213
*/
187214
public function run(Input $input, OutputInterface $output): bool
188215
{
189-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.start', $this->exercise, $input));
216+
$this->eventDispatcher->dispatch(new CliExerciseRunnerEvent('cli.run.start', $this->exercise, $input));
190217
$success = true;
191218
foreach ($this->preserveOldArgFormat($this->exercise->getArgs()) as $i => $args) {
192219
/** @var CliExecuteEvent $event */
193220
$event = $this->eventDispatcher->dispatch(
194-
new CliExecuteEvent('cli.run.student-execute.pre', new ArrayObject($args))
221+
new CliExecuteEvent('cli.run.student-execute.pre', $this->exercise, $input, new ArrayObject($args))
195222
);
196223

197224
$args = $event->getArgs();
@@ -204,7 +231,7 @@ public function run(Input $input, OutputInterface $output): bool
204231

205232
$process->start();
206233
$this->eventDispatcher->dispatch(
207-
new CliExecuteEvent('cli.run.student.executing', $args, ['output' => $output])
234+
new CliExecuteEvent('cli.run.student.executing', $this->exercise, $input, $args, ['output' => $output])
208235
);
209236
$process->wait(function ($outputType, $outputBuffer) use ($output) {
210237
$output->write($outputBuffer);
@@ -218,23 +245,25 @@ public function run(Input $input, OutputInterface $output): bool
218245
$output->lineBreak();
219246

220247
$this->eventDispatcher->dispatch(
221-
new CliExecuteEvent('cli.run.student-execute.post', $args)
248+
new CliExecuteEvent('cli.run.student-execute.post', $this->exercise, $input, $args)
222249
);
223250
}
224251

225-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.finish', $this->exercise, $input));
252+
$this->eventDispatcher->dispatch(new CliExerciseRunnerEvent('cli.run.finish', $this->exercise, $input));
226253
return $success;
227254
}
228255

229256
/**
230257
* @param ArrayObject<int, string> $args
231258
*/
232-
private function executePhpFile(string $fileName, ArrayObject $args, string $type): string
259+
private function executePhpFile(Input $input, string $fileName, ArrayObject $args, string $type): string
233260
{
234261
$process = $this->getPhpProcess(dirname($fileName), $fileName, $args);
235262

236263
$process->start();
237-
$this->eventDispatcher->dispatch(new CliExecuteEvent(sprintf('cli.verify.%s.executing', $type), $args));
264+
$this->eventDispatcher->dispatch(
265+
new CliExecuteEvent(sprintf('cli.verify.%s.executing', $type), $this->exercise, $input, $args)
266+
);
238267
$process->wait();
239268

240269
if (!$process->isSuccessful()) {

‎test/Event/CgiExecuteEventTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use GuzzleHttp\Psr7\Request;
66
use PhpSchool\PhpWorkshop\Event\CgiExecuteEvent;
7+
use PhpSchool\PhpWorkshop\Exercise\MockExercise;
8+
use PhpSchool\PhpWorkshop\Input\Input;
79
use PHPUnit\Framework\TestCase;
810
use Psr\Http\Message\RequestInterface;
911

@@ -12,7 +14,7 @@ class CgiExecuteEventTest extends TestCase
1214
public function testAddHeader(): void
1315
{
1416
$request = new Request('GET', 'https://some.site');
15-
$e = new CgiExecuteEvent('event', $request);
17+
$e = new CgiExecuteEvent('event', new MockExercise(), new Input('test'), $request);
1618

1719
$e->addHeaderToRequest('Content-Type', 'text/html');
1820
$this->assertSame(
@@ -28,7 +30,7 @@ public function testAddHeader(): void
2830
public function testModifyRequest(): void
2931
{
3032
$request = new Request('GET', 'https://some.site');
31-
$e = new CgiExecuteEvent('event', $request);
33+
$e = new CgiExecuteEvent('event', new MockExercise(), new Input('test'), $request);
3234

3335
$e->modifyRequest(function (RequestInterface $request) {
3436
return $request
@@ -49,7 +51,7 @@ public function testModifyRequest(): void
4951
public function testGetRequest(): void
5052
{
5153
$request = new Request('GET', 'https://some.site');
52-
$e = new CgiExecuteEvent('event', $request);
54+
$e = new CgiExecuteEvent('event', new MockExercise(), new Input('test'), $request);
5355

5456
$this->assertSame($request, $e->getRequest());
5557
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Event;
4+
5+
use PhpSchool\PhpWorkshop\Event\CgiExerciseRunnerEvent;
6+
use PhpSchool\PhpWorkshop\Exercise\MockExercise;
7+
use PhpSchool\PhpWorkshop\Input\Input;
8+
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class CgiExerciseRunnerEventTest extends TestCase
12+
{
13+
public function testGetters(): void
14+
{
15+
$exercise = new MockExercise();
16+
$input = new Input('app');
17+
18+
$event = new CgiExerciseRunnerEvent('Some Event', $exercise, $input, ['number' => 1]);
19+
self::assertSame($exercise, $event->getExercise());
20+
self::assertSame($input, $event->getInput());
21+
self::assertEquals(
22+
[
23+
'exercise' => $exercise,
24+
'input' => $input,
25+
'number' => 1
26+
],
27+
$event->getParameters()
28+
);
29+
}
30+
}

‎test/Event/CliExecuteEventTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PhpSchool\PhpWorkshopTest\Event;
44

55
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
6+
use PhpSchool\PhpWorkshop\Exercise\MockExercise;
7+
use PhpSchool\PhpWorkshop\Input\Input;
68
use PhpSchool\PhpWorkshop\Utils\ArrayObject;
79
use PHPUnit\Framework\TestCase;
810

@@ -11,7 +13,7 @@ class CliExecuteEventTest extends TestCase
1113
public function testAppendArg(): void
1214
{
1315
$arr = new ArrayObject([1, 2, 3]);
14-
$e = new CliExecuteEvent('event', $arr);
16+
$e = new CliExecuteEvent('event', new MockExercise(), new Input('test'), $arr);
1517

1618
$e->appendArg('4');
1719
$this->assertEquals([1, 2, 3, 4], $e->getArgs()->getArrayCopy());
@@ -21,7 +23,7 @@ public function testAppendArg(): void
2123
public function testPrependArg(): void
2224
{
2325
$arr = new ArrayObject([1, 2, 3]);
24-
$e = new CliExecuteEvent('event', $arr);
26+
$e = new CliExecuteEvent('event', new MockExercise(), new Input('test'), $arr);
2527

2628
$e->prependArg('4');
2729
$this->assertEquals([4, 1, 2, 3], $e->getArgs()->getArrayCopy());
@@ -31,7 +33,7 @@ public function testPrependArg(): void
3133
public function testGetArgs(): void
3234
{
3335
$arr = new ArrayObject([1, 2, 3]);
34-
$e = new CliExecuteEvent('event', $arr);
36+
$e = new CliExecuteEvent('event', new MockExercise(), new Input('test'), $arr);
3537

3638
$this->assertSame($arr, $e->getArgs());
3739
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Event;
4+
5+
use PhpSchool\PhpWorkshop\Event\CliExerciseRunnerEvent;
6+
use PhpSchool\PhpWorkshop\Exercise\MockExercise;
7+
use PhpSchool\PhpWorkshop\Input\Input;
8+
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class CliExerciseRunnerEventTest extends TestCase
12+
{
13+
public function testGetters(): void
14+
{
15+
$exercise = new MockExercise();
16+
$input = new Input('app');
17+
18+
$event = new CliExerciseRunnerEvent('Some Event', $exercise, $input, ['number' => 1]);
19+
self::assertSame($exercise, $event->getExercise());
20+
self::assertSame($input, $event->getInput());
21+
self::assertEquals(
22+
[
23+
'exercise' => $exercise,
24+
'input' => $input,
25+
'number' => 1
26+
],
27+
$event->getParameters()
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.