|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PhpWorkshopTest\Listener; |
| 4 | + |
| 5 | +use PhpSchool\PhpWorkshop\CommandDefinition; |
| 6 | +use PhpSchool\PhpWorkshop\Event\Event; |
| 7 | +use PhpSchool\PhpWorkshop\Listener\CheckExerciseAssignedListener; |
| 8 | +use PHPUnit_Framework_TestCase; |
| 9 | +use PhpSchool\PhpWorkshop\UserState; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author Aydin Hassan <aydin@hotmail.co.uk> |
| 13 | + */ |
| 14 | +class CheckExerciseAssignedListenerTest extends PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @dataProvider commandsThatRequireAssignedExercise |
| 18 | + * @param CommandDefinition $command |
| 19 | + */ |
| 20 | + public function testExceptionIsThrownIfNoExerciseAssigned(CommandDefinition $command) |
| 21 | + { |
| 22 | + $state = new UserState; |
| 23 | + |
| 24 | + $this->expectException(\RuntimeException::class); |
| 25 | + $this->expectExceptionMessage('No active exercise. Select one from the menu'); |
| 26 | + |
| 27 | + $listener = new CheckExerciseAssignedListener($state); |
| 28 | + $listener->__invoke(new Event('some-event', ['command' => $command])); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @dataProvider commandsThatRequireAssignedExercise |
| 33 | + * @param CommandDefinition $command |
| 34 | + */ |
| 35 | + public function testExceptionIsNotThrownIfExerciseAssigned(CommandDefinition $command) |
| 36 | + { |
| 37 | + $state = new UserState(['exercise1'], 'exercise1'); |
| 38 | + $listener = new CheckExerciseAssignedListener($state); |
| 39 | + $listener->__invoke(new Event('some-event', ['command' => $command])); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @return array |
| 44 | + */ |
| 45 | + public function commandsThatRequireAssignedExercise() |
| 46 | + { |
| 47 | + return [ |
| 48 | + [$this->command('verify')], |
| 49 | + [$this->command('run')], |
| 50 | + [$this->command('print')], |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @dataProvider commandsThatDoNotRequireAssignedExercise |
| 56 | + * @param CommandDefinition $command |
| 57 | + */ |
| 58 | + public function testExceptionIsNotThrownIfCommandDoesNotRequireAssignedExercise(CommandDefinition $command) |
| 59 | + { |
| 60 | + $state = new UserState(['exercise1'], 'exercise1'); |
| 61 | + $listener = new CheckExerciseAssignedListener($state); |
| 62 | + $listener->__invoke(new Event('some-event', ['command' => $command])); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return array |
| 67 | + */ |
| 68 | + public function commandsThatDoNotRequireAssignedExercise() |
| 69 | + { |
| 70 | + return [ |
| 71 | + [$this->command('help')], |
| 72 | + [$this->command('credits')], |
| 73 | + [$this->command('menu')], |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param $commandName |
| 79 | + * @return \PHPUnit_Framework_MockObject_MockObject|CommandDefinition |
| 80 | + */ |
| 81 | + private function command($commandName) |
| 82 | + { |
| 83 | + $command = $this->createMock(CommandDefinition::class); |
| 84 | + $command |
| 85 | + ->expects($this->any()) |
| 86 | + ->method('getName') |
| 87 | + ->willReturn($commandName); |
| 88 | + |
| 89 | + return $command; |
| 90 | + } |
| 91 | +} |
0 commit comments