|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PhpWorkshopTest\Check; |
| 4 | + |
| 5 | +use PhpSchool\PhpWorkshop\Check\FileComparisonCheck; |
| 6 | +use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface; |
| 7 | +use PhpSchool\PhpWorkshop\Exception\SolutionFileDoesNotExistException; |
| 8 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
| 9 | +use PhpSchool\PhpWorkshop\ExerciseCheck\FileComparisonExerciseCheck; |
| 10 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 11 | +use PhpSchool\PhpWorkshop\Result\FileComparisonFailure; |
| 12 | +use PhpSchool\PhpWorkshop\Solution\SingleFileSolution; |
| 13 | +use PhpSchool\PhpWorkshopTest\Asset\FileComparisonExercise; |
| 14 | +use PhpSchool\PhpWorkshopTest\BaseTest; |
| 15 | +use PhpSchool\PhpWorkshop\Result\Failure; |
| 16 | +use PhpSchool\PhpWorkshop\Result\Success; |
| 17 | + |
| 18 | +class FileComparisonCheckTest extends BaseTest |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var FileComparisonCheck |
| 22 | + */ |
| 23 | + private $check; |
| 24 | + |
| 25 | + public function setUp(): void |
| 26 | + { |
| 27 | + $this->check = new FileComparisonCheck(); |
| 28 | + $this->assertEquals('File Comparison Check', $this->check->getName()); |
| 29 | + $this->assertEquals(FileComparisonExerciseCheck::class, $this->check->getExerciseInterface()); |
| 30 | + $this->assertEquals(SimpleCheckInterface::CHECK_AFTER, $this->check->getPosition()); |
| 31 | + |
| 32 | + $this->assertTrue($this->check->canRun(ExerciseType::CGI())); |
| 33 | + $this->assertTrue($this->check->canRun(ExerciseType::CLI())); |
| 34 | + } |
| 35 | + |
| 36 | + public function testExceptionIsThrownIfReferenceFileDoesNotExist(): void |
| 37 | + { |
| 38 | + $this->expectException(SolutionFileDoesNotExistException::class); |
| 39 | + $this->expectExceptionMessage('File: "some-file.txt" does not exist in solution folder'); |
| 40 | + |
| 41 | + $exercise = new FileComparisonExercise(['some-file.txt']); |
| 42 | + $exercise->setSolution(new SingleFileSolution($this->getTemporaryFile('solution/solution.php'))); |
| 43 | + |
| 44 | + $this->check->check($exercise, new Input('app', ['program' => 'my-solution.php'])); |
| 45 | + } |
| 46 | + |
| 47 | + public function testFailureIsReturnedIfStudentsFileDoesNotExist(): void |
| 48 | + { |
| 49 | + $file = $this->getTemporaryFile('solution/some-file.txt'); |
| 50 | + file_put_contents($file, "name,age\nAydin,33\nMichael,29\n"); |
| 51 | + |
| 52 | + $exercise = new FileComparisonExercise(['some-file.txt']); |
| 53 | + $exercise->setSolution(new SingleFileSolution($this->getTemporaryFile('solution/solution.php'))); |
| 54 | + |
| 55 | + $failure = $this->check->check($exercise, new Input('app', ['program' => 'my-solution.php'])); |
| 56 | + |
| 57 | + $this->assertInstanceOf(Failure::class, $failure); |
| 58 | + $this->assertEquals('File: "some-file.txt" does not exist', $failure->getReason()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testFailureIsReturnedIfStudentFileDosNotMatchReferenceFile(): void |
| 62 | + { |
| 63 | + $file = $this->getTemporaryFile('solution/some-file.txt'); |
| 64 | + file_put_contents($file, "name,age\nAydin,33\nMichael,29\n"); |
| 65 | + |
| 66 | + $studentSolution = $this->getTemporaryFile('student/my-solution.php'); |
| 67 | + $studentFile = $this->getTemporaryFile('student/some-file.txt'); |
| 68 | + file_put_contents($studentFile, "somegibberish"); |
| 69 | + |
| 70 | + $exercise = new FileComparisonExercise(['some-file.txt']); |
| 71 | + $exercise->setSolution(new SingleFileSolution($this->getTemporaryFile('solution/solution.php'))); |
| 72 | + |
| 73 | + $failure = $this->check->check($exercise, new Input('app', ['program' => $studentSolution])); |
| 74 | + |
| 75 | + $this->assertInstanceOf(FileComparisonFailure::class, $failure); |
| 76 | + $this->assertEquals($failure->getFileName(), 'some-file.txt'); |
| 77 | + $this->assertEquals($failure->getExpectedValue(), "name,age\nAydin,33\nMichael,29\n"); |
| 78 | + $this->assertEquals($failure->getActualValue(), "somegibberish"); |
| 79 | + } |
| 80 | + |
| 81 | + public function testSuccessIsReturnedIfFilesMatch(): void |
| 82 | + { |
| 83 | + $file = $this->getTemporaryFile('solution/some-file.txt'); |
| 84 | + file_put_contents($file, "name,age\nAydin,33\nMichael,29\n"); |
| 85 | + |
| 86 | + $studentSolution = $this->getTemporaryFile('student/my-solution.php'); |
| 87 | + $studentFile = $this->getTemporaryFile('student/some-file.txt'); |
| 88 | + file_put_contents($studentFile, "name,age\nAydin,33\nMichael,29\n"); |
| 89 | + |
| 90 | + $exercise = new FileComparisonExercise(['some-file.txt']); |
| 91 | + $exercise->setSolution(new SingleFileSolution($this->getTemporaryFile('solution/solution.php'))); |
| 92 | + |
| 93 | + $this->assertInstanceOf( |
| 94 | + Success::class, |
| 95 | + $this->check->check($exercise, new Input('app', ['program' => $studentSolution])) |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
0 commit comments