Skip to content

Commit bc82383

Browse files
committed
Refactor listeners to use Input object
1 parent 62b854a commit bc82383

File tree

6 files changed

+27
-15
lines changed

6 files changed

+27
-15
lines changed

src/ExerciseCheck/SelfCheck.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpSchool\PhpWorkshop\ExerciseCheck;
44

5+
use PhpSchool\PhpWorkshop\Input\Input;
56
use PhpSchool\PhpWorkshop\Result\ResultInterface;
67

78
/**
@@ -21,8 +22,8 @@ interface SelfCheck
2122
* The method is passed the absolute file path to the student's solution and should return a result
2223
* object which indicates the success or not of the check.
2324
*
24-
* @param string $fileName The absolute path to the student's solution.
25+
* @param Input $input The command line arguments passed to the command.
2526
* @return ResultInterface The result of the check.
2627
*/
27-
public function check($fileName);
28+
public function check(Input $input);
2829
}

src/Listener/CodePatchListener.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\PhpWorkshop\CodePatcher;
66
use PhpSchool\PhpWorkshop\Event\Event;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use RuntimeException;
89

910
/**
@@ -37,7 +38,10 @@ public function __construct(CodePatcher $codePatcher)
3738
*/
3839
public function patch(Event $event)
3940
{
40-
$fileName = $event->getParameter('fileName');
41+
/** @var Input $input */
42+
$input = $event->getParameter('input');
43+
$fileName = $input->getArgument('program');
44+
4145
$this->originalCode = file_get_contents($fileName);
4246
file_put_contents(
4347
$fileName,
@@ -54,6 +58,10 @@ public function revert(Event $event)
5458
throw new RuntimeException('Can only revert previously patched code');
5559
}
5660

57-
file_put_contents($event->getParameter('fileName'), $this->originalCode);
61+
/** @var Input $input */
62+
$input = $event->getParameter('input');
63+
$fileName = $input->getArgument('program');
64+
65+
file_put_contents($fileName, $this->originalCode);
5866
}
5967
}

src/Listener/PrepareSolutionListener.php

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PhpSchool\PhpWorkshop\Listener;
44

55
use PhpSchool\PhpWorkshop\Event\Event;
6-
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
76
use RuntimeException;
87
use Symfony\Component\Process\Process;
98

src/Listener/SelfCheckListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __invoke(Event $event)
3434
$exercise = $event->getParameter('exercise');
3535

3636
if ($exercise instanceof SelfCheck) {
37-
$this->results->add($exercise->check($event->getParameter('fileName')));
37+
$this->results->add($exercise->check($event->getParameter('input')));
3838
}
3939
}
4040
}

test/Listener/CodePatchListenerTest.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpSchool\PhpWorkshop\CodePatcher;
66
use PhpSchool\PhpWorkshop\Event\Event;
77
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
8+
use PhpSchool\PhpWorkshop\Input\Input;
89
use PhpSchool\PhpWorkshop\Listener\CodePatchListener;
910
use PHPUnit_Framework_TestCase;
1011
use RuntimeException;
@@ -44,11 +45,11 @@ public function setUp()
4445

4546
public function testRevertThrowsExceptionIfPatchNotPreviouslyCalled()
4647
{
47-
$fileName = $this->file;
48+
$input = new Input('app', ['program' => $this->file]);
4849
$exercise = $this->createMock(ExerciseInterface::class);
4950

5051
$listener = new CodePatchListener($this->codePatcher);
51-
$event = new Event('event', compact('exercise', 'fileName'));
52+
$event = new Event('event', compact('exercise', 'input'));
5253

5354
$this->expectException(RuntimeException::class);
5455
$this->expectExceptionMessage('Can only revert previously patched code');
@@ -59,7 +60,7 @@ public function testPatchUpdatesCode()
5960
{
6061
file_put_contents($this->file, 'ORIGINAL CONTENT');
6162

62-
$fileName = $this->file;
63+
$input = new Input('app', ['program' => $this->file]);
6364
$exercise = $this->createMock(ExerciseInterface::class);
6465

6566
$this->codePatcher
@@ -69,7 +70,7 @@ public function testPatchUpdatesCode()
6970
->will($this->returnValue('MODIFIED CONTENT'));
7071

7172
$listener = new CodePatchListener($this->codePatcher);
72-
$event = new Event('event', compact('exercise', 'fileName'));
73+
$event = new Event('event', compact('exercise', 'input'));
7374
$listener->patch($event);
7475

7576
$this->assertStringEqualsFile($this->file, 'MODIFIED CONTENT');
@@ -79,7 +80,7 @@ public function testRevertAfterPatch()
7980
{
8081
file_put_contents($this->file, 'ORIGINAL CONTENT');
8182

82-
$fileName = $this->file;
83+
$input = new Input('app', ['program' => $this->file]);
8384
$exercise = $this->createMock(ExerciseInterface::class);
8485

8586
$this->codePatcher
@@ -89,7 +90,7 @@ public function testRevertAfterPatch()
8990
->will($this->returnValue('MODIFIED CONTENT'));
9091

9192
$listener = new CodePatchListener($this->codePatcher);
92-
$event = new Event('event', compact('exercise', 'fileName'));
93+
$event = new Event('event', compact('exercise', 'input'));
9394
$listener->patch($event);
9495
$listener->revert($event);
9596

test/Listener/SelfCheckListenerTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\PhpWorkshop\Event\Event;
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use PhpSchool\PhpWorkshop\Listener\SelfCheckListener;
89
use PhpSchool\PhpWorkshop\Result\Success;
910
use PhpSchool\PhpWorkshop\ResultAggregator;
@@ -20,13 +21,14 @@ class SelfCheckListenerTest extends PHPUnit_Framework_TestCase
2021
public function testSelfCheck()
2122
{
2223
$exercise = $this->createMock(SelfCheckExerciseInterface::class);
23-
$event = new Event('event', ['exercise' => $exercise, 'fileName' => 'some-file.php']);
24+
$input = new Input('app', ['program' => 'some-file.php']);
25+
$event = new Event('event', compact('exercise', 'input'));
2426

2527
$success = new Success('Success');
2628
$exercise
2729
->expects($this->once())
2830
->method('check')
29-
->with('some-file.php')
31+
->with($input)
3032
->will($this->returnValue($success));
3133

3234
$results = new ResultAggregator;
@@ -40,7 +42,8 @@ public function testSelfCheck()
4042
public function testExerciseWithOutSelfCheck()
4143
{
4244
$exercise = $this->createMock(ExerciseInterface::class);
43-
$event = new Event('event', ['exercise' => $exercise, 'fileName' => 'some-file.php']);
45+
$input = new Input('app', ['program' => 'some-file.php']);
46+
$event = new Event('event', compact('exercise', 'input'));
4447

4548
$results = new ResultAggregator;
4649
$listener = new SelfCheckListener($results);

0 commit comments

Comments
 (0)