improve help output
[pharext/pharext] / tests / src / pharext / CliArgsTest.php
1 <?php
2
3 namespace pharext;
4
5 require __DIR__."/../../autoload.php";
6
7 use pharext\Cli\Args as CliArgs;
8
9 class CliArgsTest extends \PHPUnit_Framework_TestCase
10 {
11 /**
12 * @var CliArgs
13 */
14 protected $args;
15
16 /**
17 * @var array
18 */
19 protected $spec;
20
21 protected function setUp() {
22 $this->spec = [
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,
33 "php"],
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,
38 "sudo -S %s"]
39 ];
40 $this->args = new CliArgs($this->spec);
41 }
42
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;
49 }
50 $this->assertSame($args->getCompiledSpec(), $spec);
51 }
52
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");
58 }
59 }
60
61 public function testParseHalt() {
62 foreach ($this->args->parse(1, ["-hv"]) as $error) {
63 throw new \Exception("Unexpected parse error: $error");
64 }
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");
70 }
71 $this->assertTrue($this->args->help);
72 $this->assertTrue($this->args->verbose);
73 $this->assertNull($this->args->quiet);
74 }
75
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");
81 }
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");
86 }
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");
91 }
92 $this->assertSame("su -c '%s'", $this->args->sudo);
93 }
94
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);
99 }
100 $this->assertTrue(isset($error));
101 }
102
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");
106 }
107 $this->assertSame(["--with-foo", "--enable-bar"], $this->args->configure);
108 }
109
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);
114 }
115 $this->assertTrue(isset($error));
116 }
117
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");
122 }
123 $this->assertSame(["--with-lib=/opt/lib"], $this->args->configure);
124 }
125
126 public function testValidate() {
127 $this->args->compile([
128 ["r", "required-option", "This option is required",
129 CliArgs::REQUIRED|CliArgs::NOARG]
130 ]);
131 foreach ($this->args->parse(0, []) as $error) {
132 throw new \Exception("Unexpected parse error: $error");
133 }
134 foreach ($this->args->validate() as $error) {
135 $this->assertStringMatchesFormat("%srequired-option%srequired", $error);
136 }
137 $this->assertTrue(isset($error));
138 }
139
140 }