ff05c8ff8cc6c05e408e1ed70418ef7e0f3f88ba
[pharext/pharext] / tests / src / pharext / CliArgsTest.php
1 <?php
2
3 namespace pharext;
4
5 require __DIR__."/../../autoload.php";
6
7 class CliArgsTest extends \PHPUnit_Framework_TestCase
8 {
9 /**
10 * @var CliArgs
11 */
12 protected $args;
13
14 /**
15 * @var array
16 */
17 protected $spec;
18
19 protected function setUp() {
20 $this->spec = [
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,
31 "php"],
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,
36 "sudo -S %s"]
37 ];
38 $this->args = new CliArgs($this->spec);
39 }
40
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;
47 }
48 $this->assertSame($args->getCompiledSpec(), $spec);
49 }
50
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");
56 }
57 }
58
59 public function testParseHalt() {
60 foreach ($this->args->parse(1, ["-hv"]) as $error) {
61 throw new \Exception("Unexpected parse error: $error");
62 }
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");
68 }
69 $this->assertTrue($this->args->help);
70 $this->assertTrue($this->args->verbose);
71 $this->assertNull($this->args->quiet);
72 }
73
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");
79 }
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");
84 }
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");
89 }
90 $this->assertSame("su -c '%s'", $this->args->sudo);
91 }
92
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);
97 }
98 $this->assertTrue(isset($error));
99 }
100
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");
104 }
105 $this->assertSame(["--with-foo", "--enable-bar"], $this->args->configure);
106 }
107
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);
112 }
113 $this->assertTrue(isset($error));
114 }
115
116 public function testValidate() {
117 $this->args->compile([
118 ["r", "required-option", "This option is required",
119 CliArgs::REQUIRED|CliArgs::NOARG]
120 ]);
121 foreach ($this->args->parse(0, []) as $error) {
122 throw new \Exception("Unexpected parse error: $error");
123 }
124 foreach ($this->args->validate() as $error) {
125 $this->assertStringMatchesFormat("%srequired-option%srequired", $error);
126 }
127 $this->assertTrue(isset($error));
128 }
129
130 }