Skip to content

Commit fa19a70

Browse files
test: cover strongly typed validation metadata normalization
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 8a0f9be commit fa19a70

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Libresign\Tests\Unit\Service\File;
11+
12+
use OCA\Libresign\Service\File\ValidationMetadataNormalizer;
13+
use PHPUnit\Framework\Attributes\DataProvider;
14+
15+
final class ValidationMetadataNormalizerTest extends \OCA\Libresign\Tests\Unit\TestCase {
16+
#[DataProvider('provideNormalizationScenarios')]
17+
public function testNormalizeMetadataContract(
18+
array $metadata,
19+
string $fileName,
20+
int $totalPages,
21+
array $expectedSubset,
22+
array $missingKeys = [],
23+
): void {
24+
$normalized = ValidationMetadataNormalizer::normalize($metadata, $fileName, $totalPages);
25+
26+
foreach ($expectedSubset as $key => $value) {
27+
$this->assertArrayHasKey($key, $normalized);
28+
$this->assertSame($value, $normalized[$key]);
29+
}
30+
31+
foreach ($missingKeys as $key) {
32+
$this->assertArrayNotHasKey($key, $normalized);
33+
}
34+
}
35+
36+
public static function provideNormalizationScenarios(): array {
37+
return [
38+
'normalizes required keys from filename and page count' => [
39+
'metadata' => [],
40+
'fileName' => 'contract.PDF',
41+
'totalPages' => 5,
42+
'expectedSubset' => [
43+
'p' => 5,
44+
'extension' => 'pdf',
45+
],
46+
],
47+
'keeps provided non-empty extension' => [
48+
'metadata' => ['extension' => 'docx'],
49+
'fileName' => 'contract.PDF',
50+
'totalPages' => 1,
51+
'expectedSubset' => [
52+
'p' => 1,
53+
'extension' => 'docx',
54+
],
55+
],
56+
'clamps negative page count to zero' => [
57+
'metadata' => [],
58+
'fileName' => 'contract.pdf',
59+
'totalPages' => -2,
60+
'expectedSubset' => [
61+
'p' => 0,
62+
'extension' => 'pdf',
63+
],
64+
],
65+
'removes optional keys with invalid types' => [
66+
'metadata' => [
67+
'original_file_deleted' => '1',
68+
'pdfVersion' => 17,
69+
'status_changed_at' => 123,
70+
],
71+
'fileName' => 'contract.pdf',
72+
'totalPages' => 1,
73+
'expectedSubset' => [
74+
'p' => 1,
75+
'extension' => 'pdf',
76+
],
77+
'missingKeys' => ['original_file_deleted', 'pdfVersion', 'status_changed_at'],
78+
],
79+
'normalizes dimensions and strips invalid entries' => [
80+
'metadata' => [
81+
'd' => [
82+
['w' => '100', 'h' => 200],
83+
['w' => 'x', 'h' => 300],
84+
],
85+
],
86+
'fileName' => 'contract.pdf',
87+
'totalPages' => 2,
88+
'expectedSubset' => [
89+
'p' => 2,
90+
'extension' => 'pdf',
91+
'd' => [
92+
['w' => 100.0, 'h' => 200.0],
93+
],
94+
],
95+
],
96+
'removes dimensions key when all entries are invalid' => [
97+
'metadata' => [
98+
'd' => [
99+
['w' => 'x', 'h' => 'y'],
100+
],
101+
],
102+
'fileName' => 'contract.pdf',
103+
'totalPages' => 1,
104+
'expectedSubset' => [
105+
'p' => 1,
106+
'extension' => 'pdf',
107+
],
108+
'missingKeys' => ['d'],
109+
],
110+
];
111+
}
112+
}

0 commit comments

Comments
 (0)