Skip to content

Commit f7c86bf

Browse files
committed
test: improve cov for PaletteData
1 parent 3d29d26 commit f7c86bf

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/Data/PaletteData.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ public function __construct(
2323
/**
2424
* Load palette data from JSON file
2525
*
26+
* @param string|null $jsonPath Optional path to JSON file (defaults to palettes.json in package)
2627
* @return array<string, PaletteData>
2728
*
2829
* @throws ProcessingException
2930
*/
30-
public static function loadFromJson(): array
31+
public static function loadFromJson(?string $jsonPath = null): array
3132
{
32-
$jsonPath = __DIR__.'/palettes.json';
33+
$jsonPath = $jsonPath ?? __DIR__.'/palettes.json';
3334

3435
if (! file_exists($jsonPath)) {
3536
throw new ProcessingException("Palettes JSON file not found at: {$jsonPath}");

tests/PaletteDataTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Bnussbau\TrmnlPipeline\Data\PaletteData;
6+
use Bnussbau\TrmnlPipeline\Exceptions\ProcessingException;
67

78
describe('PaletteData', function (): void {
89
it('can load palettes from JSON', function (): void {
@@ -76,4 +77,82 @@
7677
]);
7778
expect($palette->frameworkClass)->toBe('');
7879
});
80+
81+
it('throws when palettes JSON file does not exist', function (): void {
82+
$missingPath = sys_get_temp_dir().'/palettes-nonexistent-'.uniqid().'.json';
83+
84+
expect(fn () => PaletteData::loadFromJson($missingPath))
85+
->toThrow(ProcessingException::class, 'Palettes JSON file not found at: '.$missingPath);
86+
});
87+
88+
it('throws when palettes JSON file cannot be read', function (): void {
89+
$tmpFile = tempnam(sys_get_temp_dir(), 'palettes');
90+
if ($tmpFile === false) {
91+
throw new \RuntimeException('Failed to create temp file');
92+
}
93+
chmod($tmpFile, 0000);
94+
try {
95+
expect(fn () => PaletteData::loadFromJson($tmpFile))
96+
->toThrow(ProcessingException::class, 'Failed to read palettes JSON file');
97+
} finally {
98+
chmod($tmpFile, 0600);
99+
unlink($tmpFile);
100+
}
101+
});
102+
103+
it('throws when palettes JSON is invalid', function (): void {
104+
$tmpFile = tempnam(sys_get_temp_dir(), 'palettes');
105+
if ($tmpFile === false) {
106+
throw new \RuntimeException('Failed to create temp file');
107+
}
108+
try {
109+
file_put_contents($tmpFile, '{ invalid json }');
110+
expect(fn () => PaletteData::loadFromJson($tmpFile))
111+
->toThrow(ProcessingException::class, 'Invalid JSON in palettes file:');
112+
} finally {
113+
unlink($tmpFile);
114+
}
115+
});
116+
117+
it('throws when palettes JSON does not decode to array', function (): void {
118+
$tmpFile = tempnam(sys_get_temp_dir(), 'palettes');
119+
if ($tmpFile === false) {
120+
throw new \RuntimeException('Failed to create temp file');
121+
}
122+
try {
123+
file_put_contents($tmpFile, '123');
124+
expect(fn () => PaletteData::loadFromJson($tmpFile))
125+
->toThrow(ProcessingException::class, 'Invalid JSON structure: expected array');
126+
} finally {
127+
unlink($tmpFile);
128+
}
129+
});
130+
131+
it('throws when palettes JSON is missing data array', function (): void {
132+
$tmpFile = tempnam(sys_get_temp_dir(), 'palettes');
133+
if ($tmpFile === false) {
134+
throw new \RuntimeException('Failed to create temp file');
135+
}
136+
try {
137+
file_put_contents($tmpFile, '{}');
138+
expect(fn () => PaletteData::loadFromJson($tmpFile))
139+
->toThrow(ProcessingException::class, "Invalid palettes JSON structure: missing 'data' array");
140+
} finally {
141+
unlink($tmpFile);
142+
}
143+
});
144+
145+
it('throws when palette entry is missing id field', function (): void {
146+
$tmpFile = tempnam(sys_get_temp_dir(), 'palettes');
147+
if ($tmpFile === false) {
148+
throw new \RuntimeException('Failed to create temp file');
149+
}
150+
try {
151+
file_put_contents($tmpFile, '{"data":[{"name":"no-id"}]}');
152+
expect(fn () => PaletteData::loadFromJson($tmpFile))
153+
->toThrow(ProcessingException::class, "Palette data missing required 'id' field");
154+
} finally {
155+
unlink($tmpFile);
156+
}
157+
});
79158
});

0 commit comments

Comments
 (0)