Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e3395cb
updated s3 logs
tr4n2uil Mar 1, 2016
4da0882
added phpunit xml
tr4n2uil Mar 1, 2016
da96427
updated readme
tr4n2uil Mar 1, 2016
7143a2e
added composer install
tr4n2uil Mar 1, 2016
6bb8eac
use php 5.5
tr4n2uil Mar 1, 2016
85851f7
disable multiple binary check
tr4n2uil Mar 1, 2016
1c6cdef
download issue
tr4n2uil Mar 1, 2016
8bdb256
stop fix
tr4n2uil Mar 1, 2016
1f7ce11
more fixes
tr4n2uil Mar 1, 2016
fdc2623
fixes tearDown
tr4n2uil Mar 1, 2016
407a5e6
check only is running
tr4n2uil Mar 1, 2016
26288af
readd multiple binary test
tr4n2uil Mar 1, 2016
6a2f59a
try sleep
tr4n2uil Mar 1, 2016
e8875a3
try sleep
tr4n2uil Mar 1, 2016
ed98e77
proc close wait to close
tr4n2uil Mar 1, 2016
044b9ed
added posix kill
tr4n2uil Mar 1, 2016
cfd0188
sigterm
tr4n2uil Mar 1, 2016
f64c0cd
exec kill
tr4n2uil Mar 1, 2016
a7279bf
kill int
tr4n2uil Mar 1, 2016
24bdfa6
kill int
tr4n2uil Mar 1, 2016
15bdf45
kill int
tr4n2uil Mar 1, 2016
d660677
stricter is running
tr4n2uil Mar 2, 2016
bca2453
read stdin pipe
tr4n2uil Mar 2, 2016
40615b2
added print
tr4n2uil Mar 2, 2016
ae4491a
try check status
tr4n2uil Mar 2, 2016
37543e3
kill -2
tr4n2uil Mar 2, 2016
e19a6da
kill -2
tr4n2uil Mar 2, 2016
1e42fcc
no proc term
tr4n2uil Mar 2, 2016
13b2bce
add proc close
tr4n2uil Mar 2, 2016
55b6967
trying
tr4n2uil Mar 2, 2016
7392ec2
kill 15
tr4n2uil Mar 2, 2016
bcf452a
echo command
tr4n2uil Mar 2, 2016
a7741c1
added echo
tr4n2uil Mar 2, 2016
9519529
try force for is running test
tr4n2uil Mar 2, 2016
20b57c8
sig term
tr4n2uil Mar 2, 2016
4294f64
proc close issue
tr4n2uil Mar 2, 2016
e06bfc9
remove force
tr4n2uil Mar 2, 2016
a87d0d7
add force
tr4n2uil Mar 2, 2016
70ac51a
no close
tr4n2uil Mar 2, 2016
999de91
added build status
tr4n2uil Mar 2, 2016
e088bf3
check ps aux debug
tr4n2uil Mar 2, 2016
216f885
use exec
tr4n2uil Mar 2, 2016
d3d3b4e
fixes travis issues
tr4n2uil Mar 2, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
language: php

php:
- '5.5'

before_install:
- true && `base64 --decode <<< ZXhwb3J0IEJST1dTRVJTVEFDS19BQ0NFU1NfS0VZPUh5VmZydXJvb3dYb041eGhLZEs2Cg==`
- true && composer install

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# browserstack-local-php

