Skip to content

Commit 7ba0095

Browse files
committed
More compatibility adjustments
1 parent a348f89 commit 7ba0095

17 files changed

+77
-83
lines changed

builder/Scripts.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static function codeGenerator(Event $event)
8787
* @throws ConfigException
8888
* @throws InvalidDateException
8989
*/
90-
public function runMigrate($arguments)
90+
public function runMigrate($arguments): void
9191
{
9292
$argumentList = $this->extractArguments($arguments);
9393
if (isset($argumentList["command"])) {
@@ -160,7 +160,7 @@ protected function extractArguments(array $arguments, bool $hasCmd = true): arra
160160
* @param array $arguments
161161
* @return void
162162
*/
163-
public function runGenOpenApiDocs(array $arguments)
163+
public function runGenOpenApiDocs(array $arguments): void
164164
{
165165
$docPath = $this->workdir . '/public/docs/';
166166

@@ -187,7 +187,7 @@ public function runGenOpenApiDocs(array $arguments)
187187
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
188188
* @throws Exception
189189
*/
190-
public function runCodeGenerator(array $arguments)
190+
public function runCodeGenerator(array $arguments): void
191191
{
192192
// Get Table Name
193193
$table = null;
@@ -379,7 +379,7 @@ public function runCodeGenerator(array $arguments)
379379
echo "Processing Test for table $table...\n";
380380
$template = $loader->getTemplate('test.php');
381381
if ($save) {
382-
$file = __DIR__ . '/../tests/Functional/Rest/' . $data['className'] . 'Test.php';
382+
$file = __DIR__ . '/../tests/Rest/' . $data['className'] . 'Test.php';
383383
file_put_contents($file, $template->render($data));
384384
echo "File saved in $file\n";
385385
} else {

docker/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ COPY templates /srv/templates
3131
COPY composer.* /srv
3232
COPY phpunit.xml.dist /srv
3333
COPY db /srv/db
34-
RUN composer install --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader
34+
RUN composer install --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader && \
35+
composer dump-autoload --optimize --classmap-authoritative -q

phpunit.xml.dist

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,36 @@ and open the template in the editor.
66
-->
77

88
<!-- see http://www.phpunit.de/wiki/Documentation -->
9-
<phpunit bootstrap="./vendor/autoload.php"
9+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
bootstrap="./vendor/autoload.php"
1011
colors="true"
1112
testdox="true"
1213
convertErrorsToExceptions="true"
1314
convertNoticesToExceptions="true"
1415
convertWarningsToExceptions="true"
1516
convertDeprecationsToExceptions="true"
16-
stopOnFailure="false">
17+
stopOnFailure="false"
18+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
1719

18-
<filter>
19-
<whitelist>
20-
<directory>./src</directory>
21-
</whitelist>
22-
</filter>
20+
<coverage>
21+
<include>
22+
<directory>./src</directory>
23+
</include>
24+
</coverage>
2325

24-
<testsuites>
25-
<testsuite name="Test Suite">
26-
<directory>./tests/</directory>
27-
</testsuite>
28-
</testsuites>
26+
<testsuites>
27+
<testsuite name="Backend Tests">
28+
<directory>./tests/Rest</directory>
29+
<directory>./tests/</directory>
30+
</testsuite>
31+
</testsuites>
2932

30-
<php>
31-
<ini name="display_errors" value="On" />
32-
<ini name="display_startup_errors" value="On" />
33-
<ini name="error_reporting" value="E_ALL" />
33+
<php>
34+
<ini name="display_errors" value="On"/>
35+
<ini name="display_startup_errors" value="On"/>
36+
<ini name="error_reporting" value="E_ALL"/>
3437

35-
<!-- important, do not remove -->
36-
<env name="APP_ENV" value="test" force="true" />
37-
</php>
38+
<!-- important, do not remove -->
39+
<env name="APP_ENV" value="test" force="true"/>
40+
</php>
3841
</phpunit>

src/Model/User.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace RestReferenceArchitecture\Model;
44

5-
use ByJG\Authenticate\Definition\PasswordDefinition;
65
use ByJG\Authenticate\Model\UserModel;
76
use ByJG\Authenticate\Model\UserPropertiesModel;
7+
use ByJG\MicroOrm\Literal\HexUuidLiteral;
8+
use InvalidArgumentException;
89
use OpenApi\Attributes as OA;
9-
use RestReferenceArchitecture\Psr11;
1010

1111
#[OA\Schema(required: ["email"], type: "object", xml: new OA\Xml(name: "User"))]
1212
class User extends UserModel
@@ -26,10 +26,10 @@ class User extends UserModel
2626
const ROLE_USER = 'user';
2727

2828
/**
29-
* @var ?string
29+
* @var ?string|int|HexUuidLiteral
3030
*/
3131
#[OA\Property(type: "string", format: "string")]
32-
protected ?string $userid = null;
32+
protected string|int|HexUuidLiteral|null $userid = null;
3333

3434
/**
3535
* @var ?string
@@ -90,26 +90,24 @@ class User extends UserModel
9090
* @param string $password
9191
* @param string $admin
9292
*/
93-
public function __construct(string $name = "", string $email = "", string $username = "", string $password = "", string $admin = "")
93+
public function __construct(string $name = "", string $email = "", string $username = "", string $password = "", string $admin = "no")
9494
{
9595
parent::__construct($name, $email, $username, $password, $admin);
96-
97-
$this->withPasswordDefinition(Psr11::container()->get(PasswordDefinition::class));
9896
}
9997

10098

10199
/**
102-
* @return string|null
100+
* @return string|HexUuidLiteral|int|null
103101
*/
104-
public function getUserid(): ?string
102+
public function getUserid(): string|HexUuidLiteral|int|null
105103
{
106104
return $this->userid;
107105
}
108106

109107
/**
110-
* @param string|null $userid
108+
* @param string|HexUuidLiteral|int|null $userid
111109
*/
112-
public function setUserid(?string $userid): void
110+
public function setUserid(string|HexUuidLiteral|int|null $userid): void
113111
{
114112
$this->userid = $userid;
115113
}

src/OpenApiSpec.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace RestReferenceArchitecture;
4+
45
use OpenApi\Attributes as OA;
56

67
#[OA\Info(
@@ -23,19 +24,13 @@
2324
)]
2425
#[OA\Schema(
2526
schema: 'error',
26-
required: ['error'],
2727
properties: [
28-
new OA\Property(
29-
property: 'error',
30-
required: ['type', 'message', 'file', 'line'],
31-
properties: [
32-
new OA\Property(property: 'type', description: 'A class de Exceção', type: 'string'),
33-
new OA\Property(property: 'message', description: 'A mensagem de erro', type: 'string'),
34-
new OA\Property(property: 'file', description: 'O arquivo que gerou o erro', type: 'string'),
35-
new OA\Property(property: 'line', description: 'A linha do erro', type: 'integer')
36-
],
37-
type: 'object'
38-
)
28+
new OA\Property('error', properties: [
29+
new OA\Property(property: 'type', description: 'A class de Exceção', type: 'string'),
30+
new OA\Property(property: 'message', description: 'A mensagem de erro', type: 'string'),
31+
new OA\Property(property: 'file', description: 'O arquivo que gerou o erro', type: 'string'),
32+
new OA\Property(property: 'line', description: 'A linha do erro', type: 'integer'),
33+
])
3934
],
4035
type: 'object'
4136
)]

src/Psr11.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ class Psr11
1717
private static ?Container $container = null;
1818

1919
/**
20-
* @param null $env
20+
* @param string|null $env
2121
* @return Container
2222
* @throws ConfigException
2323
* @throws ConfigNotFoundException
2424
* @throws InvalidArgumentException
2525
* @throws InvalidDateException
2626
*/
27-
public static function container($env = null): ?Container
27+
public static function container(?string $env = null): ?Container
2828
{
2929
if (is_null(self::$container)) {
30+
if (PHP_INT_SIZE < 8) {
31+
throw new \Exception("This application requires 64-bit PHP");
32+
}
3033
self::$container = self::environment()->build($env);
3134
}
3235

src/Rest/Login.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@ class Login
5555
)]
5656
public function post(HttpResponse $response, HttpRequest $request)
5757
{
58-
OpenApiContext::validateRequest($request);
59-
60-
$json = json_decode($request->payload());
58+
$json = OpenApiContext::validateRequest($request);
6159

6260
$users = Psr11::container()->get(UsersDBDataset::class);
63-
$user = $users->isValidUser($json->username, $json->password);
61+
$user = $users->isValidUser($json["username"], $json["password"]);
6462
$metadata = JwtContext::createUserMetadata($user);
6563

6664
$response->getResponseBag()->setSerializationRule(SerializationRuleEnum::SingleObject);
@@ -149,12 +147,10 @@ public function refreshToken(HttpResponse $response, HttpRequest $request)
149147
)]
150148
public function postResetRequest(HttpResponse $response, HttpRequest $request)
151149
{
152-
OpenApiContext::validateRequest($request);
153-
154-
$json = json_decode($request->payload());
150+
$json = OpenApiContext::validateRequest($request);
155151

156152
$users = Psr11::container()->get(UsersDBDataset::class);
157-
$user = $users->getByEmail($json->email);
153+
$user = $users->getByEmail($json["email"]);
158154

159155
$token = BaseRepository::getUuid();
160156
$code = rand(10000, 99999);
@@ -168,7 +164,7 @@ public function postResetRequest(HttpResponse $response, HttpRequest $request)
168164

169165
// Send email using MailWrapper
170166
$mailWrapper = Psr11::container()->get(MailWrapperInterface::class);
171-
$envelope = Psr11::container()->get('MAIL_ENVELOPE', [$json->email, "RestReferenceArchitecture - Password Reset", "email_code.html", [
167+
$envelope = Psr11::container()->get('MAIL_ENVELOPE', [$json["email"], "RestReferenceArchitecture - Password Reset", "email_code.html", [
172168
"code" => trim(chunk_split($code, 1, ' ')),
173169
"expire" => 10
174170
]]);
@@ -181,18 +177,16 @@ public function postResetRequest(HttpResponse $response, HttpRequest $request)
181177

182178
protected function validateResetToken($response, $request): array
183179
{
184-
OpenApiContext::validateRequest($request);
185-
186-
$json = json_decode($request->payload());
180+
$json = OpenApiContext::validateRequest($request);
187181

188182
$users = Psr11::container()->get(UsersDBDataset::class);
189-
$user = $users->getByEmail($json->email);
183+
$user = $users->getByEmail($json["email"]);
190184

191185
if (is_null($user)) {
192186
throw new Error422Exception("Invalid data");
193187
}
194188

195-
if ($user->get("resettoken") != $json->token) {
189+
if ($user->get("resettoken") != $json["token"]) {
196190
throw new Error422Exception("Invalid data");
197191
}
198192

@@ -241,14 +235,14 @@ public function postConfirmCode(HttpResponse $response, HttpRequest $request)
241235
{
242236
list($users, $user, $json) = $this->validateResetToken($response, $request);
243237

244-
if ($user->get("resetcode") != $json->code) {
238+
if ($user->get("resetcode") != $json["code"]) {
245239
throw new Error422Exception("Invalid data");
246240
}
247241

248242
$user->set("resetallowed", "yes");
249243
$users->save($user);
250244

251-
$response->write(['token' => $json->token]);
245+
$response->write(['token' => $json["token"]]);
252246
}
253247

254248
/**
@@ -293,13 +287,13 @@ public function postResetPassword(HttpResponse $response, HttpRequest $request)
293287
throw new Error422Exception("Invalid data");
294288
}
295289

296-
$user->setPassword($json->password);
290+
$user->setPassword($json["password"]);
297291
$user->set("resettoken", null);
298292
$user->set("resettokenexpire", null);
299293
$user->set("resetcode", null);
300294
$user->set("resetallowed", null);
301295
$users->save($user);
302296

303-
$response->write(['token' => $json->token]);
297+
$response->write(['token' => $json["token"]]);
304298
}
305299
}

src/Util/JwtContext.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace RestReferenceArchitecture\Util;
44

5-
use ByJG\Authenticate\Model\UserModel;
65
use ByJG\Config\Exception\ConfigException;
76
use ByJG\Config\Exception\ConfigNotFoundException;
87
use ByJG\Config\Exception\DependencyInjectionException;
@@ -24,11 +23,11 @@ class JwtContext
2423
protected static ?HttpRequest $request;
2524

2625
/**
27-
* @param ?UserModel $user
26+
* @param ?User $user
2827
* @return array
2928
* @throws Error401Exception
3029
*/
31-
public static function createUserMetadata(?UserModel $user): array
30+
public static function createUserMetadata(?User $user): array
3231
{
3332
if (is_null($user)) {
3433
throw new Error401Exception('Username or password is invalid');
@@ -52,7 +51,7 @@ public static function createUserMetadata(?UserModel $user): array
5251
* @throws KeyNotFoundException
5352
* @throws ReflectionException
5453
*/
55-
public static function createToken($properties = [])
54+
public static function createToken(array $properties = [])
5655
{
5756
$jwt = Psr11::container()->get(JwtWrapper::class);
5857
$jwtData = $jwt->createJwtData($properties, 60 * 60 * 24 * 7); // 7 Dias

src/Util/OpenApiContext.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ByJG\ApiTools\Base\Schema;
66
use ByJG\RestServer\Exception\Error400Exception;
77
use ByJG\RestServer\HttpRequest;
8+
use ByJG\Serializer\Serialize;
89
use Exception;
910
use RestReferenceArchitecture\Psr11;
1011

@@ -21,7 +22,7 @@ public static function validateRequest(HttpRequest $request)
2122
$bodyRequestDef = $schema->getRequestParameters($path, $method);
2223

2324
// Validate the request body (payload)
24-
if (str_contains($request->getHeader('Content-Type'), 'multipart/')) {
25+
if (str_contains($request->getHeader('Content-Type') ?? "", 'multipart/')) {
2526
$requestBody = $request->post();
2627
$files = $request->uploadedFiles()->getKeys();
2728
$requestBody = array_merge($requestBody, array_combine($files, $files));
@@ -35,6 +36,6 @@ public static function validateRequest(HttpRequest $request)
3536
throw new Error400Exception(explode("\n", $ex->getMessage())[0]);
3637
}
3738

38-
return $requestBody;
39+
return Serialize::from($requestBody)->withDoNotParseNullValues()->toArray();
3940
}
40-
}
41+
}

templates/codegen/test.php.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Test\Functional\Rest;
3+
namespace Test\Rest;
44

55
use ByJG\RestServer\Exception\Error401Exception;
66
use ByJG\RestServer\Exception\Error403Exception;

tests/Functional/Rest/BaseApiTestCase.php renamed to tests/Rest/BaseApiTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33

4-
namespace Test\Functional\Rest;
4+
namespace Test\Rest;
55

66
use ByJG\ApiTools\ApiTestCase;
77
use ByJG\ApiTools\Base\Schema;
@@ -16,7 +16,7 @@ class BaseApiTestCase extends ApiTestCase
1616
{
1717
protected static bool $databaseReset = false;
1818

19-
protected string $filePath = __DIR__ . '/../../../public/docs/openapi.json';
19+
protected string $filePath = __DIR__ . '/../../public/docs/openapi.json';
2020

2121
protected function setUp(): void
2222
{

tests/Functional/Rest/Credentials.php renamed to tests/Rest/Credentials.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Test\Functional\Rest;
3+
namespace Test\Rest;
44

55
use ByJG\Util\Uri;
66
use ByJG\WebRequest\Psr7\Request;

0 commit comments

Comments
 (0)