-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from clue-labs/detect-binary
Use default `php` binary instead of respecting `PHP_BINARY` when automatic binary detection fails for non-CLI SAPIs
- Loading branch information
Showing
7 changed files
with
144 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/composer.lock | ||
/examples/users.db | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
// $ php examples/query.php "INSERT INTO user (name) VALUES ('Bob'),('Carol')" | ||
// $ php examples/query.php "DELETE FROM user WHERE name = ?" "Carol" | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$factory = new Clue\React\SQLite\Factory(); | ||
$db = $factory->openLazy(__DIR__ . '/users.db'); | ||
|
||
$query = isset($argv[1]) ? $argv[1] : 'SELECT 42 AS value'; | ||
$args = array_slice(isset($argv) ? $argv : [], 2); | ||
|
||
$db->query($query, $args)->then(function (Clue\React\SQLite\Result $result) { | ||
if ($result->columns !== null) { | ||
echo implode("\t", $result->columns) . PHP_EOL; | ||
foreach ($result->rows as $row) { | ||
echo implode("\t", $row) . PHP_EOL; | ||
} | ||
} else { | ||
echo "changed\tid". PHP_EOL; | ||
echo $result->changed . "\t" . $result->insertId . PHP_EOL; | ||
} | ||
}, function (Exception $e) { | ||
echo 'Error: ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
$db->quit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Clue\Tests\React\SQLite; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class FunctionalExampleTest extends TestCase | ||
{ | ||
public function testQueryExampleReturnsDefaultValue() | ||
{ | ||
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' query.php'); | ||
|
||
$this->assertEquals('value' . "\n" . '42' . "\n", $output); | ||
} | ||
|
||
public function testQueryExampleReturnsCalculatedValueFromPlaceholderVariables() | ||
{ | ||
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' query.php "SELECT ?+? AS result" 1 2'); | ||
|
||
$this->assertEquals('result' . "\n" . '3' . "\n", $output); | ||
} | ||
|
||
public function testQueryExampleExecutedWithCgiReturnsDefaultValueAfterContentTypeHeader() | ||
{ | ||
if (!$this->canExecute('php-cgi --version')) { | ||
$this->markTestSkipped('Unable to execute "php-cgi"'); | ||
} | ||
|
||
$output = $this->execExample('php-cgi query.php'); | ||
|
||
$this->assertStringEndsWith("\n\n" . 'value' . "\n" . '42' . "\n", $output); | ||
} | ||
|
||
public function testQueryExampleWithOpenBasedirRestrictedReturnsDefaultValue() | ||
{ | ||
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' -dopen_basedir=' . escapeshellarg(dirname(__DIR__)) . ' query.php'); | ||
|
||
$this->assertEquals('value' . "\n" . '42' . "\n", $output); | ||
} | ||
|
||
public function testQueryExampleWithOpenBasedirRestrictedAndAdditionalFileDescriptorReturnsDefaultValue() | ||
{ | ||
if (DIRECTORY_SEPARATOR === '\\') { | ||
$this->markTestSkipped('Not supported on Windows'); | ||
} | ||
|
||
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' -dopen_basedir=' . escapeshellarg(dirname(__DIR__)) . ' query.php 3</dev/null'); | ||
|
||
$this->assertEquals('value' . "\n" . '42' . "\n", $output); | ||
} | ||
|
||
public function testQueryExampleExecutedWithCgiAndOpenBasedirRestrictedRunsDefaultPhpAndReturnsDefaultValueAfterContentTypeHeader() | ||
{ | ||
if (!$this->canExecute('php-cgi --version') || !$this->canExecute('php --version')) { | ||
$this->markTestSkipped('Unable to execute "php-cgi" or "php"'); | ||
} | ||
|
||
$output = $this->execExample('php-cgi -dopen_basedir=' . escapeshellarg(dirname(__DIR__)) . ' query.php'); | ||
|
||
$this->assertStringEndsWith("\n\n" . 'value' . "\n" . '42' . "\n", $output); | ||
} | ||
|
||
private function canExecute($command) | ||
{ | ||
$code = 1; | ||
$null = DIRECTORY_SEPARATOR === '\\' ? 'NUL' : '/dev/null'; | ||
system("$command >$null 2>$null", $code); | ||
|
||
return $code === 0; | ||
} | ||
|
||
private function execExample($command) | ||
{ | ||
chdir(__DIR__ . '/../examples/'); | ||
|
||
return str_replace("\r\n", "\n", shell_exec($command)); | ||
} | ||
} |