5 require __DIR__
."/../../autoload.php";
7 class CliArgsTest
extends \PHPUnit_Framework_TestCase
19 protected function setUp() {
21 ["h", "help", "Display help",
22 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
23 ["v", "verbose", "More output",
24 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
25 ["q", "quiet", "Less output",
26 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
27 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
28 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
29 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
30 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
32 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
33 CliArgs
::OPTIONAL|CliArgs
::MULTI|CliArgs
::REQARG
],
34 ["s", "sudo", "Installation might need increased privileges",
35 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::OPTARG
,
38 $this->args
= new CliArgs($this->spec
);
41 public function testCompile() {
42 $args = $this->args
->compile($this->spec
);
43 $this->assertSame($args, $this->args
);
44 foreach ($this->spec
as $arg) {
45 $spec["-".$arg[0]] = $arg;
46 $spec["--".$arg[1]] = $arg;
48 $this->assertSame($args->getCompiledSpec(), $spec);
51 public function testParseNothing() {
52 $generator = $this->args
->parse(0, []);
53 $this->assertInstanceOf("Generator", $generator);
54 foreach ($generator as $error) {
55 throw new \
Exception("Unexpected parse error: $error");
59 public function testParseHalt() {
60 foreach ($this->args
->parse(1, ["-hv"]) as $error) {
61 throw new \
Exception("Unexpected parse error: $error");
63 $this->assertTrue($this->args
->help
, "help");
64 $this->assertNull($this->args
->verbose
, "verbose");
65 $this->assertNull($this->args
->quiet
, "quiet");
66 foreach ($this->args
->parse(1, ["-vhq"]) as $error) {
67 throw new \
Exception("Unexpected parse error: $error");
69 $this->assertTrue($this->args
->help
);
70 $this->assertTrue($this->args
->verbose
);
71 $this->assertNull($this->args
->quiet
);
74 public function testOptArg() {
75 $this->assertFalse(isset($this->args
->sudo
));
76 $this->assertSame("sudo -S %s", $this->args
->sudo
);
77 foreach ($this->args
->parse(1, ["--sudo"]) as $error) {
78 throw new \
Exception("Unexpected parse error: $error");
80 $this->assertSame("sudo -S %s", $this->args
->sudo
);
81 $this->assertNull($this->args
->quiet
);
82 foreach ($this->args
->parse(2, ["--sudo", "--quiet"]) as $error) {
83 throw new \
Exception("Unexpected parse error: $error");
85 $this->assertSame("sudo -S %s", $this->args
->sudo
);
86 $this->assertTrue($this->args
->quiet
);
87 foreach ($this->args
->parse(3, ["--sudo", "su -c '%s'", "--quiet"]) as $error) {
88 throw new \
Exception("Unexpected parse error: $error");
90 $this->assertSame("su -c '%s'", $this->args
->sudo
);
93 public function testReqArg() {
94 $this->assertNull($this->args
->configure
);
95 foreach ($this->args
->parse(1, ["-c"]) as $error) {
96 $this->assertStringMatchesFormat("%s--configure%srequires%sargument", $error);
98 $this->assertTrue(isset($error));
101 public function testMulti() {
102 foreach ($this->args
->parse(4, ["-c", "--with-foo", "--configure", "--enable-bar"]) as $error) {
103 throw new \
Exception("Unexpected parse error: $error");
105 $this->assertSame(["--with-foo", "--enable-bar"], $this->args
->configure
);
108 public function testUnknown() {
109 $this->assertNull($this->args
->configure
);
110 foreach ($this->args
->parse(1, ["--unknown"]) as $error) {
111 $this->assertStringMatchesFormat("%SUnknown%s--unknown%S", $error);
113 $this->assertTrue(isset($error));
116 public function testLongEquals() {
117 $this->assertNull($this->args
->configure
);
118 foreach ($this->args
->parse(1, ["--configure=--with-lib=/opt/lib"]) as $error) {
119 throw new \
Exception("Unexpected parse error: $error");
121 $this->assertSame(["--with-lib=/opt/lib"], $this->args
->configure
);
124 public function testValidate() {
125 $this->args
->compile([
126 ["r", "required-option", "This option is required",
127 CliArgs
::REQUIRED|CliArgs
::NOARG
]
129 foreach ($this->args
->parse(0, []) as $error) {
130 throw new \
Exception("Unexpected parse error: $error");
132 foreach ($this->args
->validate() as $error) {
133 $this->assertStringMatchesFormat("%srequired-option%srequired", $error);
135 $this->assertTrue(isset($error));