Skip to content

Commit fb0145d

Browse files
committed
Add query example and test CLI/CGI environment
1 parent a57b936 commit fb0145d

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

examples/query.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
// $ php examples/query.php "INSERT INTO user (name) VALUES ('Bob'),('Carol')"
4+
// $ php examples/query.php "DELETE FROM user WHERE name = ?" "Carol"
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$factory = new Clue\React\SQLite\Factory();
9+
$db = $factory->openLazy(__DIR__ . '/users.db');
10+
11+
$query = isset($argv[1]) ? $argv[1] : 'SELECT 42 AS value';
12+
$args = array_slice(isset($argv) ? $argv : [], 2);
13+
14+
$db->query($query, $args)->then(function (Clue\React\SQLite\Result $result) {
15+
if ($result->columns !== null) {
16+
echo implode("\t", $result->columns) . PHP_EOL;
17+
foreach ($result->rows as $row) {
18+
echo implode("\t", $row) . PHP_EOL;
19+
}
20+
} else {
21+
echo "changed\tid". PHP_EOL;
22+
echo $result->changed . "\t" . $result->insertId . PHP_EOL;
23+
}
24+
}, function (Exception $e) {
25+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
26+
});
27+
28+
$db->quit();

tests/FunctionalExampleTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Clue\Tests\React\SQLite;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class FunctionalExampleTest extends TestCase
8+
{
9+
public function testQueryExampleReturnsDefaultValue()
10+
{
11+
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' query.php');
12+
13+
$this->assertEquals('value' . PHP_EOL . '42' . PHP_EOL, $output);
14+
}
15+
16+
public function testQueryExampleReturnsCalculatedValueFromPlaceholderVariables()
17+
{
18+
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' query.php "SELECT ?+? AS result" 1 2');
19+
20+
$this->assertEquals('result' . PHP_EOL . '3' . PHP_EOL, $output);
21+
}
22+
23+
public function testQueryExampleExecutedWithCgiReturnsDefaultValueAfterContentTypeHeader()
24+
{
25+
$code = 1;
26+
$null = DIRECTORY_SEPARATOR === '\\' ? 'NUL' : '/dev/null';
27+
system("php-cgi --version >$null 2>$null", $code);
28+
if ($code !== 0) {
29+
$this->markTestSkipped('Unable to execute "php-cgi"');
30+
}
31+
32+
$output = $this->execExample('php-cgi query.php');
33+
34+
$this->assertStringEndsWith("\r\n\r\n" . 'value' . PHP_EOL . '42' . PHP_EOL, $output);
35+
}
36+
37+
private function execExample($command)
38+
{
39+
chdir(__DIR__ . '/../examples/');
40+
41+
return shell_exec($command);
42+
}
43+
}

0 commit comments

Comments
 (0)