Skip to content

Commit 8fa149e

Browse files
committed
test: add comprehensive tests for TemplateVariables
Add 21 tests with 53 assertions covering: - Setters/getters for all 9 variables (via DataProvider) - Null returns for unset variables - Array conversion and merge functionality - Whitelist validation (rejects unknown variables) - Type validation (rejects wrong types) - Invalid method calls Use PHP 8 attributes for DataProviders for cleaner syntax. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 070c568 commit 8fa149e

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Libresign\Tests\Unit\Handler;
11+
12+
use OCA\Libresign\Handler\TemplateVariables;
13+
use OCA\Libresign\Tests\Unit\TestCase;
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class TemplateVariablesTest extends TestCase {
20+
private TemplateVariables $variables;
21+
22+
public function setUp(): void {
23+
parent::setUp();
24+
$this->variables = new TemplateVariables();
25+
}
26+
27+
public static function provideValidValues(): array {
28+
return [
29+
'uuid' => ['uuid', 'test-uuid-123'],
30+
'signers' => ['signers', [['name' => 'John']]],
31+
'signedBy' => ['signedBy', 'LibreSign'],
32+
'direction' => ['direction', 'ltr'],
33+
'linkToSite' => ['linkToSite', 'https://example.com'],
34+
'validationSite' => ['validationSite', 'https://validate.com'],
35+
'validateIn' => ['validateIn', 'Validate in %s'],
36+
'qrcode' => ['qrcode', 'base64string'],
37+
'qrcodeSize' => ['qrcodeSize', 108],
38+
];
39+
}
40+
41+
#[DataProvider('provideValidValues')]
42+
public function testSettersAndGetters(string $variable, mixed $value): void {
43+
$setter = 'set' . ucfirst($variable);
44+
$getter = 'get' . ucfirst($variable);
45+
46+
$result = $this->variables->$setter($value);
47+
$this->assertSame($this->variables, $result);
48+
49+
$this->assertSame($value, $this->variables->$getter());
50+
51+
$this->assertTrue($this->variables->has($variable));
52+
}
53+
54+
public function testGettersReturnNullForUnsetVariables(): void {
55+
$this->assertNull($this->variables->getUuid());
56+
$this->assertNull($this->variables->getQrcodeSize());
57+
}
58+
59+
public function testToArrayAndMerge(): void {
60+
$this->variables->setUuid('uuid-123')->setSignedBy('Signer');
61+
62+
$array = $this->variables->toArray();
63+
$this->assertSame(['uuid' => 'uuid-123', 'signedBy' => 'Signer'], $array);
64+
65+
$result = $this->variables->merge(['uuid' => 'new-uuid', 'direction' => 'rtl']);
66+
$this->assertSame($this->variables, $result);
67+
$this->assertSame('new-uuid', $this->variables->getUuid());
68+
$this->assertSame('rtl', $this->variables->getDirection());
69+
}
70+
71+
public static function provideInvalidVariables(): array {
72+
return [
73+
['setInvalidVar'],
74+
['getInvalidVar'],
75+
];
76+
}
77+
78+
#[DataProvider('provideInvalidVariables')]
79+
public function testRejectsInvalidVariables(string $method): void {
80+
$this->expectException(\InvalidArgumentException::class);
81+
$this->expectExceptionMessage("Template variable 'invalidVar' is not allowed");
82+
83+
$this->variables->$method('value');
84+
}
85+
86+
public function testMergeRejectsInvalidVariables(): void {
87+
$this->expectException(\InvalidArgumentException::class);
88+
$this->expectExceptionMessage("Template variable 'invalidVar' is not allowed");
89+
90+
$this->variables->merge(['invalidVar' => 'value']);
91+
}
92+
93+
public static function provideInvalidTypes(): array {
94+
return [
95+
'uuid expects string' => ['uuid', 123, 'string', 'integer'],
96+
'signers expects array' => ['signers', 'string', 'array', 'string'],
97+
'qrcodeSize expects integer' => ['qrcodeSize', '108', 'integer', 'string'],
98+
];
99+
}
100+
101+
#[DataProvider('provideInvalidTypes')]
102+
public function testRejectsInvalidTypes(string $variable, mixed $value, string $expected, string $actual): void {
103+
$this->expectException(\InvalidArgumentException::class);
104+
$this->expectExceptionMessage("must be of type {$expected}, got {$actual}");
105+
106+
$method = 'set' . ucfirst($variable);
107+
$this->variables->$method($value);
108+
}
109+
110+
#[DataProvider('provideInvalidTypes')]
111+
public function testMergeRejectsInvalidTypes(string $variable, mixed $value, string $expected, string $actual): void {
112+
$this->expectException(\InvalidArgumentException::class);
113+
$this->expectExceptionMessage("must be of type {$expected}, got {$actual}");
114+
115+
$this->variables->merge([$variable => $value]);
116+
}
117+
118+
public function testRejectsInvalidMethod(): void {
119+
$this->expectException(\BadMethodCallException::class);
120+
$this->expectExceptionMessage("Method invalidMethod does not exist");
121+
122+
$this->variables->invalidMethod();
123+
}
124+
}

0 commit comments

Comments
 (0)