|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | 5 | use Bnussbau\TrmnlPipeline\Data\PaletteData; |
| 6 | +use Bnussbau\TrmnlPipeline\Exceptions\ProcessingException; |
6 | 7 |
|
7 | 8 | describe('PaletteData', function (): void { |
8 | 9 | it('can load palettes from JSON', function (): void { |
|
76 | 77 | ]); |
77 | 78 | expect($palette->frameworkClass)->toBe(''); |
78 | 79 | }); |
| 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 | + }); |
79 | 158 | }); |
0 commit comments