From 7ba0095d4d859d3968991e8dbb6606122a9488dc Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Thu, 19 Sep 2024 12:25:40 -0500 Subject: [PATCH] More compatibility adjustments --- builder/Scripts.php | 8 ++-- docker/Dockerfile | 3 +- phpunit.xml.dist | 41 ++++++++++--------- src/Model/User.php | 20 ++++----- src/OpenApiSpec.php | 19 ++++----- src/Psr11.php | 7 +++- src/Rest/Login.php | 30 ++++++-------- src/Util/JwtContext.php | 7 ++-- src/Util/OpenApiContext.php | 7 ++-- templates/codegen/test.php.jinja | 2 +- .../{Functional => }/Rest/BaseApiTestCase.php | 4 +- tests/{Functional => }/Rest/Credentials.php | 2 +- tests/{Functional => }/Rest/DummyHexTest.php | 2 +- tests/{Functional => }/Rest/DummyTest.php | 2 +- tests/{Functional => }/Rest/LoginTest.php | 2 +- .../Rest/SampleProtectedTest.php | 2 +- tests/{Functional => }/Rest/SampleTest.php | 2 +- 17 files changed, 77 insertions(+), 83 deletions(-) rename tests/{Functional => }/Rest/BaseApiTestCase.php (92%) rename tests/{Functional => }/Rest/Credentials.php (97%) rename tests/{Functional => }/Rest/DummyHexTest.php (99%) rename tests/{Functional => }/Rest/DummyTest.php (99%) rename tests/{Functional => }/Rest/LoginTest.php (99%) rename tests/{Functional => }/Rest/SampleProtectedTest.php (98%) rename tests/{Functional => }/Rest/SampleTest.php (93%) diff --git a/builder/Scripts.php b/builder/Scripts.php index de45432..f54d485 100755 --- a/builder/Scripts.php +++ b/builder/Scripts.php @@ -87,7 +87,7 @@ public static function codeGenerator(Event $event) * @throws ConfigException * @throws InvalidDateException */ - public function runMigrate($arguments) + public function runMigrate($arguments): void { $argumentList = $this->extractArguments($arguments); if (isset($argumentList["command"])) { @@ -160,7 +160,7 @@ protected function extractArguments(array $arguments, bool $hasCmd = true): arra * @param array $arguments * @return void */ - public function runGenOpenApiDocs(array $arguments) + public function runGenOpenApiDocs(array $arguments): void { $docPath = $this->workdir . '/public/docs/'; @@ -187,7 +187,7 @@ public function runGenOpenApiDocs(array $arguments) * @throws \ByJG\Serializer\Exception\InvalidArgumentException * @throws Exception */ - public function runCodeGenerator(array $arguments) + public function runCodeGenerator(array $arguments): void { // Get Table Name $table = null; @@ -379,7 +379,7 @@ public function runCodeGenerator(array $arguments) echo "Processing Test for table $table...\n"; $template = $loader->getTemplate('test.php'); if ($save) { - $file = __DIR__ . '/../tests/Functional/Rest/' . $data['className'] . 'Test.php'; + $file = __DIR__ . '/../tests/Rest/' . $data['className'] . 'Test.php'; file_put_contents($file, $template->render($data)); echo "File saved in $file\n"; } else { diff --git a/docker/Dockerfile b/docker/Dockerfile index a08a37f..77eac31 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -31,4 +31,5 @@ COPY templates /srv/templates COPY composer.* /srv COPY phpunit.xml.dist /srv COPY db /srv/db -RUN composer install --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader +RUN composer install --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader && \ + composer dump-autoload --optimize --classmap-authoritative -q diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 39d5297..ff5e401 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,33 +6,36 @@ and open the template in the editor. --> - + stopOnFailure="false" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> - - - ./src - - + + + ./src + + - - - ./tests/ - - + + + ./tests/Rest + ./tests/ + + - - - - + + + + - - - + + + diff --git a/src/Model/User.php b/src/Model/User.php index 5f119cf..74c90e5 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -2,11 +2,11 @@ namespace RestReferenceArchitecture\Model; -use ByJG\Authenticate\Definition\PasswordDefinition; use ByJG\Authenticate\Model\UserModel; use ByJG\Authenticate\Model\UserPropertiesModel; +use ByJG\MicroOrm\Literal\HexUuidLiteral; +use InvalidArgumentException; use OpenApi\Attributes as OA; -use RestReferenceArchitecture\Psr11; #[OA\Schema(required: ["email"], type: "object", xml: new OA\Xml(name: "User"))] class User extends UserModel @@ -26,10 +26,10 @@ class User extends UserModel const ROLE_USER = 'user'; /** - * @var ?string + * @var ?string|int|HexUuidLiteral */ #[OA\Property(type: "string", format: "string")] - protected ?string $userid = null; + protected string|int|HexUuidLiteral|null $userid = null; /** * @var ?string @@ -90,26 +90,24 @@ class User extends UserModel * @param string $password * @param string $admin */ - public function __construct(string $name = "", string $email = "", string $username = "", string $password = "", string $admin = "") + public function __construct(string $name = "", string $email = "", string $username = "", string $password = "", string $admin = "no") { parent::__construct($name, $email, $username, $password, $admin); - - $this->withPasswordDefinition(Psr11::container()->get(PasswordDefinition::class)); } /** - * @return string|null + * @return string|HexUuidLiteral|int|null */ - public function getUserid(): ?string + public function getUserid(): string|HexUuidLiteral|int|null { return $this->userid; } /** - * @param string|null $userid + * @param string|HexUuidLiteral|int|null $userid */ - public function setUserid(?string $userid): void + public function setUserid(string|HexUuidLiteral|int|null $userid): void { $this->userid = $userid; } diff --git a/src/OpenApiSpec.php b/src/OpenApiSpec.php index 808204a..0bb7ab0 100644 --- a/src/OpenApiSpec.php +++ b/src/OpenApiSpec.php @@ -1,6 +1,7 @@ build($env); } diff --git a/src/Rest/Login.php b/src/Rest/Login.php index 7c27b73..e0f5957 100644 --- a/src/Rest/Login.php +++ b/src/Rest/Login.php @@ -55,12 +55,10 @@ class Login )] public function post(HttpResponse $response, HttpRequest $request) { - OpenApiContext::validateRequest($request); - - $json = json_decode($request->payload()); + $json = OpenApiContext::validateRequest($request); $users = Psr11::container()->get(UsersDBDataset::class); - $user = $users->isValidUser($json->username, $json->password); + $user = $users->isValidUser($json["username"], $json["password"]); $metadata = JwtContext::createUserMetadata($user); $response->getResponseBag()->setSerializationRule(SerializationRuleEnum::SingleObject); @@ -149,12 +147,10 @@ public function refreshToken(HttpResponse $response, HttpRequest $request) )] public function postResetRequest(HttpResponse $response, HttpRequest $request) { - OpenApiContext::validateRequest($request); - - $json = json_decode($request->payload()); + $json = OpenApiContext::validateRequest($request); $users = Psr11::container()->get(UsersDBDataset::class); - $user = $users->getByEmail($json->email); + $user = $users->getByEmail($json["email"]); $token = BaseRepository::getUuid(); $code = rand(10000, 99999); @@ -168,7 +164,7 @@ public function postResetRequest(HttpResponse $response, HttpRequest $request) // Send email using MailWrapper $mailWrapper = Psr11::container()->get(MailWrapperInterface::class); - $envelope = Psr11::container()->get('MAIL_ENVELOPE', [$json->email, "RestReferenceArchitecture - Password Reset", "email_code.html", [ + $envelope = Psr11::container()->get('MAIL_ENVELOPE', [$json["email"], "RestReferenceArchitecture - Password Reset", "email_code.html", [ "code" => trim(chunk_split($code, 1, ' ')), "expire" => 10 ]]); @@ -181,18 +177,16 @@ public function postResetRequest(HttpResponse $response, HttpRequest $request) protected function validateResetToken($response, $request): array { - OpenApiContext::validateRequest($request); - - $json = json_decode($request->payload()); + $json = OpenApiContext::validateRequest($request); $users = Psr11::container()->get(UsersDBDataset::class); - $user = $users->getByEmail($json->email); + $user = $users->getByEmail($json["email"]); if (is_null($user)) { throw new Error422Exception("Invalid data"); } - if ($user->get("resettoken") != $json->token) { + if ($user->get("resettoken") != $json["token"]) { throw new Error422Exception("Invalid data"); } @@ -241,14 +235,14 @@ public function postConfirmCode(HttpResponse $response, HttpRequest $request) { list($users, $user, $json) = $this->validateResetToken($response, $request); - if ($user->get("resetcode") != $json->code) { + if ($user->get("resetcode") != $json["code"]) { throw new Error422Exception("Invalid data"); } $user->set("resetallowed", "yes"); $users->save($user); - $response->write(['token' => $json->token]); + $response->write(['token' => $json["token"]]); } /** @@ -293,13 +287,13 @@ public function postResetPassword(HttpResponse $response, HttpRequest $request) throw new Error422Exception("Invalid data"); } - $user->setPassword($json->password); + $user->setPassword($json["password"]); $user->set("resettoken", null); $user->set("resettokenexpire", null); $user->set("resetcode", null); $user->set("resetallowed", null); $users->save($user); - $response->write(['token' => $json->token]); + $response->write(['token' => $json["token"]]); } } diff --git a/src/Util/JwtContext.php b/src/Util/JwtContext.php index cbfc768..d149a79 100644 --- a/src/Util/JwtContext.php +++ b/src/Util/JwtContext.php @@ -2,7 +2,6 @@ namespace RestReferenceArchitecture\Util; -use ByJG\Authenticate\Model\UserModel; use ByJG\Config\Exception\ConfigException; use ByJG\Config\Exception\ConfigNotFoundException; use ByJG\Config\Exception\DependencyInjectionException; @@ -24,11 +23,11 @@ class JwtContext protected static ?HttpRequest $request; /** - * @param ?UserModel $user + * @param ?User $user * @return array * @throws Error401Exception */ - public static function createUserMetadata(?UserModel $user): array + public static function createUserMetadata(?User $user): array { if (is_null($user)) { throw new Error401Exception('Username or password is invalid'); @@ -52,7 +51,7 @@ public static function createUserMetadata(?UserModel $user): array * @throws KeyNotFoundException * @throws ReflectionException */ - public static function createToken($properties = []) + public static function createToken(array $properties = []) { $jwt = Psr11::container()->get(JwtWrapper::class); $jwtData = $jwt->createJwtData($properties, 60 * 60 * 24 * 7); // 7 Dias diff --git a/src/Util/OpenApiContext.php b/src/Util/OpenApiContext.php index 4766791..5abf66b 100644 --- a/src/Util/OpenApiContext.php +++ b/src/Util/OpenApiContext.php @@ -5,6 +5,7 @@ use ByJG\ApiTools\Base\Schema; use ByJG\RestServer\Exception\Error400Exception; use ByJG\RestServer\HttpRequest; +use ByJG\Serializer\Serialize; use Exception; use RestReferenceArchitecture\Psr11; @@ -21,7 +22,7 @@ public static function validateRequest(HttpRequest $request) $bodyRequestDef = $schema->getRequestParameters($path, $method); // Validate the request body (payload) - if (str_contains($request->getHeader('Content-Type'), 'multipart/')) { + if (str_contains($request->getHeader('Content-Type') ?? "", 'multipart/')) { $requestBody = $request->post(); $files = $request->uploadedFiles()->getKeys(); $requestBody = array_merge($requestBody, array_combine($files, $files)); @@ -35,6 +36,6 @@ public static function validateRequest(HttpRequest $request) throw new Error400Exception(explode("\n", $ex->getMessage())[0]); } - return $requestBody; + return Serialize::from($requestBody)->withDoNotParseNullValues()->toArray(); } -} \ No newline at end of file +} diff --git a/templates/codegen/test.php.jinja b/templates/codegen/test.php.jinja index 048ba1b..a44ef8f 100644 --- a/templates/codegen/test.php.jinja +++ b/templates/codegen/test.php.jinja @@ -1,6 +1,6 @@