Misc fixes

* Updated build scripts to handle gitless environments a little
  better
* PDO shell plugin improvements
* More tests
This commit is contained in:
Chris 2021-12-17 12:51:29 +01:00
parent 1eab339347
commit 0f45219747
10 changed files with 165 additions and 8 deletions

View File

@ -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

View File

@ -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 "<?php define(\"APP_VERSION\", \"$VERSION\");" > src/version

View File

@ -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.

View File

@ -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("<comment>#{$index}</> <options=bold>{$log['request']['info']['query']}</> (<info>{$log['method']}</>) ".strlen($log['response']['body'])."b");
}
}

View File

@ -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);

View File

@ -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(" <comment>%s</><info>%s</>", ($name==$this->resource?"*":" "), $name)
);
}
return;
}
$res = get_resource($name);
if ($res instanceof PdoResource) {
$this->db = $res;

View File

@ -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, ' ')) {

View File

@ -0,0 +1,42 @@
<?php
namespace Spark\Environment;
class EnvironmentTest extends \PhpUnit\Framework\TestCase
{
/**
* @covers Environment::createFromDirectory
* @covers Environment::getProjectDirectory
*/
public function testCreatingEnvironmentByTraversing()
{
$env = Environment::createFromDirectory(__DIR__, true);
$this->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());
}
}

View File

@ -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;
}

View File

@ -0,0 +1,27 @@
<?php
namespace Spark\Resource;
class ResourceManagerTest extends \PhpUnit\Framework\TestCase
{
/**
* @covers ResourceManager::registerResourceType
*/
public function testRegisteringResourceTypes()
{
$manager = new ResourceManager();
$manager->registerResourceType('foo', FooType::class);
$types = $manager->getAllResourceTypes();
$this->assertEquals( ['foo'=>FooType::class], $types );
}
}
class FooType extends ResourceType
{
public function info()
{
}
}