diff --git a/src/Plugins/Tia/ReadCapture.php b/src/Plugins/Tia/ReadCapture.php index 945eb968..7380f981 100644 --- a/src/Plugins/Tia/ReadCapture.php +++ b/src/Plugins/Tia/ReadCapture.php @@ -89,13 +89,16 @@ final class ReadCapture private static function disable(): void { + if (in_array(self::PROTOCOL, stream_get_wrappers(), true)) { + stream_wrapper_unregister(self::PROTOCOL); + } + if (self::$previousWrapper === null) { stream_wrapper_restore(self::PROTOCOL); return; } - stream_wrapper_unregister(self::PROTOCOL); stream_wrapper_register(self::PROTOCOL, self::$previousWrapper); } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 821f6624..e2e8487d 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2101,6 +2101,10 @@ ✓ the file wrapper → it passes filesystem operations through while a test records ✓ the file wrapper → it writes with LOCK_EX while a test records ✓ the file wrapper → it reports a missing path to the error handler as plain PHP does while a test records + ✓ the file wrapper → it keeps no memory for each filesystem call while a test records + ✓ the file wrapper → it keeps no memory for each test it records + ✓ the file wrapper → it is restored quietly when the test already restored it + ✓ the file wrapper → it is restored quietly when the test unregistered it ✓ the file wrapper → it reports symlinks as links while a test records with dataset "a link to an existing file" ✓ the file wrapper → it reports symlinks as links while a test records with dataset "a dangling link" ✓ a wrapper already registered for file:// → it opens reads through it and still links them @@ -2623,4 +2627,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1915 passed (4368 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1919 passed (4374 assertions) \ No newline at end of file diff --git a/tests/Unit/Plugins/Tia/ReadCapture.php b/tests/Unit/Plugins/Tia/ReadCapture.php index 5e580f29..a2e7d829 100644 --- a/tests/Unit/Plugins/Tia/ReadCapture.php +++ b/tests/Unit/Plugins/Tia/ReadCapture.php @@ -238,6 +238,78 @@ describe('the file wrapper', function (): void { ]); }); + it('keeps no memory for each filesystem call while a test records', function (): void { + $growth = 0; + + tiaReadCaptureLinks(function () use (&$growth): void { + fclose(fopen(__FILE__, 'rb')); + + $before = memory_get_usage(); + + for ($i = 0; $i < 10_000; $i++) { + fclose(fopen(__FILE__, 'rb')); + } + + $growth = memory_get_usage() - $before; + }); + + expect($growth)->toBeLessThan(100_000); + }); + + it('keeps no memory for each test it records', function (): void { + tiaReadCaptureLinks(fn (): bool => true); + + $before = memory_get_usage(); + + for ($i = 0; $i < 5_000; $i++) { + tiaReadCaptureLinks(fn (): bool => true); + } + + expect(memory_get_usage() - $before)->toBeLessThan(100_000); + }); + + it('is restored quietly when the test already restored it', function (): void { + $errors = []; + + set_error_handler(function (int $level, string $message) use (&$errors): bool { + $errors[] = $message; + + return true; + }); + + try { + tiaReadCaptureLinks(fn (): bool => stream_wrapper_restore('file')); + } finally { + restore_error_handler(); + } + + expect($errors)->toBeEmpty() + ->and(tiaReadCaptureWrapperType())->toBe('plainfile'); + }); + + it('is restored quietly when the test unregistered it', function (): void { + $errors = []; + + set_error_handler(function (int $level, string $message) use (&$errors): bool { + $errors[] = $message; + + return true; + }); + + try { + tiaReadCaptureLinks(fn (): bool => stream_wrapper_unregister('file')); + } finally { + restore_error_handler(); + + if (! in_array('file', stream_get_wrappers(), true)) { + stream_wrapper_restore('file'); + } + } + + expect($errors)->toBeEmpty() + ->and(tiaReadCaptureWrapperType())->toBe('plainfile'); + }); + it('reports symlinks as links while a test records', function (bool $targetExists, bool $expectedExists): void { $directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-read-capture-'.bin2hex(random_bytes(8)); mkdir($directory); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 8336f074..507b1dde 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -21,7 +21,7 @@ test('parallel', function () use ($run): void { $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); $profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile'); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 30 skipped, 1788 passed (3843 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 30 skipped, 1792 passed (3849 assertions)'; expect($output) ->toContain("Tests: {$expected}")