From 4ed056cce4182e565dd61a14238dbecac8464d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 10 Jan 2022 14:04:50 +0100 Subject: [PATCH 1/3] Test installing as dependency --- .github/workflows/ci.yml | 1 + phpunit.xml.dist | 1 + phpunit.xml.legacy | 1 + tests/install-as-dep/.gitignore | 2 ++ tests/install-as-dep/composer.json | 11 +++++++++++ tests/install-as-dep/query.php | 26 ++++++++++++++++++++++++++ 6 files changed, 42 insertions(+) create mode 100644 tests/install-as-dep/.gitignore create mode 100644 tests/install-as-dep/composer.json create mode 100644 tests/install-as-dep/query.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 44baeaf..20607d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,3 +35,4 @@ jobs: if: ${{ matrix.php >= 7.3 }} - run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy if: ${{ matrix.php < 7.3 }} + - run: cd tests/install-as-dep && composer install && php query.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8c38d9a..1b8eb6a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,6 +9,7 @@ ./tests/ + ./tests/install-as-dep/ diff --git a/phpunit.xml.legacy b/phpunit.xml.legacy index e65d4cb..080247b 100644 --- a/phpunit.xml.legacy +++ b/phpunit.xml.legacy @@ -8,6 +8,7 @@ ./tests/ + ./tests/install-as-dep/ diff --git a/tests/install-as-dep/.gitignore b/tests/install-as-dep/.gitignore new file mode 100644 index 0000000..c8153b5 --- /dev/null +++ b/tests/install-as-dep/.gitignore @@ -0,0 +1,2 @@ +/composer.lock +/vendor/ diff --git a/tests/install-as-dep/composer.json b/tests/install-as-dep/composer.json new file mode 100644 index 0000000..d8b828e --- /dev/null +++ b/tests/install-as-dep/composer.json @@ -0,0 +1,11 @@ +{ + "require": { + "clue/reactphp-sqlite": "*@dev" + }, + "repositories": [ + { + "type": "path", + "url": "../../" + } + ] +} diff --git a/tests/install-as-dep/query.php b/tests/install-as-dep/query.php new file mode 100644 index 0000000..d2ab547 --- /dev/null +++ b/tests/install-as-dep/query.php @@ -0,0 +1,26 @@ +openLazy(':memory:', null, ['idle' => 0.001]); + +$query = isset($argv[1]) ? $argv[1] : 'SELECT 42 AS value'; +$args = array_slice(isset($argv) ? $argv : [], 2); + +$db->query('SELECT ? AS answer', [42])->then(function (Clue\React\SQLite\Result $result) { + if ($result->columns !== ['answer'] || count($result->rows) !== 1 || $result->rows[0]['answer'] !== 42) { + var_dump($result); + throw new RuntimeException('Unexpected result'); + } + + $answer = $result->rows[0]['answer']; + echo 'Answer: ' . $answer . PHP_EOL; +})->then(null, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; + exit(1); +}); From 1b3a564ccc4f97012beb5c2810e69e6d607a44a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sat, 1 Jan 2022 21:46:06 +0100 Subject: [PATCH 2/3] Support running from PHAR --- .github/workflows/ci.yml | 1 + src/Factory.php | 26 ++++++++++++++++++++++---- tests/install-as-dep/.gitignore | 1 + tests/install-as-dep/composer.json | 11 ++++++++++- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 20607d9..842b6e4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,3 +36,4 @@ jobs: - run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy if: ${{ matrix.php < 7.3 }} - run: cd tests/install-as-dep && composer install && php query.php + - run: cd tests/install-as-dep && php -d phar.readonly=0 vendor/bin/phar-composer build . query.phar && php query.phar diff --git a/src/Factory.php b/src/Factory.php index 98a4f28..7e7f6ca 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -226,7 +226,16 @@ public function openLazy($filename, $flags = null, array $options = []) private function openProcessIo($filename, $flags = null) { - $command = 'exec ' . \escapeshellarg($this->bin) . ' sqlite-worker.php'; + $cwd = null; + $worker = \dirname(__DIR__) . '/res/sqlite-worker.php'; + + if (\class_exists('Phar', false) && \Phar::running(false) !== '') { + $worker = '-r' . 'require(' . \var_export($worker, true) . ');'; // @codeCoverageIgnore + } else { + $cwd = __DIR__ . '/../res'; + $worker = \basename($worker); + } + $command = 'exec ' . \escapeshellarg($this->bin) . ' ' . escapeshellarg($worker); // Try to get list of all open FDs (Linux/Mac and others) $fds = @\scandir('/dev/fd'); @@ -269,7 +278,7 @@ private function openProcessIo($filename, $flags = null) $command = 'exec bash -c ' . \escapeshellarg($command); } - $process = new Process($command, __DIR__ . '/../res', null, $pipes); + $process = new Process($command, $cwd, null, $pipes); $process->start($this->loop); $db = new ProcessIoDatabase($process); @@ -285,7 +294,16 @@ private function openProcessIo($filename, $flags = null) private function openSocketIo($filename, $flags = null) { - $command = \escapeshellarg($this->bin) . ' sqlite-worker.php'; + $cwd = null; + $worker = \dirname(__DIR__) . '/res/sqlite-worker.php'; + + if (\class_exists('Phar', false) && \Phar::running(false) !== '') { + $worker = '-r' . 'require(' . \var_export($worker, true) . ');'; // @codeCoverageIgnore + } else { + $cwd = __DIR__ . '/../res'; + $worker = \basename($worker); + } + $command = \escapeshellarg($this->bin) . ' ' . escapeshellarg($worker); // launch process without default STDIO pipes, but inherit STDERR $null = \DIRECTORY_SEPARATOR === '\\' ? 'nul' : '/dev/null'; @@ -307,7 +325,7 @@ private function openSocketIo($filename, $flags = null) stream_set_blocking($server, false); $command .= ' ' . stream_socket_get_name($server, false); - $process = new Process($command, __DIR__ . '/../res', null, $pipes); + $process = new Process($command, $cwd, null, $pipes); $process->start($this->loop); $deferred = new Deferred(function () use ($process, $server) { diff --git a/tests/install-as-dep/.gitignore b/tests/install-as-dep/.gitignore index c8153b5..070e2f2 100644 --- a/tests/install-as-dep/.gitignore +++ b/tests/install-as-dep/.gitignore @@ -1,2 +1,3 @@ /composer.lock +/query.phar /vendor/ diff --git a/tests/install-as-dep/composer.json b/tests/install-as-dep/composer.json index d8b828e..ff46121 100644 --- a/tests/install-as-dep/composer.json +++ b/tests/install-as-dep/composer.json @@ -2,10 +2,19 @@ "require": { "clue/reactphp-sqlite": "*@dev" }, + "require-dev": { + "clue/phar-composer": "^1.0" + }, + "bin": [ + "query.php" + ], "repositories": [ { "type": "path", - "url": "../../" + "url": "../../", + "options": { + "symlink": false + } } ] } From c0da996b86429f241dec4b95aea8a774c051e388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 11 Jan 2022 07:14:25 +0100 Subject: [PATCH 3/3] Continue test suite when building PHAR fails on Windows --- .github/workflows/ci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 842b6e4..53c3223 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,4 +36,8 @@ jobs: - run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy if: ${{ matrix.php < 7.3 }} - run: cd tests/install-as-dep && composer install && php query.php - - run: cd tests/install-as-dep && php -d phar.readonly=0 vendor/bin/phar-composer build . query.phar && php query.phar + - run: cd tests/install-as-dep && php -d phar.readonly=0 vendor/bin/phar-composer build . query.phar + continue-on-error: ${{ matrix.os == 'windows-2019' }} + id: phar + - run: php tests/install-as-dep/query.phar + if: ${{ steps.phar.outcome == 'success' }}