Misc fixes
* Updated build scripts to handle gitless environments a little better * PDO shell plugin improvements * More tests
This commit is contained in:
		| @@ -5,11 +5,13 @@ if [ ! -f spark.phar ]; then | |||||||
|     exit 1 |     exit 1 | ||||||
| fi | fi | ||||||
|  |  | ||||||
| VERSION="$(git describe --tags)" | if [ -z "$VERSION" ]; then | ||||||
| PATH="$PWD/tools:$PATH" |     VERSION="$(git describe --tags)" | ||||||
|  |     PATH="$PWD/tools:$PATH" | ||||||
|  | fi | ||||||
|  |  | ||||||
| if [ -z "$VERSION" ]; then | 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 |     exit 1 | ||||||
| fi | fi | ||||||
|  |  | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ VERSION="$(git describe --tags)" | |||||||
|  |  | ||||||
| if [ -z "$VERSION" ]; then | if [ -z "$VERSION" ]; then | ||||||
|     echo "Could not parse version from git" |     echo "Could not parse version from git" | ||||||
|     exit 1 |     exit 0 | ||||||
| fi | fi | ||||||
|  |  | ||||||
| echo -e "<?php define(\"APP_VERSION\", \"$VERSION\");" > src/version | echo -e "<?php define(\"APP_VERSION\", \"$VERSION\");" > src/version | ||||||
|   | |||||||
| @@ -11,3 +11,5 @@ | |||||||
| - Rewritten script runner with proper variable substitution. | - Rewritten script runner with proper variable substitution. | ||||||
| - Added utility libraries for HTTP requests (Guzzle), templating (Twig) and dotenv | - Added utility libraries for HTTP requests (Guzzle), templating (Twig) and dotenv | ||||||
|   support (symfony/dotenv, activates with environment). |   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. | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ use Spark\Commands\Command; | |||||||
| use SparkPlug; | use SparkPlug; | ||||||
| use SparkPlug\Com\Noccy\ApiClient\Api\Method; | use SparkPlug\Com\Noccy\ApiClient\Api\Method; | ||||||
| use SparkPlug\Com\Noccy\ApiClient\ApiClientPlugin; | use SparkPlug\Com\Noccy\ApiClient\ApiClientPlugin; | ||||||
|  | use SparkPlug\Com\Noccy\ApiClient\Log\RequestData; | ||||||
| use SparkPlug\Com\Noccy\ApiClient\Request\RequestBuilder; | use SparkPlug\Com\Noccy\ApiClient\Request\RequestBuilder; | ||||||
| use Symfony\Component\Console\Helper\Table; | use Symfony\Component\Console\Helper\Table; | ||||||
| use Symfony\Component\Console\Input\InputArgument; | use Symfony\Component\Console\Input\InputArgument; | ||||||
| @@ -29,7 +30,17 @@ class ApiLogsCommand extends Command | |||||||
|         /** @var ApiClientPlugin */ |         /** @var ApiClientPlugin */ | ||||||
|         $plugin = get_plugin('com.noccy.apiclient'); |         $plugin = get_plugin('com.noccy.apiclient'); | ||||||
|  |  | ||||||
|  |         $iter = $plugin->getLogIterator("default"); | ||||||
|  |  | ||||||
|  |         foreach ($iter as $index=>$log) { | ||||||
|  |             $this->dumpLog($log, $index, $output); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         return Command::SUCCESS; |         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"); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ namespace SparkPlug\Com\Noccy\ApiClient; | |||||||
|  |  | ||||||
| use SparkPlug; | use SparkPlug; | ||||||
| use SparkPlug\Com\Noccy\ApiClient\Log\RequestLog; | use SparkPlug\Com\Noccy\ApiClient\Log\RequestLog; | ||||||
|  | use SparkPlug\Com\Noccy\ApiClient\Log\LogIterator; | ||||||
|  |  | ||||||
| class ApiClientPlugin extends SparkPlug | class ApiClientPlugin extends SparkPlug | ||||||
| { | { | ||||||
| @@ -113,6 +114,14 @@ class ApiClientPlugin extends SparkPlug | |||||||
|         $log = new RequestLog($logsDir.$name.".json"); |         $log = new RequestLog($logsDir.$name.".json"); | ||||||
|         return $log; |         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); | register_plugin("com.noccy.apiclient", new ApiClientPlugin); | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ namespace SparkPlug\Com\Noccy\Pdo\Shell\Shell; | |||||||
|  |  | ||||||
| use SparkPlug\Com\Noccy\Pdo\PdoResource; | use SparkPlug\Com\Noccy\Pdo\PdoResource; | ||||||
| use Spark\Commands\Command; | use Spark\Commands\Command; | ||||||
|  | use Spark\SparkApplication; | ||||||
| use Symfony\Component\Console\Helper\Table; | use Symfony\Component\Console\Helper\Table; | ||||||
| use Symfony\Component\Console\Helper\TableSeparator; | use Symfony\Component\Console\Helper\TableSeparator; | ||||||
| use Symfony\Component\Console\Input\InputArgument; | use Symfony\Component\Console\Input\InputArgument; | ||||||
| @@ -178,6 +179,9 @@ class PdoShell { | |||||||
|             $query = $json['query']??'?'; |             $query = $json['query']??'?'; | ||||||
|             $res = $json['result']??[]; |             $res = $json['result']??[]; | ||||||
|         } else { |         } else { | ||||||
|  |             if (!$this->lastQuery) { | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|             $query = $this->lastQuery['query']; |             $query = $this->lastQuery['query']; | ||||||
|             $res = $this->lastQuery['result']; |             $res = $this->lastQuery['result']; | ||||||
|         } |         } | ||||||
| @@ -223,6 +227,25 @@ class PdoShell { | |||||||
|     private function doSelectCommand(array $args) |     private function doSelectCommand(array $args) | ||||||
|     { |     { | ||||||
|         $name = array_shift($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); |         $res = get_resource($name); | ||||||
|         if ($res instanceof PdoResource) { |         if ($res instanceof PdoResource) { | ||||||
|             $this->db = $res; |             $this->db = $res; | ||||||
|   | |||||||
| @@ -10,6 +10,13 @@ class ScriptRunner | |||||||
|  |  | ||||||
|     private ?string $directory = null; |     private ?string $directory = null; | ||||||
|  |  | ||||||
|  |     private bool $quiet; | ||||||
|  |  | ||||||
|  |     public function __construct(bool $quiet=false) | ||||||
|  |     { | ||||||
|  |         $this->quiet = $quiet; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     public function setDirectory(?string $directory) |     public function setDirectory(?string $directory) | ||||||
|     { |     { | ||||||
|         $this->directory = $directory; |         $this->directory = $directory; | ||||||
| @@ -22,6 +29,10 @@ class ScriptRunner | |||||||
|  |  | ||||||
|     public function evaluateDefinedScript(string $name) |     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]; |         $script = $this->scripts[$name]; | ||||||
|         $this->evaluate($script); |         $this->evaluate($script); | ||||||
|     } |     } | ||||||
| @@ -44,10 +55,12 @@ class ScriptRunner | |||||||
|             $subscript = $this->scripts[$subname]; |             $subscript = $this->scripts[$subname]; | ||||||
|             $this->evaluate($subscript); |             $this->evaluate($subscript); | ||||||
|         } else { |         } else { | ||||||
|             if (posix_isatty(STDOUT)) { |             if (!$this->quiet) { | ||||||
|                 printf("\e[0;33m> \e[0;93m%s\e[0m\n", $script); |                 if (posix_isatty(STDOUT)) { | ||||||
|             } else { |                     printf("\e[0;33m> \e[0;93m%s\e[0m\n", $script); | ||||||
|                 printf("> %s\n", $script); |                 } else { | ||||||
|  |                     printf("> %s\n", $script); | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|      |      | ||||||
|             if (str_contains($script, ' ')) { |             if (str_contains($script, ' ')) { | ||||||
|   | |||||||
							
								
								
									
										42
									
								
								tests/Environment/EnvironmentTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								tests/Environment/EnvironmentTest.php
									
									
									
									
									
										Normal 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()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -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; | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										27
									
								
								tests/Resource/ResourceManagerTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								tests/Resource/ResourceManagerTest.php
									
									
									
									
									
										Normal 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() | ||||||
|  |     { | ||||||
|  |  | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user