Skip to content

Commit 77b64e6

Browse files
committed
chore: changes noted in the code review
Signed-off-by: Crisciany Souza <[email protected]>
1 parent d08b9d2 commit 77b64e6

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

lib/Service/Certificate/RulesService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ class RulesService {
4343
public function __construct(
4444
protected IL10N $l10n,
4545
) {
46-
4746
}
4847

4948
public function getRule(string $fieldName): array {
49+
if (!array_key_exists($fieldName, $this->rules)) return [];
5050
if (!isset($this->rules[$fieldName]['helperText'])) {
5151
$this->rules[$fieldName]['helperText'] = $this->getHelperText($fieldName);
5252
if (empty($this->rules[$fieldName]['helperText'])) {

lib/Service/Certificate/ValidateService.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,34 @@ public function __construct(
1717
protected RulesService $rulesService,
1818
protected IL10N $l10n,
1919
) {
20-
2120
}
2221

23-
public function validate(string $fieldName, string $value):void {
22+
public function validate(string $fieldName, string $value): void {
2423
$rule = $this->rulesService->getRule($fieldName);
2524
$value = trim($value);
2625
$length = strlen($value);
26+
27+
if ($fieldName === 'id' && $length === 0) {
28+
throw new InvalidArgumentException('Parameter id is invalid');
29+
}
30+
2731
if (!$length && isset($rule['required']) && $rule['required']) {
2832
throw new InvalidArgumentException(
2933
$this->l10n->t("Parameter '%s' is required!", [$fieldName])
3034
);
3135
}
36+
3237
if ($length > $rule['max'] || $length < $rule['min']) {
3338
throw new InvalidArgumentException(
34-
$this->l10n->t("Parameter '%s' should be betweeen %s and %s.", [$fieldName, $rule['min'], $rule['max']])
39+
$this->l10n->t(
40+
"Parameter '%s' should be between %s and %s.",
41+
[$fieldName, $rule['min'], $rule['max']]
42+
)
3543
);
3644
}
3745
}
3846

47+
3948
public function validateNames(array $names) {
4049
foreach ($names as $item) {
4150
if (empty($item['id'])) {

tests/php/Unit/Service/Certificate/ValidateServiceTest.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ private function getService(): ValidateService {
3636
public function testValidateWithValidInput(string $fieldName, string $value): void {
3737
$service = $this->getService();
3838
$service->validate($fieldName, $value);
39-
$this->assertTrue(true); // se não lançar exceção, passou
39+
$this->assertTrue(true);
4040
}
4141

4242
public static function providerValidInputs(): array {
4343
return [
44-
['CN', 'John Doe'], // requerido, dentro do limite
45-
['C', 'BR'], // exatamente 2 letras
46-
['ST', 'Amazonas'], // válido
47-
['L', 'Manaus'], // válido
48-
['O', 'LibreCode'], // válido
49-
['OU', 'Development'], // válido
44+
['CN', 'John Doe'],
45+
['C', 'BR'],
46+
['ST', 'Amazonas'],
47+
['L', 'Manaus'],
48+
['O', 'LibreCode'],
49+
['OU', 'Development'],
5050
];
5151
}
5252

@@ -60,16 +60,11 @@ public function testValidateWithInvalidInput(string $fieldName, string $value, s
6060

6161
public static function providerInvalidInputs(): array {
6262
return [
63-
// CN é obrigatório → vazio deve falhar
6463
['CN', '', "Parameter 'CN' is required!"],
65-
// CN muito longo
66-
['CN', str_repeat('a', 65), "Parameter 'CN' should be betweeen 1 and 64."],
67-
// C muito curto
68-
['C', 'B', "Parameter 'C' should be betweeen 2 and 2."],
69-
// C muito longo
70-
['C', 'BRA', "Parameter 'C' should be betweeen 2 and 2."],
71-
// ST acima do limite
72-
['ST', str_repeat('x', 129), "Parameter 'ST' should be betweeen 1 and 128."],
64+
['CN', str_repeat('a', 65), "Parameter 'CN' should be between 1 and 64."],
65+
['C', 'B', "Parameter 'C' should be between 2 and 2."],
66+
['C', 'BRA', "Parameter 'C' should be between 2 and 2."],
67+
['ST', str_repeat('x', 129), "Parameter 'ST' should be between 1 and 128."],
7368
];
7469
}
7570

@@ -90,7 +85,8 @@ public function testValidateNamesWithoutIdShouldFail(): void {
9085
$service = $this->getService();
9186

9287
$names = [
93-
['id' => '', 'value' => 'Invalid Name'],
88+
['id' => '', 'value' => 'Name'],
89+
['value' => 'Name'],
9490
];
9591

9692
$this->expectException(InvalidArgumentException::class);
@@ -99,16 +95,23 @@ public function testValidateNamesWithoutIdShouldFail(): void {
9995
$service->validateNames($names);
10096
}
10197

102-
public function testValidateNamesWithInvalidValueShouldFail(): void {
103-
$service = $this->getService();
10498

105-
$names = [
106-
['id' => 'C', 'value' => 'BRA'], // inválido, deve ter 2 chars
107-
];
99+
#[DataProvider('providerInvalidNames')]
100+
public function testValidateNamesWithInvalidValueShouldFail(array $name, string $expectedMessage): void {
101+
$service = $this->getService();
108102

109103
$this->expectException(InvalidArgumentException::class);
110-
$this->expectExceptionMessage("Parameter 'C' should be betweeen 2 and 2.");
104+
$this->expectExceptionMessage($expectedMessage);
111105

112-
$service->validateNames($names);
106+
$service->validateNames([$name]);
107+
}
108+
109+
public static function providerInvalidNames(): array {
110+
return [
111+
[['id' => 'C', 'value' => ''], "Parameter 'C' should be between 2 and 2."],
112+
[['id' => 'C', 'value' => 'B'], "Parameter 'C' should be between 2 and 2."],
113+
[['id' => 'C', 'value' => 'BRA'], "Parameter 'C' should be between 2 and 2."],
114+
[['id' => 'C', 'value' => 'BRAA'], "Parameter 'C' should be between 2 and 2."],
115+
];
113116
}
114117
}

0 commit comments

Comments
 (0)