From 65b252c393ffedb0ff7628b5ed61a66db5ce11a5 Mon Sep 17 00:00:00 2001 From: Joao M Date: Wed, 24 May 2023 14:51:51 -0500 Subject: [PATCH 01/24] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..189d6a4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Joao Gilberto Magalhaes + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 435415df7997c4b72b78d131a6bb9f7221779937 Mon Sep 17 00:00:00 2001 From: Joao M Date: Wed, 24 May 2023 14:53:46 -0500 Subject: [PATCH 02/24] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f8d6bcf..599b389 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Boilerplate Project Template for RESTFul API -[![Build Status](https://github.com/byjg/php-rest-template/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-rest-template/actions/workflows/phpunit.yml) +[![Build Status](https://github.com/byjg/php-rest-template/actions/workflows/build-app-image.yml/badge.svg?branch=master)](https://github.com/byjg/php-rest-template/actions/workflows/build-app-image.yml) [![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com) [![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-rest-template/) [![GitHub license](https://img.shields.io/github/license/byjg/php-rest-template.svg)](https://opensource.byjg.com/opensource/licensing.html) From 59d8e94a7899a221ba4d4e3a3cb1ecb40b439107 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Fri, 2 Jun 2023 20:17:29 -0500 Subject: [PATCH 03/24] Add teardown for tests --- templates/codegen/test.php.jinja | 5 ----- tests/Functional/Rest/BaseApiTestCase.php | 13 +++++++++++++ tests/Functional/Rest/DummyHexTest.php | 5 ----- tests/Functional/Rest/DummyTest.php | 5 ----- tests/Functional/Rest/LoginTest.php | 7 ++----- tests/Functional/Rest/SampleProtectedTest.php | 5 ----- tests/Functional/Rest/SampleTest.php | 5 ----- 7 files changed, 15 insertions(+), 30 deletions(-) diff --git a/templates/codegen/test.php.jinja b/templates/codegen/test.php.jinja index 9a8b769..639968d 100644 --- a/templates/codegen/test.php.jinja +++ b/templates/codegen/test.php.jinja @@ -11,13 +11,8 @@ use {{ namespace }}\Repository\BaseRepository; class {{ className }}Test extends BaseApiTestCase { - protected $filePath = __DIR__ . '/../../../public/docs/openapi.json'; - protected function setUp(): void { - $schema = Schema::getInstance(file_get_contents($this->filePath)); - $this->setSchema($schema); - parent::setUp(); } diff --git a/tests/Functional/Rest/BaseApiTestCase.php b/tests/Functional/Rest/BaseApiTestCase.php index 2c69437..0f6f12d 100644 --- a/tests/Functional/Rest/BaseApiTestCase.php +++ b/tests/Functional/Rest/BaseApiTestCase.php @@ -3,6 +3,7 @@ namespace Test\Functional\Rest; +use ByJG\ApiTools\Base\Schema; use ByJG\DbMigration\Database\MySqlDatabase; use ByJG\DbMigration\Migration; use RestTemplate\Psr11; @@ -13,6 +14,18 @@ class BaseApiTestCase extends \ByJG\ApiTools\ApiTestCase { protected static $databaseReset = false; + protected $filePath = __DIR__ . '/../../../public/docs/openapi.json'; + + protected function setUp(): void + { + $this->setSchema(Schema::getInstance(file_get_contents($this->filePath))); + } + + protected function tearDown(): void + { + $this->setSchema(null); + } + public function getPsr7Request() { $uri = Uri::getInstanceFromString() diff --git a/tests/Functional/Rest/DummyHexTest.php b/tests/Functional/Rest/DummyHexTest.php index de500ad..8b3e7d7 100644 --- a/tests/Functional/Rest/DummyHexTest.php +++ b/tests/Functional/Rest/DummyHexTest.php @@ -11,13 +11,8 @@ class DummyHexTest extends BaseApiTestCase { - protected $filePath = __DIR__ . '/../../../public/docs/openapi.json'; - protected function setUp(): void { - $schema = Schema::getInstance(file_get_contents($this->filePath)); - $this->setSchema($schema); - parent::setUp(); } diff --git a/tests/Functional/Rest/DummyTest.php b/tests/Functional/Rest/DummyTest.php index 68ad97a..0acf026 100644 --- a/tests/Functional/Rest/DummyTest.php +++ b/tests/Functional/Rest/DummyTest.php @@ -11,13 +11,8 @@ class DummyTest extends BaseApiTestCase { - protected $filePath = __DIR__ . '/../../../public/docs/openapi.json'; - protected function setUp(): void { - $schema = Schema::getInstance(file_get_contents($this->filePath)); - $this->setSchema($schema); - parent::setUp(); } diff --git a/tests/Functional/Rest/LoginTest.php b/tests/Functional/Rest/LoginTest.php index 7eddd8f..7018ac3 100644 --- a/tests/Functional/Rest/LoginTest.php +++ b/tests/Functional/Rest/LoginTest.php @@ -13,12 +13,9 @@ */ class LoginTest extends BaseApiTestCase { - protected $filePath = __DIR__ . '/../../../public/docs/openapi.json'; - - public function setUp(): void + protected function setUp(): void { - $schema = Schema::getInstance(file_get_contents($this->filePath)); - $this->setSchema($schema); + parent::setUp(); } public function testLoginOk() diff --git a/tests/Functional/Rest/SampleProtectedTest.php b/tests/Functional/Rest/SampleProtectedTest.php index 76eae61..1fc5ead 100644 --- a/tests/Functional/Rest/SampleProtectedTest.php +++ b/tests/Functional/Rest/SampleProtectedTest.php @@ -10,13 +10,8 @@ */ class SampleProtectedTest extends BaseApiTestCase { - protected $filePath = __DIR__ . '/../../../public/docs/openapi.json'; - protected function setUp(): void { - $schema = Schema::getInstance(file_get_contents($this->filePath)); - $this->setSchema($schema); - parent::setUp(); } diff --git a/tests/Functional/Rest/SampleTest.php b/tests/Functional/Rest/SampleTest.php index e46afbb..c194b94 100644 --- a/tests/Functional/Rest/SampleTest.php +++ b/tests/Functional/Rest/SampleTest.php @@ -11,13 +11,8 @@ */ class SampleTest extends BaseApiTestCase { - protected $filePath = __DIR__ . '/../../../public/docs/openapi.json'; - protected function setUp(): void { - $schema = Schema::getInstance(file_get_contents($this->filePath)); - $this->setSchema($schema); - parent::setUp(); } From 547018cb821688de469c0e37053a4ad65f79f5d2 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Sun, 4 Jun 2023 21:23:43 -0500 Subject: [PATCH 04/24] Code formatting and fix php cs --- .idea/runConfigurations/down_local_dev.xml | 8 +++ .idea/runConfigurations/openapi.xml | 8 +++ .idea/runConfigurations/up_local_dev.xml | 8 +++ builder/Scripts.php | 47 ++++++------- composer.json | 2 + public/docs/index.html | 5 +- public/docs/openapi.js | 7 +- src/Repository/DummyRepository.php | 6 +- src/Rest/Login.php | 68 ++++++++----------- src/Util/HexUuidLiteral.php | 3 +- templates/codegen/test.php.jinja | 16 ++--- tests/Functional/Rest/BaseApiTestCase.php | 8 ++- tests/Functional/Rest/DummyHexTest.php | 23 +++---- tests/Functional/Rest/DummyTest.php | 23 +++---- tests/Functional/Rest/LoginTest.php | 12 ++-- tests/Functional/Rest/SampleProtectedTest.php | 11 ++- tests/Functional/Rest/SampleTest.php | 5 -- 17 files changed, 127 insertions(+), 133 deletions(-) create mode 100644 .idea/runConfigurations/down_local_dev.xml create mode 100644 .idea/runConfigurations/openapi.xml create mode 100644 .idea/runConfigurations/up_local_dev.xml diff --git a/.idea/runConfigurations/down_local_dev.xml b/.idea/runConfigurations/down_local_dev.xml new file mode 100644 index 0000000..2517792 --- /dev/null +++ b/.idea/runConfigurations/down_local_dev.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/openapi.xml b/.idea/runConfigurations/openapi.xml new file mode 100644 index 0000000..5d7c032 --- /dev/null +++ b/.idea/runConfigurations/openapi.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/up_local_dev.xml b/.idea/runConfigurations/up_local_dev.xml new file mode 100644 index 0000000..ee245d6 --- /dev/null +++ b/.idea/runConfigurations/up_local_dev.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/builder/Scripts.php b/builder/Scripts.php index e23f407..013fb06 100755 --- a/builder/Scripts.php +++ b/builder/Scripts.php @@ -4,6 +4,7 @@ use ByJG\AnyDataset\Db\DbDriverInterface; use ByJG\Config\Exception\ConfigNotFoundException; +use ByJG\Config\Exception\DependencyInjectionException; use ByJG\Config\Exception\EnvironmentException; use ByJG\Config\Exception\KeyNotFoundException; use ByJG\DbMigration\Database\MySqlDatabase; @@ -12,10 +13,11 @@ use ByJG\JinjaPhp\Loader\FileSystemLoader; use ByJG\Util\Uri; use Composer\Script\Event; -use RestTemplate\Psr11; use Exception; +use OpenApi\Generator; use Psr\SimpleCache\InvalidArgumentException; use ReflectionException; +use RestTemplate\Psr11; class Scripts extends BaseScripts { @@ -26,12 +28,13 @@ public function __construct() /** * @param Event $event + * @return void * @throws ConfigNotFoundException - * @throws EnvironmentException * @throws InvalidArgumentException * @throws InvalidMigrationFile * @throws KeyNotFoundException * @throws ReflectionException + * @throws DependencyInjectionException */ public static function migrate(Event $event) { @@ -41,8 +44,8 @@ public static function migrate(Event $event) /** * @param Event $event + * @return void * @throws ConfigNotFoundException - * @throws EnvironmentException * @throws InvalidArgumentException * @throws KeyNotFoundException * @throws ReflectionException @@ -55,8 +58,8 @@ public static function genOpenApiDocs(Event $event) /** * @param Event $event + * @return void * @throws ConfigNotFoundException - * @throws EnvironmentException * @throws InvalidArgumentException * @throws KeyNotFoundException * @throws ReflectionException @@ -69,8 +72,9 @@ public static function codeGenerator(Event $event) /** * @param $arguments + * @return void * @throws ConfigNotFoundException - * @throws EnvironmentException + * @throws DependencyInjectionException * @throws InvalidArgumentException * @throws InvalidMigrationFile * @throws KeyNotFoundException @@ -116,11 +120,11 @@ public function runMigrate($arguments) } /** - * @param $arguments + * @param array $arguments * @param bool $hasCmd * @return array */ - protected function extractArguments($arguments, $hasCmd = true) { + protected function extractArguments($arguments, bool $hasCmd = true) { $ret = [ '--up-to' => null, '--yes' => null, @@ -130,51 +134,48 @@ protected function extractArguments($arguments, $hasCmd = true) { $start = 0; if ($hasCmd) { - $ret['command'] = isset($arguments[0]) ? $arguments[0] : null; + $ret['command'] = $arguments[0] ?? null; $start = 1; } for ($i=$start; $i < count($arguments); $i++) { $args = explode("=", $arguments[$i]); - $ret[$args[0]] = isset($args[1]) ? $args[1] : true; + $ret[$args[0]] = $args[1] ?? true; } return $ret; } /** - * @param $arguments - * @throws ConfigNotFoundException - * @throws EnvironmentException - * @throws InvalidArgumentException - * @throws KeyNotFoundException - * @throws ReflectionException + * @param array $arguments + * @return void */ public function runGenOpenApiDocs($arguments) { $docPath = $this->workdir . '/public/docs/'; - $generator = (new \OpenApi\Generator()) + $generator = (new Generator()) ->setConfig([ "operationId.hash" => false ]); $openapi = $generator->generate( [ $this->workdir . '/src', - ], - null, - true + ] ); file_put_contents("$docPath/openapi.json", $openapi->toJson()); } /** - * @param $arguments + * @param array $arguments + * @return void * @throws ConfigNotFoundException - * @throws EnvironmentException + * @throws DependencyInjectionException * @throws InvalidArgumentException * @throws KeyNotFoundException * @throws ReflectionException + * @throws \ByJG\Serializer\Exception\InvalidArgumentException + * @throws Exception */ public function runCodeGenerator($arguments) { @@ -182,7 +183,7 @@ public function runCodeGenerator($arguments) $table = null; if (in_array("--table", $arguments)) { $index = array_search("--table", $arguments); - $table = isset($arguments[$index + 1]) ? $arguments[$index + 1] : null; + $table = $arguments[$index + 1] ?? null; unset($arguments[$index + 1]); unset($arguments[$index]); } @@ -281,7 +282,7 @@ public function runCodeGenerator($arguments) } } - // Create an array with non nullable fields but primary keys + // Create an array with non-nullable fields but primary keys $nonNullableFields = []; foreach ($tableDefinition as $field) { if ($field['null'] == 'NO' && $field['key'] != 'PRI') { diff --git a/composer.json b/composer.json index 14f02fc..f3bd22f 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,8 @@ "migrate": "Builder\\Scripts::migrate", "codegen": "Builder\\Scripts::codeGenerator", "openapi": "Builder\\Scripts::genOpenApiDocs", + "up-local-dev": "docker-compose -f docker-compose-dev.yml up -d", + "down-local-dev": "docker-compose -f docker-compose-dev.yml down", "post-create-project-cmd": "Builder\\PostCreateScript::run" }, "autoload-dev": { diff --git a/public/docs/index.html b/public/docs/index.html index 26ac3bb..cd58ffe 100644 --- a/public/docs/index.html +++ b/public/docs/index.html @@ -1,6 +1,8 @@ - + + + OpenAPI Viewer