|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpSchool\PhpWorkshopTest\Listener; |
| 6 | + |
| 7 | +use PhpSchool\PhpWorkshop\Event\Event; |
| 8 | +use PhpSchool\PhpWorkshop\Listener\InitialCodeListener; |
| 9 | +use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl; |
| 10 | +use PhpSchool\PhpWorkshopTest\Asset\ExerciseWithInitialCode; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Symfony\Component\Filesystem\Filesystem; |
| 13 | + |
| 14 | +class InitialCodeListenerTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var Filesystem |
| 18 | + */ |
| 19 | + private $filesystem; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + private $cwd; |
| 25 | + |
| 26 | + public function setUp(): void |
| 27 | + { |
| 28 | + $this->filesystem = new Filesystem(); |
| 29 | + |
| 30 | + $this->cwd = sprintf('%s/%s', str_replace('\\', '/', sys_get_temp_dir()), $this->getName()); |
| 31 | + mkdir($this->cwd, 0775, true); |
| 32 | + } |
| 33 | + |
| 34 | + public function testExerciseCodeIsCopiedIfExerciseProvidesInitialCode(): void |
| 35 | + { |
| 36 | + $exercise = new ExerciseWithInitialCode(); |
| 37 | + |
| 38 | + $event = new Event('exercise.selected', ['exercise' => $exercise]); |
| 39 | + |
| 40 | + $listener = new InitialCodeListener($this->cwd); |
| 41 | + $listener->__invoke($event); |
| 42 | + |
| 43 | + $this->assertFileExists($this->cwd . '/init-solution.php'); |
| 44 | + $this->assertFileEquals( |
| 45 | + $exercise->getInitialCode()->getFiles()[0]->getAbsolutePath(), |
| 46 | + $this->cwd . '/init-solution.php' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public function testExerciseCodeIsNotCopiedIfExerciseDoesNotProvideInitialCode(): void |
| 51 | + { |
| 52 | + $exercise = new CliExerciseImpl(); |
| 53 | + |
| 54 | + $event = new Event('exercise.selected', ['exercise' => $exercise]); |
| 55 | + |
| 56 | + $listener = new InitialCodeListener($this->cwd); |
| 57 | + $listener->__invoke($event); |
| 58 | + |
| 59 | + $this->assertEmpty(array_diff(scandir($this->cwd), ['.', '..'])); |
| 60 | + } |
| 61 | + |
| 62 | + public function tearDown(): void |
| 63 | + { |
| 64 | + $this->filesystem->remove($this->cwd); |
| 65 | + } |
| 66 | +} |
0 commit comments