Skip to content

Commit 48ddfb4

Browse files
committed
Fix IOException file path test on Windows
Use directory-based test on Windows where chmod doesn't restrict file access, and chmod-based test on Unix systems.
1 parent 5639119 commit 48ddfb4

1 file changed

Lines changed: 28 additions & 11 deletions

File tree

tests/Unit/EdgeCaseValidationTest.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,35 @@
323323
});
324324

325325
it('sets file path on unreadable file exception', function (): void {
326-
// Create an unreadable file
327-
$tempFile = tempnam(sys_get_temp_dir(), 'jsonstream_test_');
328-
chmod($tempFile, 0000);
329-
330-
try {
331-
StreamReader::fromFile($tempFile);
332-
expect(false)->toBeTrue(); // Should not reach here
333-
} catch (IOException $e) {
334-
expect($e->getFilePath())->toBe($tempFile);
335-
} finally {
336-
chmod($tempFile, 0644);
326+
if (DIRECTORY_SEPARATOR === '\\') {
327+
// Windows: chmod does not restrict access; use a directory instead
328+
// fopen() on a directory fails on Windows, triggering IOException
329+
$tempFile = tempnam(sys_get_temp_dir(), 'jsonstream_test_');
337330
unlink($tempFile);
331+
mkdir($tempFile);
332+
333+
try {
334+
StreamReader::fromFile($tempFile);
335+
expect(false)->toBeTrue(); // Should not reach here
336+
} catch (IOException $e) {
337+
expect($e->getFilePath())->toBe($tempFile);
338+
} finally {
339+
rmdir($tempFile);
340+
}
341+
} else {
342+
// Unix: chmod makes the file unreadable
343+
$tempFile = tempnam(sys_get_temp_dir(), 'jsonstream_test_');
344+
chmod($tempFile, 0000);
345+
346+
try {
347+
StreamReader::fromFile($tempFile);
348+
expect(false)->toBeTrue(); // Should not reach here
349+
} catch (IOException $e) {
350+
expect($e->getFilePath())->toBe($tempFile);
351+
} finally {
352+
chmod($tempFile, 0644);
353+
unlink($tempFile);
354+
}
338355
}
339356
});
340357
});

0 commit comments

Comments
 (0)