Skip to content

Commit 95986f9

Browse files
committed
added dynamic args support
1 parent 57f06a1 commit 95986f9

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

lib/Local.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Local {
1818
public function __construct() {
1919
$this->key = getenv("BROWSERSTACK_ACCESS_KEY");
2020
$this->logfile = getcwd() . "/local.log";
21+
$this->user_args = array();
2122
}
2223

2324
public function __destruct() {
@@ -32,40 +33,46 @@ public function isRunning() {
3233
}
3334

3435
public function add_args($arg_key, $value = NULL) {
35-
if ($arg_key == "-key")
36+
if ($arg_key == "key")
3637
$this->key = $value;
37-
elseif ($arg_key == "-binaryPath")
38+
elseif ($arg_key == "binaryPath")
3839
$this->binary_path = $value;
39-
elseif ($arg_key == "-logfile")
40+
elseif ($arg_key == "logfile")
4041
$this->logfile = $value;
41-
elseif ($arg_key == "-v")
42+
elseif ($arg_key == "v")
4243
$this->verbose_flag = "-vvv";
43-
elseif ($arg_key == "-force")
44+
elseif ($arg_key == "force")
4445
$this->force_flag = "-force";
45-
elseif ($arg_key == "-only")
46+
elseif ($arg_key == "only")
4647
$this->only_flag = "-only";
47-
elseif ($arg_key == "-onlyAutomate")
48+
elseif ($arg_key == "onlyAutomate")
4849
$this->only_automate_flag = "-onlyAutomate";
49-
elseif ($arg_key == "-forcelocal")
50+
elseif ($arg_key == "forcelocal")
5051
$this->force_local_flag = "-forcelocal";
51-
elseif ($arg_key == "-localIdentifier")
52+
elseif ($arg_key == "localIdentifier")
5253
$this->local_identifier_flag = "-localIdentifier $value";
53-
elseif ($arg_key == "-proxyHost")
54+
elseif ($arg_key == "proxyHost")
5455
$this->proxy_host = "-proxyHost $value";
55-
elseif ($arg_key == "-proxyPort")
56+
elseif ($arg_key == "proxyPort")
5657
$this->proxy_port = "-proxyPort $value";
57-
elseif ($arg_key == "-proxyUser")
58+
elseif ($arg_key == "proxyUser")
5859
$this->proxy_user = "-proxyUser $value";
59-
elseif ($arg_key == "-proxyPass")
60+
elseif ($arg_key == "proxyPass")
6061
$this->proxy_pass = "-proxyPass $value";
61-
elseif ($arg_key == "-forceproxy")
62+
elseif ($arg_key == "forceproxy")
6263
$this->force_proxy_flag = "-forceproxy";
63-
elseif ($arg_key == "-hosts")
64+
elseif ($arg_key == "hosts")
6465
$this->hosts = $value;
65-
elseif ($arg_key == "-f") {
66+
elseif ($arg_key == "f") {
6667
$this->folder_flag = "-f";
6768
$this->folder_path = $value;
6869
}
70+
elseif (strtolower($value) == "true"){
71+
array_push($this->user_args, "-$arg_key");
72+
}
73+
else {
74+
array_push($this->user_args, "-$arg_key '$value'");
75+
}
6976
}
7077

7178
public function start($arguments) {
@@ -138,7 +145,8 @@ public function command() {
138145
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
139146
$exec = "call";
140147

141-
$command = "$exec $this->binary_path -logFile $this->logfile $this->folder_flag $this->key $this->folder_path $this->force_local_flag $this->local_identifier_flag $this->only_flag $this->only_automate_flag $this->proxy_host $this->proxy_port $this->proxy_user $this->proxy_pass $this->force_proxy_flag $this->force_flag $this->verbose_flag $this->hosts";
148+
$user_args = join(' ', $this->user_args);
149+
$command = "$exec $this->binary_path -logFile $this->logfile $this->folder_flag $this->key $this->folder_path $this->force_local_flag $this->local_identifier_flag $this->only_flag $this->only_automate_flag $this->proxy_host $this->proxy_port $this->proxy_user $this->proxy_pass $this->force_proxy_flag $this->force_flag $this->verbose_flag $this->hosts $user_args";
142150
$command = preg_replace('/\s+/S', " ", $command);
143151
return $command;
144152
}

tests/LocalTest.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,59 @@ public function tearDown(){
2121
}
2222

2323
public function test_verbose() {
24-
$this->bs_local->add_args('-v');
24+
$this->bs_local->add_args('v');
2525
$this->assertContains('-v',$this->bs_local->command());
2626
}
2727

2828
public function test_set_folder() {
29-
$this->bs_local->add_args('-f', "/");
29+
$this->bs_local->add_args('f', "/");
3030
$this->assertContains('-f',$this->bs_local->command());
3131
$this->assertContains('/',$this->bs_local->command());
3232
}
3333

3434
public function test_enable_force() {
35-
$this->bs_local->add_args("-force");
35+
$this->bs_local->add_args("force");
3636
}
3737

3838
public function test_set_local_identifier() {
39-
$this->bs_local->add_args("-localIdentifier", "randomString");
39+
$this->bs_local->add_args("localIdentifier", "randomString");
4040
$this->assertContains('-localIdentifier randomString',$this->bs_local->command());
4141
}
4242

4343
public function test_enable_only() {
44-
$this->bs_local->add_args("-only");
44+
$this->bs_local->add_args("only");
4545
$this->assertContains('-only',$this->bs_local->command());
4646
}
4747

4848
public function test_enable_only_automate() {
49-
$this->bs_local->add_args("-onlyAutomate");
49+
$this->bs_local->add_args("onlyAutomate");
5050
$this->assertContains('-onlyAutomate', $this->bs_local->command());
5151
}
5252

5353
public function test_enable_force_local() {
54-
$this->bs_local->add_args("-forcelocal");
54+
$this->bs_local->add_args("forcelocal");
5555
$this->assertContains('-forcelocal',$this->bs_local->command());
5656
}
5757

58+
public function test_custom_boolean_argument() {
59+
$this->bs_local->add_args("boolArg1", true);
60+
$this->bs_local->add_args("boolArg2", true);
61+
$this->assertContains('-boolArg1',$this->bs_local->command());
62+
$this->assertContains('-boolArg2',$this->bs_local->command());
63+
}
64+
65+
public function test_custom_keyval() {
66+
$this->bs_local->add_args("customKey1", "custom value1");
67+
$this->bs_local->add_args("customKey2", "custom value2");
68+
$this->assertContains('-customKey1 \'custom value1\'',$this->bs_local->command());
69+
$this->assertContains('-customKey2 \'custom value2\'',$this->bs_local->command());
70+
}
71+
5872
public function test_set_proxy() {
59-
$this->bs_local->add_args("-proxyHost", "localhost");
60-
$this->bs_local->add_args("-proxyPort", 8080);
61-
$this->bs_local->add_args("-proxyUser", "user");
62-
$this->bs_local->add_args("-proxyPass", "pass");
73+
$this->bs_local->add_args("proxyHost", "localhost");
74+
$this->bs_local->add_args("proxyPort", 8080);
75+
$this->bs_local->add_args("proxyUser", "user");
76+
$this->bs_local->add_args("proxyPass", "pass");
6377
$this->assertContains('-proxyHost localhost -proxyPort 8080 -proxyUser user -proxyPass pass',$this->bs_local->command());
6478
}
6579

@@ -68,35 +82,34 @@ public function test_enable_force_proxy() {
6882
$this->assertContains('-forceproxy',$this->bs_local->command());
6983
}
7084

71-
7285
public function test_hosts() {
7386
$this->bs_local->add_args("-hosts", "localhost,8080,0");
7487
$this->assertContains('localhost,8080,0',$this->bs_local->command());
7588
}
7689

7790
public function test_isRunning() {
7891
$this->assertFalse($this->bs_local->isRunning());
79-
$this->bs_local->start(array('-v' => true));
92+
$this->bs_local->start(array('v' => true));
8093
$this->assertTrue($this->bs_local->isRunning());
8194
$this->bs_local->stop();
8295
$this->assertFalse($this->bs_local->isRunning());
83-
$this->bs_local->start(array('-v' => true));
96+
$this->bs_local->start(array('v' => true));
8497
$this->assertTrue($this->bs_local->isRunning());
8598
}
8699

87100
public function test_checkPid() {
88101
$this->assertFalse($this->bs_local->isRunning());
89-
$this->bs_local->start(array('-v' => true));
102+
$this->bs_local->start(array('v' => true));
90103
$this->assertTrue($this->bs_local->pid > 0);
91104
}
92105

93106
public function test_multiple_binary() {
94-
$this->bs_local->start(array('-v' => true));
107+
$this->bs_local->start(array('v' => true));
95108
$bs_local_2 = new Local();
96109
$log_file2 = getcwd(). '/log2.log';
97110
print($log_file2);
98111
try {
99-
$bs_local_2->start(array('-v' => true, '-logfile' => $log_file2));
112+
$bs_local_2->start(array('v' => true, 'logfile' => $log_file2));
100113
$this->fail("Expected Exception has not been raised.");
101114
} catch (LocalException $ex) {
102115
$emessage = $ex->getMessage();

0 commit comments

Comments
 (0)