Skip to content

File comparison check #202

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 7 commits into from
Jun 12, 2021
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
6 changes: 6 additions & 0 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
declare(strict_types=1);

use Colors\Color;
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Listener\InitialCodeListener;
use PhpSchool\PhpWorkshop\Listener\TearDownListener;
use PhpSchool\PhpWorkshop\Logger\ConsoleLogger;
use PhpSchool\PhpWorkshop\Logger\Logger;
use PhpSchool\PhpWorkshop\Result\FileComparisonFailure;
use PhpSchool\PhpWorkshop\ResultRenderer\FileComparisonFailureRenderer;
use Psr\Log\LoggerInterface;
use function DI\create;
use function DI\factory;
Expand Down Expand Up @@ -122,6 +125,7 @@
$c->get(ComposerCheck::class),
$c->get(FunctionRequirementsCheck::class),
$c->get(DatabaseCheck::class),
$c->get(FileComparisonCheck::class)
]);
},
CommandRouter::class => function (ContainerInterface $c) {
Expand Down Expand Up @@ -261,6 +265,7 @@
},
DatabaseCheck::class => create(),
ComposerCheck::class => create(),
FileComparisonCheck::class => create(),

//Utils
Filesystem::class => create(),
Expand Down Expand Up @@ -332,6 +337,7 @@ function (CgiResult $result) use ($c) {
$factory->registerRenderer(CliRequestFailure::class, CliRequestFailureRenderer::class);

$factory->registerRenderer(ComparisonFailure::class, ComparisonFailureRenderer::class);
$factory->registerRenderer(FileComparisonFailure::class, FileComparisonFailureRenderer::class);

return $factory;
},
Expand Down
93 changes: 93 additions & 0 deletions src/Check/FileComparisonCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Check;

use InvalidArgumentException;
use PhpSchool\PhpWorkshop\Exception\SolutionFileDoesNotExistException;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
use PhpSchool\PhpWorkshop\ExerciseCheck\FileComparisonExerciseCheck;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Result\Failure;
use PhpSchool\PhpWorkshop\Result\FileComparisonFailure;
use PhpSchool\PhpWorkshop\Result\ResultInterface;
use PhpSchool\PhpWorkshop\Result\Success;
use PhpSchool\PhpWorkshop\Utils\Path;

/**
* This check verifies that any additional files which should be created by a student, match the ones
* created by the reference solution.
*/
class FileComparisonCheck implements SimpleCheckInterface
{
/**
* Return the check's name.
*/
public function getName(): string
{
return 'File Comparison Check';
}

/**
* Simply check that the file exists.
*
* @param ExerciseInterface&ProvidesSolution $exercise The exercise to check against.
* @param Input $input The command line arguments passed to the command.
* @return ResultInterface The result of the check.
*/
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
{
if (!$exercise instanceof FileComparisonExerciseCheck) {
throw new InvalidArgumentException();
}

foreach ($exercise->getFilesToCompare() as $file) {
$studentFile = Path::join(dirname($input->getRequiredArgument('program')), $file);
$referenceFile = Path::join($exercise->getSolution()->getBaseDirectory(), $file);

if (!file_exists($referenceFile)) {
throw SolutionFileDoesNotExistException::fromExpectedFile($file);
}

if (!file_exists($studentFile)) {
return Failure::fromCheckAndReason($this, sprintf('File: "%s" does not exist', $file));
}

$actual = (string) file_get_contents($studentFile);
$expected = (string) file_get_contents($referenceFile);

if ($expected !== $actual) {
return new FileComparisonFailure($this, $file, $expected, $actual);
}
}

return Success::fromCheck($this);
}

/**
* This check can run on any exercise type.
*
* @param ExerciseType $exerciseType
* @return bool
*/
public function canRun(ExerciseType $exerciseType): bool
{
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI], true);
}

public function getExerciseInterface(): string
{
return FileComparisonExerciseCheck::class;
}

/**
* This check must run after executing the solution because the files will not exist otherwise.
*/
public function getPosition(): string
{
return SimpleCheckInterface::CHECK_AFTER;
}
}
20 changes: 20 additions & 0 deletions src/Exception/SolutionFileDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Exception;

/**
* Represents the situation where an exercise requires compares a user generated file with
* one provided with the solution but it does not exist
*/
class SolutionFileDoesNotExistException extends RuntimeException
{
/**
* Static constructor to create an instance from the expected filename.
*/
public static function fromExpectedFile(string $expectedFile): self
{
return new self(sprintf('File: "%s" does not exist in solution folder', $expectedFile));
}
}
17 changes: 17 additions & 0 deletions src/ExerciseCheck/FileComparisonExerciseCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\ExerciseCheck;

