Skip to content

Commit d08b9d2

Browse files
committed
chore:more test scenarios
Signed-off-by: Crisciany Souza <[email protected]>
1 parent 44ca686 commit d08b9d2

File tree

1 file changed

+83
-12
lines changed

1 file changed

+83
-12
lines changed

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

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,107 @@
88

99
namespace OCA\Libresign\Tests\Unit\Service;
1010

11+
use InvalidArgumentException;
1112
use OCA\Libresign\AppInfo\Application;
1213
use OCA\Libresign\Service\Certificate\RulesService;
1314
use OCA\Libresign\Service\Certificate\ValidateService;
1415
use OCP\IL10N;
1516
use OCP\L10N\IFactory as IL10NFactory;
1617
use PHPUnit\Framework\Attributes\DataProvider;
1718

18-
class ValidateServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
19+
final class ValidateServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
1920

2021
private IL10N $l10n;
2122

2223
public function setUp(): void {
2324
$this->l10n = \OCP\Server::get(IL10NFactory::class)->get(Application::APP_ID);
2425
}
2526

26-
public function getService(): RulesService {
27-
return new RulesService(
28-
$this->l10n,
27+
private function getService(): ValidateService {
28+
$rulesService = new RulesService($this->l10n);
29+
return new ValidateService(
30+
$rulesService,
31+
$this->l10n
2932
);
3033
}
3134

32-
public function testValidateWithValidInput(): void {
33-
$service = new ValidateService($this->getService(), $this->l10n);
34-
$service->validate('CN', 'John Doe');
35+
#[DataProvider('providerValidInputs')]
36+
public function testValidateWithValidInput(string $fieldName, string $value): void {
37+
$service = $this->getService();
38+
$service->validate($fieldName, $value);
39+
$this->assertTrue(true); // se não lançar exceção, passou
3540
}
3641

37-
public function testValidateWithInvalidInput(): void {
38-
$service = new ValidateService($this->getService(), $this->l10n);
39-
$this->expectException(\InvalidArgumentException::class);
40-
$this->expectExceptionMessage("Parameter 'CN' should be betweeen 1 and 64.");
41-
$service->validate('CN', str_repeat('a', 65));
42+
public static function providerValidInputs(): array {
43+
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
50+
];
51+
}
52+
53+
#[DataProvider('providerInvalidInputs')]
54+
public function testValidateWithInvalidInput(string $fieldName, string $value, string $expectedMessage): void {
55+
$service = $this->getService();
56+
$this->expectException(InvalidArgumentException::class);
57+
$this->expectExceptionMessage($expectedMessage);
58+
$service->validate($fieldName, $value);
59+
}
60+
61+
public static function providerInvalidInputs(): array {
62+
return [
63+
// CN é obrigatório → vazio deve falhar
64+
['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."],
73+
];
74+
}
75+
76+
public function testValidateNamesWithValidArray(): void {
77+
$service = $this->getService();
78+
79+
$names = [
80+
['id' => 'CN', 'value' => 'Maria da Silva'],
81+
['id' => 'C', 'value' => 'BR'],
82+
];
83+
84+
$service->validateNames($names);
85+
86+
$this->assertTrue(true);
87+
}
88+
89+
public function testValidateNamesWithoutIdShouldFail(): void {
90+
$service = $this->getService();
91+
92+
$names = [
93+
['id' => '', 'value' => 'Invalid Name'],
94+
];
95+
96+
$this->expectException(InvalidArgumentException::class);
97+
$this->expectExceptionMessage('Parameter id is required!');
98+
99+
$service->validateNames($names);
100+
}
101+
102+
public function testValidateNamesWithInvalidValueShouldFail(): void {
103+
$service = $this->getService();
104+
105+
$names = [
106+
['id' => 'C', 'value' => 'BRA'], // inválido, deve ter 2 chars
107+
];
108+
109+
$this->expectException(InvalidArgumentException::class);
110+
$this->expectExceptionMessage("Parameter 'C' should be betweeen 2 and 2.");
111+
112+
$service->validateNames($names);
42113
}
43114
}

0 commit comments

Comments
 (0)