Skip to content

Commit 270f19e

Browse files
authored
Merge pull request #139 from php-school/extract-get-solution
Extract getSolution to seperate interface
2 parents 8ea28cb + 7135c09 commit 270f19e

File tree

6 files changed

+47
-32
lines changed

6 files changed

+47
-32
lines changed

src/Exercise/CgiExercise.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @package PhpSchool\PhpWorkshop\Exercise
1111
*/
12-
interface CgiExercise
12+
interface CgiExercise extends ProvidesSolution
1313
{
1414
/**
1515
* This method should return an array of PSR-7 requests, which will be forwarded to the student's

src/Exercise/CliExercise.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @package PhpSchool\PhpWorkshop\Exercise
99
*/
10-
interface CliExercise
10+
interface CliExercise extends ProvidesSolution
1111
{
1212
/**
1313
* This method should return an array of strings which will be passed to the student's solution

src/Exercise/ExerciseInterface.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public function getName();
2929
*/
3030
public function getType();
3131

32+
/**
33+
* Get the absolute path to the markdown file which contains the exercise problem.
34+
*
35+
* @return string
36+
*/
37+
public function getProblem();
38+
3239
/**
3340
* This is where the exercise specifies the extra checks it may require. It is also
3441
* possible to grab the event dispatcher from the exercise dispatcher and listen to any
@@ -46,20 +53,6 @@ public function configure(ExerciseDispatcher $dispatcher);
4653
*/
4754
public function getDescription();
4855

49-
/**
50-
* Get the exercise solution.
51-
*
52-
* @return SolutionInterface
53-
*/
54-
public function getSolution();
55-
56-
/**
57-
* Get the absolute path to the markdown file which contains the exercise problem.
58-
*
59-
* @return string
60-
*/
61-
public function getProblem();
62-
6356
/**
6457
* Allows to perform some cleanup after the exercise solution's have been executed, for example
6558
* remove files, close DB connections.

src/Exercise/ProvidesSolution.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Exercise;
4+
5+
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
6+
7+
/**
8+
* @author Aydin Hassan <[email protected]>
9+
*/
10+
interface ProvidesSolution
11+
{
12+
/**
13+
* Get the exercise solution.
14+
*
15+
* @return SolutionInterface
16+
*/
17+
public function getSolution();
18+
}

src/ResultRenderer/ResultsRenderer.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Colors\Color;
66
use PhpSchool\CliMenu\Terminal\TerminalInterface;
7+
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
78
use PhpSchool\PhpWorkshop\Factory\ResultRendererFactory;
89
use PhpSchool\PhpWorkshop\Output\OutputInterface;
910
use PhpSchool\PhpWorkshop\Result\SuccessInterface;
@@ -153,23 +154,25 @@ private function renderSuccessInformation(
153154
$output->writeLine($this->style(" PASS!", ['green', 'bg_default', 'bold']));
154155
$output->emptyLine();
155156

156-
$output->writeLine("Here's the official solution in case you want to compare notes:");
157-
$output->writeLine($this->lineBreak());
157+
if ($exercise instanceof ProvidesSolution) {
158+
$output->writeLine("Here's the official solution in case you want to compare notes:");
159+
$output->writeLine($this->lineBreak());
158160

159-
foreach ($exercise->getSolution()->getFiles() as $file) {
160-
$output->writeLine($this->style($file->getRelativePath(), ['bold', 'cyan', 'underline']));
161-
$output->emptyLine();
161+
foreach ($exercise->getSolution()->getFiles() as $file) {
162+
$output->writeLine($this->style($file->getRelativePath(), ['bold', 'cyan', 'underline']));
163+
$output->emptyLine();
162164

163-
$code = $file->getContents();
164-
if (pathinfo($file->getRelativePath(), PATHINFO_EXTENSION) === 'php') {
165-
$code = $this->syntaxHighlighter->highlight($code);
166-
}
165+
$code = $file->getContents();
166+
if (pathinfo($file->getRelativePath(), PATHINFO_EXTENSION) === 'php') {
167+
$code = $this->syntaxHighlighter->highlight($code);
168+
}
167169

168-
//make sure there is a new line at the end
169-
$code = preg_replace('/\n$/', '', $code) . "\n";
170+
//make sure there is a new line at the end
171+
$code = preg_replace('/\n$/', '', $code) . "\n";
170172

171-
$output->write($code);
172-
$output->writeLine($this->lineBreak());
173+
$output->write($code);
174+
$output->writeLine($this->lineBreak());
175+
}
173176
}
174177

175178
$completedCount = count($userState->getCompletedExercises());

test/Listener/PrepareSolutionListenerTest.php

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

55
use PhpSchool\PhpWorkshop\Event\Event;
6+
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
67
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
78
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
89
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
@@ -50,7 +51,7 @@ public function testIfSolutionRequiresComposerButComposerCannotBeLocatedExceptio
5051
$refProp->setValue($this->listener, []);
5152

5253
$solution = $this->createMock(SolutionInterface::class);
53-
$exercise = $this->createMock(ExerciseInterface::class);
54+
$exercise = $this->createMock([ExerciseInterface::class, CliExercise::class]);
5455
$exercise->expects($this->any())
5556
->method('getSolution')
5657
->will($this->returnValue($solution));
@@ -72,7 +73,7 @@ public function testIfSolutionRequiresComposerButVendorDirExistsNothingIsDone()
7273
$this->assertFileExists(sprintf('%s/vendor', dirname($this->file)));
7374

7475
$solution = $this->createMock(SolutionInterface::class);
75-
$exercise = $this->createMock(ExerciseInterface::class);
76+
$exercise = $this->createMock([ExerciseInterface::class, CliExercise::class]);
7677
$exercise->expects($this->any())
7778
->method('getSolution')
7879
->will($this->returnValue($solution));
@@ -105,7 +106,7 @@ public function testIfSolutionRequiresComposerComposerInstallIsExecuted()
105106
]));
106107

107108
$solution = $this->createMock(SolutionInterface::class);
108-
$exercise = $this->createMock(ExerciseInterface::class);
109+
$exercise = $this->createMock([ExerciseInterface::class, CliExercise::class]);
109110
$exercise->expects($this->any())
110111
->method('getSolution')
111112
->will($this->returnValue($solution));

0 commit comments

Comments
 (0)