/**
* This interface should be implemented when you require the check `\PhpSchool\PhpWorkshop\Check\FileComparisonCheck`
* in your exercise.
*/
interface FileComparisonExerciseCheck
{
/**
* @return array<string>
*/
public function getFilesToCompare(): array;
}
74 changes: 74 additions & 0 deletions src/Result/FileComparisonFailure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Result;

use PhpSchool\PhpWorkshop\Check\CheckInterface;

/**
* Result to represent a failed file comparison
*/
class FileComparisonFailure implements FailureInterface
{
use ResultTrait;

/**
* @var string
*/
private $fileName;

/**
* @var string
*/
private $expectedValue;

/**
* @var string
*/
private $actualValue;

/**
* @param CheckInterface $check The check that produced this result.
* @param string $fileName
* @param string $expectedValue
* @param string $actualValue
*/
public function __construct(CheckInterface $check, string $fileName, string $expectedValue, string $actualValue)
{
$this->check = $check;
$this->fileName = $fileName;
$this->expectedValue = $expectedValue;
$this->actualValue = $actualValue;
}

/**
* Get the name of the file to be verified
*
* @return string
*/
public function getFileName(): string
{
return $this->fileName;
}

/**
* Get the expected value.
*
* @return string
*/
public function getExpectedValue(): string
{
return $this->expectedValue;
}

/**
* Get the actual value.
*
* @return string
*/
public function getActualValue(): string
{
return $this->actualValue;
}
}
62 changes: 62 additions & 0 deletions src/ResultRenderer/FileComparisonFailureRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\ResultRenderer;

use PhpSchool\PhpWorkshop\Result\FileComparisonFailure;

/**
* Renderer for `PhpSchool\PhpWorkshop\Result\FileComparisonFailure`.
*/
class FileComparisonFailureRenderer implements ResultRendererInterface
{
/**
* @var FileComparisonFailure
*/
private $result;

/**
* @param FileComparisonFailure $result The failure.
*/
public function __construct(FileComparisonFailure $result)
{
$this->result = $result;
}

/**
* Print the actual and expected output.
*
* @param ResultsRenderer $renderer
* @return string
*/
public function render(ResultsRenderer $renderer): string
{
return sprintf(
" %s%s\n%s\n\n %s%s\n%s\n",
$renderer->style('YOUR OUTPUT FOR: ', ['bold', 'yellow']),
$renderer->style($this->result->getFileName(), ['bold', 'green']),
$this->indent($renderer->style(sprintf('"%s"', $this->result->getActualValue()), 'red')),
$renderer->style('EXPECTED OUTPUT FOR: ', ['bold', 'yellow']),
$renderer->style($this->result->getFileName(), ['bold', 'green']),
$this->indent($renderer->style(sprintf('"%s"', $this->result->getExpectedValue()), 'green'))
);
}

/**
* @param string $string
* @return string
*/
private function indent(string $string): string
{
return implode(
"\n",
array_map(
function ($line) {
return sprintf(' %s', $line);
},
explode("\n", $string)
)
);
}
}
78 changes: 78 additions & 0 deletions test/Asset/FileComparisonExercise.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\ComposerCheck;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseCheck\FileComparisonExerciseCheck;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;

class FileComparisonExercise implements ExerciseInterface, FileComparisonExerciseCheck
{
/**
* @var array<string>
*/
private $files;

/**
* @var SolutionInterface
*/
private $solution;

public function __construct(array $files)
{
$this->files = $files;
}

public function getName(): string
{
// TODO: Implement getName() method.
}

public function getDescription(): string
{
// TODO: Implement getDescription() method.
}

public function setSolution(SolutionInterface $solution): void
{
$this->solution = $solution;
}

public function getSolution(): SolutionInterface
{
return $this->solution;
}

public function getProblem(): string
{
// TODO: Implement getProblem() method.
}

public function tearDown(): void
{
// TODO: Implement tearDown() method.
}

public function getArgs(): array
{
return []; // TODO: Implement getArgs() method.
}

public function getType(): ExerciseType
{
return ExerciseType::CLI();
}

public function configure(ExerciseDispatcher $dispatcher): void
{
$dispatcher->requireCheck(ComposerCheck::class);
}

public function getFilesToCompare(): array
{
return $this->files;
}
}
Loading