Skip to content

Commit 08f37a7

Browse files
committed
test(cache): completar cobertura de FileCacheStrategy
- Adicionados 2 testes em FileCacheStrategyTest.php: * testConstructorThrowsWhenDirectoryCannotBeCreated — L28: mkdir falha dentro de um arquivo (não-diretório) → DiscoveryException::cacheCorruption * testClearReturnsSilentlyWhenDirectoryUnreadable — L94: glob() retorna false com dir sem leitura → clear() retorna sem throw - Suprimidos PHP Warnings com @ operator: * @mkdir() no construtor (L27): E_WARNING antes de retornar false * O false já é tratado — o warning é ruído Methods: 71.43% → 85.71% (6/7) | Lines: 93.94% → 96.97% (32/33) L94 coberta apenas em ambientes não-root (markTestSkipped se root) keyToPath() = método privado não rastreável individualmente pelo PCOV Tests: 222, Assertions: 440 ✓
1 parent d030090 commit 08f37a7

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/Cache/FileCacheStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public function __construct(
2525
private readonly string $cacheDirectory,
2626
) {
27-
if (! is_dir($this->cacheDirectory) && ! mkdir($this->cacheDirectory, 0o755, true)) {
27+
if (! is_dir($this->cacheDirectory) && ! @mkdir($this->cacheDirectory, 0o755, true)) {
2828
throw DiscoveryException::cacheCorruption('init', "Cannot create cache directory: {$this->cacheDirectory}");
2929
}
3030
}

tests/Unit/Cache/FileCacheStrategyTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,45 @@ public function testCreatesDirectoryOnConstruction(): void
166166
$this->removeDir($dir);
167167
}
168168

169+
public function testConstructorThrowsWhenDirectoryCannotBeCreated(): void
170+
{
171+
// Create a file where the directory should be — mkdir will fail
172+
$blockingFile = sys_get_temp_dir() . '/kc_block_' . uniqid();
173+
file_put_contents($blockingFile, 'blocking');
174+
175+
// Trying to create a directory INSIDE the blocking file triggers mkdir failure
176+
$impossibleDir = $blockingFile . '/subdir';
177+
178+
$this->expectException(DiscoveryException::class);
179+
180+
try {
181+
new FileCacheStrategy($impossibleDir);
182+
} finally {
183+
unlink($blockingFile);
184+
}
185+
}
186+
187+
public function testClearReturnsSilentlyWhenDirectoryUnreadable(): void
188+
{
189+
// Populate some cache files first
190+
$this->cache->set('x', new DiscoveryResult());
191+
192+
// Make the cache directory unreadable so glob() returns false
193+
chmod($this->cacheDir, 0o000);
194+
195+
if (is_readable($this->cacheDir)) {
196+
chmod($this->cacheDir, 0o755);
197+
$this->markTestSkipped('Cannot make directory unreadable (running as root).');
198+
}
199+
200+
// clear() must not throw — just return early (L94)
201+
$this->cache->clear();
202+
$this->assertTrue(true); // reached here without exception
203+
204+
// Restore so tearDown can clean up
205+
chmod($this->cacheDir, 0o755);
206+
}
207+
169208
private function removeDir(string $dir): void
170209
{
171210
if (! is_dir($dir)) {

0 commit comments

Comments
 (0)