|
2 | 2 |
|
3 | 3 | namespace PhpSchool\PhpWorkshopTest; |
4 | 4 |
|
| 5 | +use PhpSchool\PhpWorkshop\CommandArgument; |
| 6 | +use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException; |
5 | 7 | use PHPUnit_Framework_TestCase; |
6 | 8 | use PhpSchool\PhpWorkshop\CommandDefinition; |
7 | 9 |
|
8 | 10 | /** |
9 | | - * Class CommandDefinitionTest |
10 | | - * @package PhpSchool\PhpWorkshopTest |
11 | 11 | * @author Aydin Hassan <aydin@hotmail.co.uk> |
12 | 12 | */ |
13 | 13 | class CommandDefinitionTest extends PHPUnit_Framework_TestCase |
14 | 14 | { |
15 | 15 |
|
16 | | - public function testGettersSetters() |
| 16 | + public function testGettersSettersWithStringArgs() |
17 | 17 | { |
18 | 18 | $callable = function () { |
19 | 19 | }; |
20 | 20 | $definition = new CommandDefinition('animal', ['name'], $callable); |
21 | 21 |
|
22 | 22 | $this->assertSame($definition->getName(), 'animal'); |
23 | | - $this->assertSame(['name'], $definition->getRequiredArgs()); |
| 23 | + |
| 24 | + $requiredArgs = $definition->getRequiredArgs(); |
| 25 | + |
| 26 | + $this->assertCount(1, $requiredArgs); |
| 27 | + $this->assertInstanceOf(CommandArgument::class, $requiredArgs[0]); |
| 28 | + $this->assertSame('name', $requiredArgs[0]->getName()); |
| 29 | + $this->assertSame($callable, $definition->getCommandCallable()); |
| 30 | + } |
| 31 | + |
| 32 | + public function testGettersSettersWithObjArgs() |
| 33 | + { |
| 34 | + $callable = function () { |
| 35 | + }; |
| 36 | + $definition = new CommandDefinition('animal', [new CommandArgument('name')], $callable); |
| 37 | + |
| 38 | + $this->assertSame($definition->getName(), 'animal'); |
| 39 | + |
| 40 | + $requiredArgs = $definition->getRequiredArgs(); |
| 41 | + |
| 42 | + $this->assertCount(1, $requiredArgs); |
| 43 | + $this->assertInstanceOf(CommandArgument::class, $requiredArgs[0]); |
| 44 | + $this->assertSame('name', $requiredArgs[0]->getName()); |
24 | 45 | $this->assertSame($callable, $definition->getCommandCallable()); |
25 | 46 | } |
| 47 | + |
| 48 | + public function testExceptionIsThrowWhenTryingToAddRequiredArgAfterOptionalArg() |
| 49 | + { |
| 50 | + $this->expectException(InvalidArgumentException::class); |
| 51 | + $this->expectExceptionMessage('A required argument cannot follow an optional argument'); |
| 52 | + |
| 53 | + $definition = new CommandDefinition('animal', [], 'strlen'); |
| 54 | + $definition |
| 55 | + ->addArgument(CommandArgument::optional('optional-arg')) |
| 56 | + ->addArgument(CommandArgument::required('required-arg')); |
| 57 | + } |
| 58 | + |
| 59 | + public function testExceptionIsThrownWithWrongParameterToAddArgument() |
| 60 | + { |
| 61 | + $this->expectException(InvalidArgumentException::class); |
| 62 | + $msg = 'Parameter: "argument" can only be one of: "string", "PhpSchool\PhpWorkshop\CommandArgument" '; |
| 63 | + $msg .= 'Received: "stdClass"'; |
| 64 | + |
| 65 | + $this->expectExceptionMessage($msg); |
| 66 | + $definition = new CommandDefinition('animal', [], 'strlen'); |
| 67 | + $definition->addArgument(new \stdClass); |
| 68 | + } |
26 | 69 | } |
0 commit comments