66use TypePHP \Internal \Config ;
77
88describe ('FileFilter Unit Tests ' , function () {
9+ beforeEach (function () {
10+ Config::reset ();
11+ });
12+
13+ afterEach (function () {
14+ Config::reset ();
15+ });
16+
917 test ('returns false for null, empty, or false file paths ' , function () {
1018 expect (FileFilter::isFileExcluded (null ))->toBeFalse ()
1119 ->and (FileFilter::isFileExcluded ('' ))->toBeFalse ()
2230 });
2331
2432 test ('excludes storage and cache paths matching default config patterns ' , function () {
25- Config::reset ();
26-
2733 $ storagePath = str_replace ('\\' , '/ ' , getcwd () . '/storage/framework/views/cache.php ' );
2834 $ varPath = str_replace ('\\' , '/ ' , getcwd () . '/var/cache/test.php ' );
2935
4248 });
4349
4450 test ('allows specific vendor package when included with a more specific pattern ' , function () {
45- Config::set ([
46- 'include ' => [
47- 'src/** ' ,
48- 'vendor/my-company/whitelisted-package/** ' ,
49- ],
50- 'exclude ' => [
51- 'vendor/** ' ,
52- ],
53- ]);
54-
55- $ whitelistedPath = str_replace ('\\' , '/ ' , getcwd () . '/vendor/my-company/whitelisted-package/src/Service.php ' );
56- $ otherVendorPath = str_replace ('\\' , '/ ' , getcwd () . '/vendor/guzzlehttp/guzzle/src/Client.php ' );
57-
58- expect (FileFilter::isFileExcluded ($ whitelistedPath ))->toBeFalse ()
59- ->and (FileFilter::isFileExcluded ($ otherVendorPath ))->toBeTrue ()
60- ;
61-
62- Config::reset ();
51+ try {
52+ Config::set ([
53+ 'include ' => [
54+ 'src/** ' ,
55+ 'vendor/my-company/whitelisted-package/** ' ,
56+ ],
57+ 'exclude ' => [
58+ 'vendor/** ' ,
59+ ],
60+ ]);
61+
62+ $ whitelistedPath = str_replace ('\\' , '/ ' , getcwd () . '/vendor/my-company/whitelisted-package/src/Service.php ' );
63+ $ otherVendorPath = str_replace ('\\' , '/ ' , getcwd () . '/vendor/guzzlehttp/guzzle/src/Client.php ' );
64+
65+ expect (FileFilter::isFileExcluded ($ whitelistedPath ))->toBeFalse ()
66+ ->and (FileFilter::isFileExcluded ($ otherVendorPath ))->toBeTrue ()
67+ ;
68+ } finally {
69+ Config::reset ();
70+ }
6371 });
6472
6573 test ('allows including or excluding single specific files ' , function () {
66- Config::set ([
67- 'include ' => [
68- 'src/** ' ,
69- 'vendor/monolog/monolog/src/Monolog/Logger.php ' ,
70- ],
71- 'exclude ' => [
72- 'src/Legacy/UnsafeFile.php ' ,
73- 'vendor/** ' ,
74- ],
75- ]);
76-
77- $ normalSrc = str_replace ('\\' , '/ ' , getcwd () . '/src/TypePHP.php ' );
78- $ excludedSingleFile = str_replace ('\\' , '/ ' , getcwd () . '/src/Legacy/UnsafeFile.php ' );
79- $ includedSingleVendorFile = str_replace ('\\' , '/ ' , getcwd () . '/vendor/monolog/monolog/src/Monolog/Logger.php ' );
80- $ otherVendorFile = str_replace ('\\' , '/ ' , getcwd () . '/vendor/monolog/monolog/src/Monolog/Formatter.php ' );
81-
82- expect (FileFilter::isFileExcluded ($ normalSrc ))->toBeFalse ()
83- ->and (FileFilter::isFileExcluded ($ excludedSingleFile ))->toBeTrue ()
84- ->and (FileFilter::isFileExcluded ($ includedSingleVendorFile ))->toBeFalse ()
85- ->and (FileFilter::isFileExcluded ($ otherVendorFile ))->toBeTrue ()
86- ;
87-
88- Config::reset ();
74+ try {
75+ Config::set ([
76+ 'include ' => [
77+ 'src/** ' ,
78+ 'vendor/monolog/monolog/src/Monolog/Logger.php ' ,
79+ ],
80+ 'exclude ' => [
81+ 'src/Legacy/UnsafeFile.php ' ,
82+ 'vendor/** ' ,
83+ ],
84+ ]);
85+
86+ $ normalSrc = str_replace ('\\' , '/ ' , getcwd () . '/src/TypePHP.php ' );
87+ $ excludedSingleFile = str_replace ('\\' , '/ ' , getcwd () . '/src/Legacy/UnsafeFile.php ' );
88+ $ includedSingleVendorFile = str_replace ('\\' , '/ ' , getcwd () . '/vendor/monolog/monolog/src/Monolog/Logger.php ' );
89+ $ otherVendorFile = str_replace ('\\' , '/ ' , getcwd () . '/vendor/monolog/monolog/src/Monolog/Formatter.php ' );
90+
91+ expect (FileFilter::isFileExcluded ($ normalSrc ))->toBeFalse ()
92+ ->and (FileFilter::isFileExcluded ($ excludedSingleFile ))->toBeTrue ()
93+ ->and (FileFilter::isFileExcluded ($ includedSingleVendorFile ))->toBeFalse ()
94+ ->and (FileFilter::isFileExcluded ($ otherVendorFile ))->toBeTrue ()
95+ ;
96+ } finally {
97+ Config::reset ();
98+ }
8999 });
90100
91101 test ('excludes non-PHP files automatically ' , function () {
96106 });
97107
98108 test ('excludes a specific single file inside an explicitly included directory ' , function () {
99- Config::set ([
100- 'include ' => [
101- 'app/Services/** ' ,
102- ],
103- 'exclude ' => [
104- 'app/Services/LegacyService.php ' ,
105- ],
106- ]);
107-
108- $ normalService = str_replace ('\\' , '/ ' , getcwd () . '/app/Services/UserService.php ' );
109- $ excludedService = str_replace ('\\' , '/ ' , getcwd () . '/app/Services/LegacyService.php ' );
110-
111- expect (FileFilter::isFileExcluded ($ normalService ))->toBeFalse ()
112- ->and (FileFilter::isFileExcluded ($ excludedService ))->toBeTrue ()
113- ;
114-
115- Config::reset ();
109+ try {
110+ Config::set ([
111+ 'include ' => [
112+ 'app/Services/** ' ,
113+ ],
114+ 'exclude ' => [
115+ 'app/Services/LegacyService.php ' ,
116+ ],
117+ ]);
118+
119+ $ normalService = str_replace ('\\' , '/ ' , getcwd () . '/app/Services/UserService.php ' );
120+ $ excludedService = str_replace ('\\' , '/ ' , getcwd () . '/app/Services/LegacyService.php ' );
121+
122+ expect (FileFilter::isFileExcluded ($ normalService ))->toBeFalse ()
123+ ->and (FileFilter::isFileExcluded ($ excludedService ))->toBeTrue ()
124+ ;
125+ } finally {
126+ Config::reset ();
127+ }
116128 });
117129
118130 test ('allows including entire working directory using double asterisk glob ' , function () {
119- Config::set ([
120- 'include ' => [
121- '** ' ,
122- ],
123- 'exclude ' => [
124- 'vendor/** ' ,
125- 'storage/** ' ,
126- ],
127- ]);
128-
129- $ rootPhpFile = str_replace ('\\' , '/ ' , getcwd () . '/index.php ' );
130- $ deepPhpFile = str_replace ('\\' , '/ ' , getcwd () . '/app/Http/Controllers/UserController.php ' );
131- $ vendorPhpFile = str_replace ('\\' , '/ ' , getcwd () . '/vendor/composer/autoload.php ' );
132-
133- expect (FileFilter::isFileExcluded ($ rootPhpFile ))->toBeFalse ()
134- ->and (FileFilter::isFileExcluded ($ deepPhpFile ))->toBeFalse ()
135- ->and (FileFilter::isFileExcluded ($ vendorPhpFile ))->toBeTrue ()
136- ;
137-
138- Config::reset ();
131+ try {
132+ Config::set ([
133+ 'include ' => [
134+ '** ' ,
135+ ],
136+ 'exclude ' => [
137+ 'vendor/** ' ,
138+ 'storage/** ' ,
139+ ],
140+ ]);
141+
142+ $ rootPhpFile = str_replace ('\\' , '/ ' , getcwd () . '/index.php ' );
143+ $ deepPhpFile = str_replace ('\\' , '/ ' , getcwd () . '/app/Http/Controllers/UserController.php ' );
144+ $ vendorPhpFile = str_replace ('\\' , '/ ' , getcwd () . '/vendor/composer/autoload.php ' );
145+
146+ expect (FileFilter::isFileExcluded ($ rootPhpFile ))->toBeFalse ()
147+ ->and (FileFilter::isFileExcluded ($ deepPhpFile ))->toBeFalse ()
148+ ->and (FileFilter::isFileExcluded ($ vendorPhpFile ))->toBeTrue ()
149+ ;
150+ } finally {
151+ Config::reset ();
152+ }
139153 });
140154
141155 test ('does not exclude cached files from contract docblock extraction ' , function () {
142- $ customCacheDir = getcwd () . '/storage/typephp-cache ' ;
143-
144- Config::set ([
145- 'cache_dir ' => $ customCacheDir ,
146- 'include ' => [
147- '** ' ,
148- ],
149- 'exclude ' => [],
150- ]);
151-
152- $ cachedFilePath = str_replace ('\\' , '/ ' , $ customCacheDir . '/v0.1_hash123.php ' );
153- $ normalFilePath = str_replace ('\\' , '/ ' , getcwd () . '/app/Models/User.php ' );
154-
155- expect (FileFilter::isFileExcluded ($ cachedFilePath ))->toBeFalse ()
156- ->and (FileFilter::isFileExcluded ($ normalFilePath ))->toBeFalse ()
157- ;
158-
159- Config::reset ();
156+ try {
157+ $ customCacheDir = getcwd () . '/storage/typephp-cache ' ;
158+
159+ Config::set ([
160+ 'cache_dir ' => $ customCacheDir ,
161+ 'include ' => [
162+ '** ' ,
163+ ],
164+ 'exclude ' => [],
165+ ]);
166+
167+ $ cachedFilePath = str_replace ('\\' , '/ ' , $ customCacheDir . '/v0.1_hash123.php ' );
168+ $ normalFilePath = str_replace ('\\' , '/ ' , getcwd () . '/app/Models/User.php ' );
169+
170+ expect (FileFilter::isFileExcluded ($ cachedFilePath ))->toBeFalse ()
171+ ->and (FileFilter::isFileExcluded ($ normalFilePath ))->toBeFalse ()
172+ ;
173+ } finally {
174+ Config::reset ();
175+ }
160176 });
161- });
177+ });
0 commit comments