From 0f452197478f0cb7eedb571686246129422faee5 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 17 Dec 2021 12:51:29 +0100 Subject: [PATCH] Misc fixes * Updated build scripts to handle gitless environments a little better * PDO shell plugin improvements * More tests --- .spark/build/package.sh | 8 ++-- .spark/build/update-version.sh | 2 +- VERSIONS.md | 2 + .../Commands/ApiLogsCommand.php | 11 +++++ plugins/com.noccy.apiclient/sparkplug.php | 9 ++++ .../com.noccy.pdo.shell/Shell/PdoShell.php | 23 ++++++++++ src/Environment/ScriptRunner.php | 21 ++++++++-- tests/Environment/EnvironmentTest.php | 42 +++++++++++++++++++ tests/Environment/ScriptRunnerTest.php | 28 +++++++++++++ tests/Resource/ResourceManagerTest.php | 27 ++++++++++++ 10 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 tests/Environment/EnvironmentTest.php create mode 100644 tests/Resource/ResourceManagerTest.php diff --git a/.spark/build/package.sh b/.spark/build/package.sh index e3f1ed0..2b98805 100755 --- a/.spark/build/package.sh +++ b/.spark/build/package.sh @@ -5,11 +5,13 @@ if [ ! -f spark.phar ]; then exit 1 fi -VERSION="$(git describe --tags)" -PATH="$PWD/tools:$PATH" +if [ -z "$VERSION" ]; then + VERSION="$(git describe --tags)" + PATH="$PWD/tools:$PATH" +fi if [ -z "$VERSION" ]; then - echo "Could not parse version from git" + echo "Could not parse version from git. Export VERSION if you are building manually." exit 1 fi diff --git a/.spark/build/update-version.sh b/.spark/build/update-version.sh index 99197a8..4950f60 100755 --- a/.spark/build/update-version.sh +++ b/.spark/build/update-version.sh @@ -12,7 +12,7 @@ VERSION="$(git describe --tags)" if [ -z "$VERSION" ]; then echo "Could not parse version from git" - exit 1 + exit 0 fi echo -e " src/version diff --git a/VERSIONS.md b/VERSIONS.md index c816f86..de49b9b 100644 --- a/VERSIONS.md +++ b/VERSIONS.md @@ -11,3 +11,5 @@ - Rewritten script runner with proper variable substitution. - Added utility libraries for HTTP requests (Guzzle), templating (Twig) and dotenv support (symfony/dotenv, activates with environment). +- Plugins in beta: com.noccy.pdo, com.noccy.pdo.shell, com.noccy.watcher. +- Plugins in alpha: com.noccy.apiclient. diff --git a/plugins/com.noccy.apiclient/Commands/ApiLogsCommand.php b/plugins/com.noccy.apiclient/Commands/ApiLogsCommand.php index 72c9bd3..0a3d341 100644 --- a/plugins/com.noccy.apiclient/Commands/ApiLogsCommand.php +++ b/plugins/com.noccy.apiclient/Commands/ApiLogsCommand.php @@ -6,6 +6,7 @@ use Spark\Commands\Command; use SparkPlug; use SparkPlug\Com\Noccy\ApiClient\Api\Method; use SparkPlug\Com\Noccy\ApiClient\ApiClientPlugin; +use SparkPlug\Com\Noccy\ApiClient\Log\RequestData; use SparkPlug\Com\Noccy\ApiClient\Request\RequestBuilder; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputArgument; @@ -29,7 +30,17 @@ class ApiLogsCommand extends Command /** @var ApiClientPlugin */ $plugin = get_plugin('com.noccy.apiclient'); + $iter = $plugin->getLogIterator("default"); + + foreach ($iter as $index=>$log) { + $this->dumpLog($log, $index, $output); + } return Command::SUCCESS; } + + private function dumpLog(array $log, int $index, OutputInterface $output) + { + $output->writeln("#{$index} {$log['request']['info']['query']} ({$log['method']}) ".strlen($log['response']['body'])."b"); + } } diff --git a/plugins/com.noccy.apiclient/sparkplug.php b/plugins/com.noccy.apiclient/sparkplug.php index 592fcec..c32aeb4 100644 --- a/plugins/com.noccy.apiclient/sparkplug.php +++ b/plugins/com.noccy.apiclient/sparkplug.php @@ -4,6 +4,7 @@ namespace SparkPlug\Com\Noccy\ApiClient; use SparkPlug; use SparkPlug\Com\Noccy\ApiClient\Log\RequestLog; +use SparkPlug\Com\Noccy\ApiClient\Log\LogIterator; class ApiClientPlugin extends SparkPlug { @@ -113,6 +114,14 @@ class ApiClientPlugin extends SparkPlug $log = new RequestLog($logsDir.$name.".json"); return $log; } + + public function getLogIterator(string $name): LogIterator + { + $env = get_environment(); + $logsDir = $env->getConfigDirectory() . "/api/logs/"; + $iter = new LogIterator($logsDir.$name.".json"); + return $iter; + } } register_plugin("com.noccy.apiclient", new ApiClientPlugin); diff --git a/plugins/com.noccy.pdo.shell/Shell/PdoShell.php b/plugins/com.noccy.pdo.shell/Shell/PdoShell.php index 45206ca..7774ee8 100644 --- a/plugins/com.noccy.pdo.shell/Shell/PdoShell.php +++ b/plugins/com.noccy.pdo.shell/Shell/PdoShell.php @@ -4,6 +4,7 @@ namespace SparkPlug\Com\Noccy\Pdo\Shell\Shell; use SparkPlug\Com\Noccy\Pdo\PdoResource; use Spark\Commands\Command; +use Spark\SparkApplication; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Input\InputArgument; @@ -178,6 +179,9 @@ class PdoShell { $query = $json['query']??'?'; $res = $json['result']??[]; } else { + if (!$this->lastQuery) { + return; + } $query = $this->lastQuery['query']; $res = $this->lastQuery['result']; } @@ -223,6 +227,25 @@ class PdoShell { private function doSelectCommand(array $args) { $name = array_shift($args); + + if (!$name) { + $app = SparkApplication::$instance; + $resources = $app->getResourceManager(); + + $named = $resources->getAllNamedResources(); + foreach ($named as $name=>$resource) { + if (!($resource instanceof PdoResource)) { + continue; + } + $this->output->writeln( + sprintf(" %s%s", ($name==$this->resource?"*":" "), $name) + ); + } + return; + + } + + $res = get_resource($name); if ($res instanceof PdoResource) { $this->db = $res; diff --git a/src/Environment/ScriptRunner.php b/src/Environment/ScriptRunner.php index 0b6ce2e..5ef5065 100644 --- a/src/Environment/ScriptRunner.php +++ b/src/Environment/ScriptRunner.php @@ -10,6 +10,13 @@ class ScriptRunner private ?string $directory = null; + private bool $quiet; + + public function __construct(bool $quiet=false) + { + $this->quiet = $quiet; + } + public function setDirectory(?string $directory) { $this->directory = $directory; @@ -22,6 +29,10 @@ class ScriptRunner public function evaluateDefinedScript(string $name) { + if (!array_key_exists($name, $this->scripts)) { + printf("error: The script {$name} is not defined\n"); + exit(1); + } $script = $this->scripts[$name]; $this->evaluate($script); } @@ -44,10 +55,12 @@ class ScriptRunner $subscript = $this->scripts[$subname]; $this->evaluate($subscript); } else { - if (posix_isatty(STDOUT)) { - printf("\e[0;33m> \e[0;93m%s\e[0m\n", $script); - } else { - printf("> %s\n", $script); + if (!$this->quiet) { + if (posix_isatty(STDOUT)) { + printf("\e[0;33m> \e[0;93m%s\e[0m\n", $script); + } else { + printf("> %s\n", $script); + } } if (str_contains($script, ' ')) { diff --git a/tests/Environment/EnvironmentTest.php b/tests/Environment/EnvironmentTest.php new file mode 100644 index 0000000..f659027 --- /dev/null +++ b/tests/Environment/EnvironmentTest.php @@ -0,0 +1,42 @@ +assertInstanceOf(Environment::class, $env); + $this->assertEquals(dirname(__DIR__), $env->getProjectDirectory()); + } + + /** + * @covers Environment::createFromDirectory + */ + public function testFailingToCreatingEnvironmentFromMissingConfig() + { + $env = Environment::createFromDirectory(__DIR__); + + $this->assertNull($env); + } + + /** + * @covers Environment::createFromDirectory + * @covers Environment::getProjectDirectory + */ + public function testCreatingEnvironmentDirectly() + { + $env = Environment::createFromDirectory(__DIR__."/.."); + + $this->assertInstanceOf(Environment::class, $env); + $this->assertEquals(dirname(__DIR__), $env->getProjectDirectory()); + } + +} \ No newline at end of file diff --git a/tests/Environment/ScriptRunnerTest.php b/tests/Environment/ScriptRunnerTest.php index 1bb95ff..fe3a4a5 100644 --- a/tests/Environment/ScriptRunnerTest.php +++ b/tests/Environment/ScriptRunnerTest.php @@ -27,4 +27,32 @@ class ScriptRunnerTest extends \PhpUnit\Framework\TestCase ]; } + /** + * @covers ScriptRunner::defineScript + * @covers ScriptRunner::evalutateDefinedScript + */ + public function testDefiningAndCallingScript() + { + $runner = new ScriptRunner(true); + $runner->defineScript('test1', __CLASS__."::call1"); + $runner->defineScript('test2', [ __CLASS__."::call2" ]); + + $this->assertFalse(self::$call1); + $runner->evaluateDefinedScript('test1'); + $this->assertTrue(self::$call1); + + $this->assertFalse(self::$call2); + $runner->evaluateDefinedScript('test2'); + $this->assertTrue(self::$call2); + } + + public static function call1() + { self::$call1 = true; } + + public static function call2() + { self::$call2 = true; } + + private static bool $call1 = false; + private static bool $call2 = false; + } diff --git a/tests/Resource/ResourceManagerTest.php b/tests/Resource/ResourceManagerTest.php new file mode 100644 index 0000000..6a7ed7b --- /dev/null +++ b/tests/Resource/ResourceManagerTest.php @@ -0,0 +1,27 @@ +registerResourceType('foo', FooType::class); + + $types = $manager->getAllResourceTypes(); + $this->assertEquals( ['foo'=>FooType::class], $types ); + } +} + +class FooType extends ResourceType +{ + public function info() + { + + } +} \ No newline at end of file