5 require __DIR__
."/../../autoload.php";
7 use pharext\Cli\Args
as CliArgs
;
9 class CliArgsTest
extends \PHPUnit_Framework_TestCase
21 protected function setUp() {
23 ["h", "help", "Display help",
24 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
25 ["v", "verbose", "More output",
26 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
27 ["q", "quiet", "Less output",
28 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
29 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
30 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
31 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
32 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
34 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
35 CliArgs
::OPTIONAL|CliArgs
::MULTI|CliArgs
::REQARG
],
36 ["s", "sudo", "Installation might need increased privileges",
37 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::OPTARG
,
40 $this->args
= new CliArgs($this->spec
);
43 public function testCompile() {
44 $args = $this->args
->compile($this->spec
);
45 $this->assertSame($args, $this->args
);
46 foreach ($this->spec
as $arg) {
47 $spec["-".$arg[0]] = $arg;
48 $spec["--".$arg[1]] = $arg;
50 $this->assertSame($args->getCompiledSpec(), $spec);
53 public function testParseNothing() {
54 $generator = $this->args
->parse(0, []);
55 $this->assertInstanceOf("Generator", $generator);
56 foreach ($generator as $error) {
57 throw new \
Exception("Unexpected parse error: $error");
61 public function testParseHalt() {
62 foreach ($this->args
->parse(1, ["-hv"]) as $error) {
63 throw new \
Exception("Unexpected parse error: $error");
65 $this->assertTrue($this->args
->help
, "help");
66 $this->assertNull($this->args
->verbose
, "verbose");
67 $this->assertNull($this->args
->quiet
, "quiet");
68 foreach ($this->args
->parse(1, ["-vhq"]) as $error) {
69 throw new \
Exception("Unexpected parse error: $error");
71 $this->assertTrue($this->args
->help
);
72 $this->assertTrue($this->args
->verbose
);
73 $this->assertNull($this->args
->quiet
);
76 public function testOptArg() {
77 $this->assertFalse(isset($this->args
->sudo
));
78 $this->assertSame("sudo -S %s", $this->args
->sudo
);
79 foreach ($this->args
->parse(1, ["--sudo"]) as $error) {
80 throw new \
Exception("Unexpected parse error: $error");
82 $this->assertSame("sudo -S %s", $this->args
->sudo
);
83 $this->assertNull($this->args
->quiet
);
84 foreach ($this->args
->parse(2, ["--sudo", "--quiet"]) as $error) {
85 throw new \
Exception("Unexpected parse error: $error");
87 $this->assertSame("sudo -S %s", $this->args
->sudo
);
88 $this->assertTrue($this->args
->quiet
);
89 foreach ($this->args
->parse(3, ["--sudo", "su -c '%s'", "--quiet"]) as $error) {
90 throw new \
Exception("Unexpected parse error: $error");
92 $this->assertSame("su -c '%s'", $this->args
->sudo
);
95 public function testReqArg() {
96 $this->assertNull($this->args
->configure
);
97 foreach ($this->args
->parse(1, ["-c"]) as $error) {
98 $this->assertStringMatchesFormat("%s--configure%srequires%sargument", $error);
100 $this->assertTrue(isset($error));
103 public function testMulti() {
104 foreach ($this->args
->parse(4, ["-c", "--with-foo", "--configure", "--enable-bar"]) as $error) {
105 throw new \
Exception("Unexpected parse error: $error");
107 $this->assertSame(["--with-foo", "--enable-bar"], $this->args
->configure
);
110 public function testUnknown() {
111 $this->assertNull($this->args
->configure
);
112 foreach ($this->args
->parse(1, ["--unknown"]) as $error) {
113 $this->assertStringMatchesFormat("%SUnknown%s--unknown%S", $error);
115 $this->assertTrue(isset($error));
118 public function testLongEquals() {
119 $this->assertNull($this->args
->configure
);
120 foreach ($this->args
->parse(1, ["--configure=--with-lib=/opt/lib"]) as $error) {
121 throw new \
Exception("Unexpected parse error: $error");
123 $this->assertSame(["--with-lib=/opt/lib"], $this->args
->configure
);
126 public function testValidate() {
127 $this->args
->compile([
128 ["r", "required-option", "This option is required",
129 CliArgs
::REQUIRED|CliArgs
::NOARG
]
131 foreach ($this->args
->parse(0, []) as $error) {
132 throw new \
Exception("Unexpected parse error: $error");
134 foreach ($this->args
->validate() as $error) {
135 $this->assertStringMatchesFormat("%srequired-option%srequired", $error);
137 $this->assertTrue(isset($error));