|
8 | 8 |
|
9 | 9 | namespace OCA\Libresign\Tests\Unit\Service;
|
10 | 10 |
|
| 11 | +use InvalidArgumentException; |
11 | 12 | use OCA\Libresign\AppInfo\Application;
|
12 | 13 | use OCA\Libresign\Service\Certificate\RulesService;
|
13 | 14 | use OCA\Libresign\Service\Certificate\ValidateService;
|
14 | 15 | use OCP\IL10N;
|
15 | 16 | use OCP\L10N\IFactory as IL10NFactory;
|
16 | 17 | use PHPUnit\Framework\Attributes\DataProvider;
|
17 | 18 |
|
18 |
| -class ValidateServiceTest extends \OCA\Libresign\Tests\Unit\TestCase { |
| 19 | +final class ValidateServiceTest extends \OCA\Libresign\Tests\Unit\TestCase { |
19 | 20 |
|
20 | 21 | private IL10N $l10n;
|
21 | 22 |
|
22 | 23 | public function setUp(): void {
|
23 | 24 | $this->l10n = \OCP\Server::get(IL10NFactory::class)->get(Application::APP_ID);
|
24 | 25 | }
|
25 | 26 |
|
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 |
29 | 32 | );
|
30 | 33 | }
|
31 | 34 |
|
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 |
35 | 40 | }
|
36 | 41 |
|
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); |
42 | 113 | }
|
43 | 114 | }
|
0 commit comments