1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
<?php
namespace Illuminate\Tests\Console;
use Illuminate\Console\Parser;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class ConsoleParserTest extends TestCase
{
public function testBasicParameterParsing()
{
$results = Parser::parse('command:name');
$this->assertSame('command:name', $results[0]);
$results = Parser::parse('command:name {argument} {--option}');
$this->assertSame('command:name', $results[0]);
$this->assertSame('argument', $results[1][0]->getName());
$this->assertSame('option', $results[2][0]->getName());
$this->assertFalse($results[2][0]->acceptValue());
$results = Parser::parse('command:name {argument*} {--option=}');
$this->assertSame('command:name', $results[0]);
$this->assertSame('argument', $results[1][0]->getName());
$this->assertTrue($results[1][0]->isArray());
$this->assertTrue($results[1][0]->isRequired());
$this->assertSame('option', $results[2][0]->getName());
$this->assertTrue($results[2][0]->acceptValue());
$results = Parser::parse('command:name {argument?*} {--option=*}');
$this->assertSame('command:name', $results[0]);
$this->assertSame('argument', $results[1][0]->getName());
$this->assertTrue($results[1][0]->isArray());
$this->assertFalse($results[1][0]->isRequired());
$this->assertSame('option', $results[2][0]->getName());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertTrue($results[2][0]->isArray());
$results = Parser::parse('command:name {argument?* : The argument description.} {--option=* : The option description.}');
$this->assertSame('command:name', $results[0]);
$this->assertSame('argument', $results[1][0]->getName());
$this->assertSame('The argument description.', $results[1][0]->getDescription());
$this->assertTrue($results[1][0]->isArray());
$this->assertFalse($results[1][0]->isRequired());
$this->assertSame('option', $results[2][0]->getName());
$this->assertSame('The option description.', $results[2][0]->getDescription());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertTrue($results[2][0]->isArray());
$results = Parser::parse('command:name
{argument?* : The argument description.}
{--option=* : The option description.}');
$this->assertSame('command:name', $results[0]);
$this->assertSame('argument', $results[1][0]->getName());
$this->assertSame('The argument description.', $results[1][0]->getDescription());
$this->assertTrue($results[1][0]->isArray());
$this->assertFalse($results[1][0]->isRequired());
$this->assertSame('option', $results[2][0]->getName());
$this->assertSame('The option description.', $results[2][0]->getDescription());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertTrue($results[2][0]->isArray());
}
public function testShortcutNameParsing()
{
$results = Parser::parse('command:name {--o|option}');
$this->assertSame('o', $results[2][0]->getShortcut());
$this->assertSame('option', $results[2][0]->getName());
$this->assertFalse($results[2][0]->acceptValue());
$results = Parser::parse('command:name {--o|option=}');
$this->assertSame('o', $results[2][0]->getShortcut());
$this->assertSame('option', $results[2][0]->getName());
$this->assertTrue($results[2][0]->acceptValue());
$results = Parser::parse('command:name {--o|option=*}');
$this->assertSame('command:name', $results[0]);
$this->assertSame('o', $results[2][0]->getShortcut());
$this->assertSame('option', $results[2][0]->getName());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertTrue($results[2][0]->isArray());
$results = Parser::parse('command:name {--o|option=* : The option description.}');
$this->assertSame('command:name', $results[0]);
$this->assertSame('o', $results[2][0]->getShortcut());
$this->assertSame('option', $results[2][0]->getName());
$this->assertSame('The option description.', $results[2][0]->getDescription());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertTrue($results[2][0]->isArray());
$results = Parser::parse('command:name
{--o|option=* : The option description.}');
$this->assertSame('command:name', $results[0]);
$this->assertSame('o', $results[2][0]->getShortcut());
$this->assertSame('option', $results[2][0]->getName());
$this->assertSame('The option description.', $results[2][0]->getDescription());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertTrue($results[2][0]->isArray());
}
public function testDefaultValueParsing()
{
$results = Parser::parse('command:name {argument=defaultArgumentValue} {--option=defaultOptionValue}');
$this->assertFalse($results[1][0]->isRequired());
$this->assertSame('defaultArgumentValue', $results[1][0]->getDefault());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertSame('defaultOptionValue', $results[2][0]->getDefault());
$results = Parser::parse('command:name {argument=*defaultArgumentValue1,defaultArgumentValue2} {--option=*defaultOptionValue1,defaultOptionValue2}');
$this->assertTrue($results[1][0]->isArray());
$this->assertFalse($results[1][0]->isRequired());
$this->assertEquals(['defaultArgumentValue1', 'defaultArgumentValue2'], $results[1][0]->getDefault());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertTrue($results[2][0]->isArray());
$this->assertEquals(['defaultOptionValue1', 'defaultOptionValue2'], $results[2][0]->getDefault());
}
public function testArgumentDefaultValue()
{
$results = Parser::parse('command:name {argument= : The argument description.}');
$this->assertNull($results[1][0]->getDefault());
$results = Parser::parse('command:name {argument=default : The argument description.}');
$this->assertSame('default', $results[1][0]->getDefault());
}
public function testOptionDefaultValue()
{
$results = Parser::parse('command:name {--option= : The option description.}');
$this->assertNull($results[2][0]->getDefault());
$results = Parser::parse('command:name {--option=default : The option description.}');
$this->assertSame('default', $results[2][0]->getDefault());
}
public function testNameIsSpacesException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to determine command name from signature.');
Parser::parse(" \t\n\r\x0B\f");
}
public function testNameInEmptyException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to determine command name from signature.');
Parser::parse('');
}
}
|