Skip to content

Commit 2c2b841

Browse files
committed
Refactor PathMatcherTest and StreamWrapperTest to ensure Config resets after each test case
1 parent 0edc2bc commit 2c2b841

2 files changed

Lines changed: 105 additions & 77 deletions

File tree

tests/Internal/PathMatcherTest.php

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,32 +138,40 @@
138138
});
139139

140140
test('rejects unwhitelisted vendor, var, and storage paths when config does not include them', function () {
141-
Config::set([
142-
'include' => ['src/**', 'app/**'],
143-
]);
144-
145-
expect(PathMatcher::mayPathBeIncluded('vendor/monolog/monolog/src/Logger.php'))->toBeFalse()
146-
->and(PathMatcher::mayPathBeIncluded('/var/www/vendor/symfony/console/App.php'))->toBeFalse()
147-
->and(PathMatcher::mayPathBeIncluded('/var/www/var/cache/Container.php'))->toBeFalse()
148-
->and(PathMatcher::mayPathBeIncluded('storage/framework/views/1.php'))->toBeFalse()
149-
;
141+
try {
142+
Config::set([
143+
'include' => ['src/**', 'app/**'],
144+
]);
145+
146+
expect(PathMatcher::mayPathBeIncluded('vendor/monolog/monolog/src/Logger.php'))->toBeFalse()
147+
->and(PathMatcher::mayPathBeIncluded('/var/www/vendor/symfony/console/App.php'))->toBeFalse()
148+
->and(PathMatcher::mayPathBeIncluded('/var/www/var/cache/Container.php'))->toBeFalse()
149+
->and(PathMatcher::mayPathBeIncluded('storage/framework/views/1.php'))->toBeFalse()
150+
;
151+
} finally {
152+
Config::reset();
153+
}
150154
});
151155

152156
test('permits vendor, var, or storage paths when explicitly whitelisted in include config', function () {
153-
Config::set([
154-
'include' => [
155-
'src/**',
156-
'vendor/my-org/my-package/**',
157-
'var/plugins/**',
158-
'storage/custom/**',
159-
],
160-
]);
161-
162-
expect(PathMatcher::mayPathBeIncluded('vendor/my-org/my-package/src/Service.php'))->toBeTrue()
163-
->and(PathMatcher::mayPathBeIncluded('/var/www/var/plugins/Plugin.php'))->toBeTrue()
164-
->and(PathMatcher::mayPathBeIncluded('storage/custom/Handler.php'))->toBeTrue()
165-
->and(PathMatcher::mayPathBeIncluded('src/App/Controller.php'))->toBeTrue()
166-
;
157+
try {
158+
Config::set([
159+
'include' => [
160+
'src/**',
161+
'vendor/my-org/my-package/**',
162+
'var/plugins/**',
163+
'storage/custom/**',
164+
],
165+
]);
166+
167+
expect(PathMatcher::mayPathBeIncluded('vendor/my-org/my-package/src/Service.php'))->toBeTrue()
168+
->and(PathMatcher::mayPathBeIncluded('/var/www/var/plugins/Plugin.php'))->toBeTrue()
169+
->and(PathMatcher::mayPathBeIncluded('storage/custom/Handler.php'))->toBeTrue()
170+
->and(PathMatcher::mayPathBeIncluded('src/App/Controller.php'))->toBeTrue()
171+
;
172+
} finally {
173+
Config::reset();
174+
}
167175
});
168176
});
169177

@@ -274,4 +282,4 @@
274282
expect(true)->toBeTrue();
275283
});
276284
});
277-
});
285+
});

tests/Internal/StreamWrapperTest.php

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ function testIgnoredFileFunc(int $id): int
164164
});
165165