[![Build Status](https://travis-ci.org/browserstack/browserstack-local-php.svg?branch=master)](https://travis-ci.org/browserstack/browserstack-local-php)

## Setup

Installation is possible using [Composer](https://getcomposer.org/).
Expand Down Expand Up @@ -52,6 +54,6 @@ Test the installation by running a simple test file, check out example.php in th
Testing is possible using [PHPUnit](https://phpunit.de/).

To run the tests, run the command:
`phpunit tests/LocalTest.php`
`phpunit`


4 changes: 4 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

$me = new Local();
$me->isRunning();
echo "starting";
$args = array("v" => 1);
$me->start($args);
echo "started";
echo $me->isRunning();
echo "stopping";
$me->stop();
echo "stopped";
echo $me->isRunning();
38 changes: 30 additions & 8 deletions lib/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Local {
private $handle = NULL;
private $pipes = array();
private $loghandle = NULL;
public $pid = NULL;

public function __construct() {
$this->key = getenv("BROWSERSTACK_ACCESS_KEY");
Expand All @@ -27,7 +28,7 @@ public function isRunning() {
return False;

$status = proc_get_status($this->handle);
return $status["running"];
return is_null($status["running"]) ? false : $status["running"];
}

public function add_args($arg_key, $value = NULL) {
Expand Down Expand Up @@ -73,24 +74,29 @@ public function start($arguments) {
$this->binary_path = $this->binary->binary_path();

$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);

$call = $this->command();
system('echo "" > '. $this->logfile);
$this->handle = proc_open($call, $descriptorspec, $this->pipes);
$status = proc_get_status($this->handle);
$this->pid = $status['pid'];

$this->loghandle = fopen($this->logfile,"r");
while (true) {
$buffer = fread($this->loghandle, 1024);
if (preg_match("/Error:[^\n]+/i", $buffer, $match)) {
$this->stop();
throw new LocalException($match[0]);
proc_terminate($this->handle);
break;
}
elseif (preg_match("/\bPress Ctrl-C to exit\b/i", $buffer, $match))
elseif (preg_match("/\bPress Ctrl-C to exit\b/i", $buffer, $match)){
fclose($this->loghandle);
break;
}

//flush();
sleep(1);
Expand All @@ -101,12 +107,28 @@ public function stop() {
fclose($this->loghandle);
if (is_null($this->handle))
return;
else
else {
while(fgets($this->pipes[0]));
fclose($this->pipes[0]);
fclose($this->pipes[1]);
fclose($this->pipes[2]);

if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
exec('kill -15 ' . $this->pid);

proc_terminate($this->handle);
while($this->isRunning())
sleep(1);
}
}

public function command() {
$command = "$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_flag $this->verbose_flag $this->hosts";
$exec = "exec";
// TODO to test on windows
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
$exec = "call";

$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_flag $this->verbose_flag $this->hosts";
$command = preg_replace('/\s+/S', " ", $command);
return $command;
}
Expand Down
23 changes: 16 additions & 7 deletions lib/LocalBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,41 @@ private function server_home() {

private function platform_url(){
if (PHP_OS == "Darwin")
return "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-darwin-x64";
return 'https://s3.amazonaws.com/bs-automate-prod/local/BrowserStackLocal-darwin-x64';
else if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
return "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal.exe";
return 'https://s3.amazonaws.com/bs-automate-prod/local/BrowserStackLocal-win32.exe';
if ((strtoupper(PHP_OS)) == "LINUX") {
if (PHP_INT_SIZE * 8 == 64)
return "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-x64";
return 'https://s3.amazonaws.com/bs-automate-prod/local/BrowserStackLocal-linux-x64';
else
return "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-ia32";
return 'https://s3.amazonaws.com/bs-automate-prod/local/BrowserStackLocal-linux-ia32';
}
}

public function download_binary($path) {
$url = $this->platform_url();
if (!file_exists($path))
mkdir($path, 0777, true);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$file = fopen($path . '/BrowserStackLocal', "w+");
fputs($file, $data);
fclose($file);

file_put_contents($path . '/BrowserStackLocal', fopen($url, 'r'));
chmod($path . '/BrowserStackLocal', 0755);
return $path . "/BrowserStackLocal";
}

private function get_available_dirs() {
$arrlength = count($this->possible_binary_paths);
for($x = 0; $x < $arrlength; $x++) {
$path = $this->possible_binary_paths[$x];
$localpath = $path . "/BrowserStackLocal";
if(file_exists($localpath) || $this->make_path($path))
if(file_exists($path) || $this->make_path($path))
return $path;
}
throw new LocalException("Error trying to download BrowserStack Local binary");
Expand Down
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<phpunit>
<testsuites>
<testsuite name="local">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

37 changes: 21 additions & 16 deletions tests/LocalTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
// An example of using php-browserstacklocal.

namespace BrowserStack;

Expand All @@ -17,6 +16,10 @@ public function setUp(){
$this->bs_local = new Local();
}

public function tearDown(){
$this->bs_local->stop();
}

public function test_verbose() {
$this->bs_local->add_args('v');
$this->assertContains('-v',$this->bs_local->command());
Expand Down Expand Up @@ -65,30 +68,32 @@ public function test_hosts() {
$this->assertContains('localhost,8080,0',$this->bs_local->command());
}

public function test_isRunning() {
$this->assertFalse($this->bs_local->isRunning());
$this->bs_local->start(array('v' => true));
$this->assertTrue($this->bs_local->isRunning());
$this->bs_local->stop();
$this->assertFalse($this->bs_local->isRunning());
$this->bs_local->start(array('v' => true));
$this->assertTrue($this->bs_local->isRunning());
}

public function test_checkPid() {
$this->assertFalse($this->bs_local->isRunning());
$this->bs_local->start(array('v' => true));
$this->assertTrue($this->bs_local->pid > 0);
}

public function test_multiple_binary() {
$this->bs_local->start(array('v' => true));
$bs_local_2 = new Local();
try {
$bs_local_2->start(array('v' => true));
$this->fail("Expected Exception has not been raised.");
} catch (LocalException $ex) {
$emessage = $ex->getMessage();
$this->assertEquals(trim($emessage), 'Error: Either another browserstack local client is running on your machine or some server is listening on port 45691');
$bs_local_2->stop();
$this->bs_local->stop();
sleep(2);
return;
}
$this->fail("Expected Exception has not been raised.");
$this->bs_local->stop();
sleep(2);
}

public function test_isRunning() {
$this->assertFalse($this->bs_local->isRunning());
$this->bs_local->start(array('v' => true));
$this->assertTrue($this->bs_local->isRunning());
$this->bs_local->stop();
sleep(2);
$this->assertFalse($this->bs_local->isRunning());
}
}