Skip to content

Extract getSolution to seperate interface #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Exercise/CgiExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @package PhpSchool\PhpWorkshop\Exercise
*/
interface CgiExercise
interface CgiExercise extends ProvidesSolution
{
/**
* This method should return an array of PSR-7 requests, which will be forwarded to the student's
Expand Down
2 changes: 1 addition & 1 deletion src/Exercise/CliExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @package PhpSchool\PhpWorkshop\Exercise
*/
interface CliExercise
interface CliExercise extends ProvidesSolution
{
/**
* This method should return an array of strings which will be passed to the student's solution
Expand Down
21 changes: 7 additions & 14 deletions src/Exercise/ExerciseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public function getName();
*/
public function getType();

/**
* Get the absolute path to the markdown file which contains the exercise problem.
*
* @return string
*/
public function getProblem();

/**
* This is where the exercise specifies the extra checks it may require. It is also
* possible to grab the event dispatcher from the exercise dispatcher and listen to any
Expand All @@ -46,20 +53,6 @@ public function configure(ExerciseDispatcher $dispatcher);
*/
public function getDescription();

/**
* Get the exercise solution.
*
* @return SolutionInterface
*/
public function getSolution();

/**
* Get the absolute path to the markdown file which contains the exercise problem.
*
* @return string
*/
public function getProblem();

/**
* Allows to perform some cleanup after the exercise solution's have been executed, for example
* remove files, close DB connections.
Expand Down
18 changes: 18 additions & 0 deletions src/Exercise/ProvidesSolution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpSchool\PhpWorkshop\Exercise;

use PhpSchool\PhpWorkshop\Solution\SolutionInterface;

/**
* @author Aydin Hassan <[email protected]>
*/
interface ProvidesSolution
{
/**
* Get the exercise solution.
*
* @return SolutionInterface
*/
public function getSolution();
}
29 changes: 16 additions & 13 deletions src/ResultRenderer/ResultsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Colors\Color;
use PhpSchool\CliMenu\Terminal\TerminalInterface;
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
use PhpSchool\PhpWorkshop\Factory\ResultRendererFactory;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Result\SuccessInterface;
Expand Down Expand Up @@ -153,23 +154,25 @@ private function renderSuccessInformation(
$output->writeLine($this->style(" PASS!", ['green', 'bg_default', 'bold']));
$output->emptyLine();

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

foreach ($exercise->getSolution()->getFiles() as $file) {
$output->writeLine($this->style($file->getRelativePath(), ['bold', 'cyan', 'underline']));
$output->emptyLine();
foreach ($exercise->getSolution()->getFiles() as $file) {
$output->writeLine($this->style($file->getRelativePath(), ['bold', 'cyan', 'underline']));
$output->emptyLine();

$code = $file->getContents();
if (pathinfo($file->getRelativePath(), PATHINFO_EXTENSION) === 'php') {
$code = $this->syntaxHighlighter->highlight($code);
}
$code = $file->getContents();
if (pathinfo($file->getRelativePath(), PATHINFO_EXTENSION) === 'php') {
$code = $this->syntaxHighlighter->highlight($code);
}

//make sure there is a new line at the end
$code = preg_replace('/\n$/', '', $code) . "\n";
//make sure there is a new line at the end
$code = preg_replace('/\n$/', '', $code) . "\n";

$output->write($code);
$output->writeLine($this->lineBreak());
$output->write($code);
$output->writeLine($this->lineBreak());
}
}

$completedCount = count($userState->getCompletedExercises());
Expand Down
7 changes: 4 additions & 3 deletions test/Listener/PrepareSolutionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpSchool\PhpWorkshopTest\Listener;

use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
Expand Down Expand Up @@ -50,7 +51,7 @@ public function testIfSolutionRequiresComposerButComposerCannotBeLocatedExceptio
$refProp->setValue($this->listener, []);

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

$solution = $this->createMock(SolutionInterface::class);
$exercise = $this->createMock(ExerciseInterface::class);
$exercise = $this->createMock([ExerciseInterface::class, CliExercise::class]);
$exercise->expects($this->any())
->method('getSolution')
->will($this->returnValue($solution));
Expand Down Expand Up @@ -105,7 +106,7 @@ public function testIfSolutionRequiresComposerComposerInstallIsExecuted()
]));

$solution = $this->createMock(SolutionInterface::class);
$exercise = $this->createMock(ExerciseInterface::class);
$exercise = $this->createMock([ExerciseInterface::class, CliExercise::class]);
$exercise->expects($this->any())
->method('getSolution')
->will($this->returnValue($solution));
Expand Down