166166
describe('stream_open() Fast-Paths & Whitelist Preservation', function () {
167+
afterEach(function () {
168+
Config::reset();
169+
});
170+
167171
test('bypasses AST transformation on non-PHP files', function () {
168172
$wrapper = new StreamWrapper();
169173
$openedPath = null;
@@ -178,67 +182,83 @@ function testIgnoredFileFunc(int $id): int
178182
});
179183

180184
test('bypasses AST transformation for unwhitelisted vendor files', function () {
181-
Config::set([
182-
'include' => ['src/**'],
183-
'exclude' => ['vendor/**'],
184-
]);
185-
186-
$projectRoot = str_replace('\\', '/', Config::getProjectRoot());
187-
$vendorFile = $projectRoot . '/vendor/composer/autoload_real.php';
188-
189-
if (file_exists($vendorFile)) {
190-
$wrapper = new StreamWrapper();
191-
$openedPath = null;
192-
$success = $wrapper->stream_open($vendorFile, 'r', 0, $openedPath);
193-
194-
expect($success)->toBeTrue();
195-
$wrapper->stream_close();
185+
try {
186+
Config::set([
187+
'include' => ['src/**'],
188+
'exclude' => ['vendor/**'],
189+
]);
190+
191+
$projectRoot = str_replace('\\', '/', Config::getProjectRoot());
192+
$vendorFile = $projectRoot . '/vendor/composer/autoload_real.php';
193+
194+
if (file_exists($vendorFile)) {
195+
$wrapper = new StreamWrapper();
196+
$openedPath = null;
197+
$success = $wrapper->stream_open($vendorFile, 'r', 0, $openedPath);
198+
199+
expect($success)->toBeTrue();
200+
$wrapper->stream_close();
201+
}
202+
} finally {
203+
Config::reset();
196204
}
197205
});
198206

199207
test('transforms whitelisted vendor files when explicitly included in config', function () {
200-
Config::set([
201-
'include' => [
202-
'src/**',
203-
'vendor/monolog/monolog/src/Monolog/Logger.php',
204-
],
205-
'exclude' => [
206-
'vendor/**',
207-
],
208-
]);
209-
210-
$projectRoot = str_replace('\\', '/', Config::getProjectRoot());
211-
$whitelistedVendorFile = $projectRoot . '/vendor/monolog/monolog/src/Monolog/Logger.php';
212-
213-
expect(FileFilter::isFileExcluded($whitelistedVendorFile))->toBeFalse();
208+
try {
209+
Config::set([
210+
'include' => [
211+
'src/**',
212+
'vendor/monolog/monolog/src/Monolog/Logger.php',
213+
],
214+
'exclude' => [
215+
'vendor/**',
216+
],
217+
]);
218+
219+
$projectRoot = str_replace('\\', '/', Config::getProjectRoot());
220+
$whitelistedVendorFile = $projectRoot . '/vendor/monolog/monolog/src/Monolog/Logger.php';
221+
222+
expect(FileFilter::isFileExcluded($whitelistedVendorFile))->toBeFalse();
223+
} finally {
224+
Config::reset();
225+
}
214226
});
215227
});
216228

217229
describe('Vendor Subpackage Isolation', function () {
230+
afterEach(function () {
231+
Config::reset();
232+
});
233+
218234
test('strictly isolates vendor files with nested src directories when application includes specific src subpackages', function () {
219-
Config::set([
220-
'include' => [
221-
'src/**',
222-
'src/Core/**',
223-
'src/Storefront/**',
224-
'src/Administration/**',
225-
],
226-
'exclude' => [
227-
'vendor/**',
228-
'storage/**',
229-
'var/**',
230-
'cache/**',
231-
],
232-
]);
233-
234-
$projectRoot = Config::getProjectRoot();
235-
236-
$vendorFile = str_replace('\\', '/', $projectRoot . '/vendor/doctrine/dbal/src/Core/Table.php');
237-
$appFile = str_replace('\\', '/', $projectRoot . '/src/Core/Framework/Util.php');
238-
239-
expect(FileFilter::isFileExcluded($vendorFile))->toBeTrue()
240-
->and(FileFilter::isFileExcluded($appFile))->toBeFalse()
241-
;
235+
try {
236+
Config::set([
237+
'include' => [
238+
'src/**',
239+
'src/Core/**',
240+
'src/Storefront/**',
241+
'src/Administration/**',
242+
],
243+
'exclude' => [
244+
'vendor/**',
245+
'storage/**',
246+
'var/**',
247+
'cache/**',
248+
],
249+
]);
250+
251+
$projectRoot = Config::getProjectRoot();
252+
253+
$vendorFile = str_replace('\\', '/', $projectRoot . '/vendor/doctrine/dbal/src/Core/Table.php');
254+
$appFile = str_replace('\\', '/', $projectRoot . '/src/Core/Framework/Util.php');
255+
256+
expect(FileFilter::isFileExcluded($vendorFile))->toBeTrue()
257+
->and(FileFilter::isFileExcluded($appFile))->toBeFalse()
258+
;
259+
} finally {
260+
Config::reset();
261+
}
242262
});
243263
});
244-
});
264+
});

0 commit comments

Comments
 (0)