1212namespace CodeIgniter \Autoloader ;
1313
1414use App \Controllers \Home ;
15+ use Closure ;
1516use CodeIgniter \Exceptions \ConfigException ;
1617use CodeIgniter \Test \CIUnitTestCase ;
18+ use CodeIgniter \Test \ReflectionHelper ;
1719use Config \Autoload ;
1820use Config \Modules ;
1921use Config \Services ;
2022use InvalidArgumentException ;
23+ use PHPUnit \Framework \MockObject \MockObject ;
2124use RuntimeException ;
2225use UnnamespacedClass ;
2326
2831 */
2932final class AutoloaderTest extends CIUnitTestCase
3033{
34+ use ReflectionHelper;
35+
3136 private Autoloader $ loader ;
37+ private Closure $ classLoader ;
3238
3339 protected function setUp (): void
3440 {
@@ -50,13 +56,15 @@ protected function setUp(): void
5056
5157 $ this ->loader = new Autoloader ();
5258 $ this ->loader ->initialize ($ config , $ modules )->register ();
59+
60+ $ this ->classLoader = $ this ->getPrivateMethodInvoker ($ this ->loader , 'loadInNamespace ' );
5361 }
5462
5563 protected function tearDown (): void
5664 {
57- $ this ->loader ->unregister ();
58-
5965 parent ::tearDown ();
66+
67+ $ this ->loader ->unregister ();
6068 }
6169
6270 public function testLoadStoredClass (): void
@@ -96,9 +104,10 @@ public function testInitializeTwice(): void
96104
97105 public function testServiceAutoLoaderFromShareInstances (): void
98106 {
99- $ autoloader = Services::autoloader ();
107+ $ classLoader = $ this ->getPrivateMethodInvoker (Services::autoloader (), 'loadInNamespace ' );
108+
100109 // look for Home controller, as that should be in base repo
101- $ actual = $ autoloader -> loadClass (Home::class);
110+ $ actual = $ classLoader (Home::class);
102111 $ expected = APPPATH . 'Controllers ' . DIRECTORY_SEPARATOR . 'Home.php ' ;
103112 $ this ->assertSame ($ expected , realpath ($ actual ) ?: $ actual );
104113 }
@@ -109,8 +118,10 @@ public function testServiceAutoLoader(): void
109118 $ autoloader ->initialize (new Autoload (), new Modules ());
110119 $ autoloader ->register ();
111120
121+ $ classLoader = $ this ->getPrivateMethodInvoker ($ autoloader , 'loadInNamespace ' );
122+
112123 // look for Home controller, as that should be in base repo
113- $ actual = $ autoloader -> loadClass (Home::class);
124+ $ actual = $ classLoader (Home::class);
114125 $ expected = APPPATH . 'Controllers ' . DIRECTORY_SEPARATOR . 'Home.php ' ;
115126 $ this ->assertSame ($ expected , realpath ($ actual ) ?: $ actual );
116127
@@ -119,41 +130,43 @@ public function testServiceAutoLoader(): void
119130
120131 public function testExistingFile (): void
121132 {
122- $ actual = $ this ->loader -> loadClass (Home::class);
133+ $ actual = ( $ this ->classLoader ) (Home::class);
123134 $ expected = APPPATH . 'Controllers ' . DIRECTORY_SEPARATOR . 'Home.php ' ;
124135 $ this ->assertSame ($ expected , $ actual );
125136
126- $ actual = $ this ->loader -> loadClass ('CodeIgniter\Helpers\array_helper ' );
137+ $ actual = ( $ this ->classLoader ) ('CodeIgniter\Helpers\array_helper ' );
127138 $ expected = SYSTEMPATH . 'Helpers ' . DIRECTORY_SEPARATOR . 'array_helper.php ' ;
128139 $ this ->assertSame ($ expected , $ actual );
129140 }
130141
131142 public function testMatchesWithPrecedingSlash (): void
132143 {
133- $ actual = $ this ->loader -> loadClass (Home::class);
144+ $ actual = ( $ this ->classLoader ) (Home::class);
134145 $ expected = APPPATH . 'Controllers ' . DIRECTORY_SEPARATOR . 'Home.php ' ;
135146 $ this ->assertSame ($ expected , $ actual );
136147 }
137148
138149 public function testMatchesWithFileExtension (): void
139150 {
140- $ actual = $ this ->loader ->loadClass ('\App\Controllers\Home.php ' );
141- $ expected = APPPATH . 'Controllers ' . DIRECTORY_SEPARATOR . 'Home.php ' ;
142- $ this ->assertSame ($ expected , $ actual );
151+ /** @var Autoloader&MockObject $classLoader */
152+ $ classLoader = $ this ->getMockBuilder (Autoloader::class)->onlyMethods (['loadInNamespace ' ])->getMock ();
153+ $ classLoader ->expects ($ this ->once ())->method ('loadInNamespace ' )->with (Home::class);
154+
155+ $ classLoader ->loadClass ('\App\Controllers\Home.php ' );
143156 }
144157
145158 public function testMissingFile (): void
146159 {
147- $ this ->assertFalse ($ this ->loader -> loadClass ('\App\Missing\Classname ' ));
160+ $ this ->assertFalse (( $ this ->classLoader ) ('\App\Missing\Classname ' ));
148161 }
149162
150163 public function testAddNamespaceWorks (): void
151164 {
152- $ this ->assertFalse ($ this ->loader -> loadClass ('My\App\Class ' ));
165+ $ this ->assertFalse (( $ this ->classLoader ) ('My\App\Class ' ));
153166
154167 $ this ->loader ->addNamespace ('My\App ' , __DIR__ );
155168
156- $ actual = $ this ->loader -> loadClass ('My\App\AutoloaderTest ' );
169+ $ actual = ( $ this ->classLoader ) ('My\App\AutoloaderTest ' );
157170 $ expected = __FILE__ ;
158171
159172 $ this ->assertSame ($ expected , $ actual );
@@ -168,11 +181,11 @@ public function testAddNamespaceMultiplePathsWorks(): void
168181 ],
169182 ]);
170183
171- $ actual = $ this ->loader -> loadClass ('My\App\App ' );
184+ $ actual = ( $ this ->classLoader ) ('My\App\App ' );
172185 $ expected = APPPATH . 'Config ' . DIRECTORY_SEPARATOR . 'App.php ' ;
173186 $ this ->assertSame ($ expected , $ actual );
174187
175- $ actual = $ this ->loader -> loadClass ('My\App\AutoloaderTest ' );
188+ $ actual = ( $ this ->classLoader ) ('My\App\AutoloaderTest ' );
176189 $ expected = __FILE__ ;
177190 $ this ->assertSame ($ expected , $ actual );
178191 }
@@ -183,7 +196,7 @@ public function testAddNamespaceStringToArray(): void
183196
184197 $ this ->assertSame (
185198 __FILE__ ,
186- $ this ->loader -> loadClass ('App\Controllers\AutoloaderTest ' )
199+ ( $ this ->classLoader ) ('App\Controllers\AutoloaderTest ' )
187200 );
188201 }
189202
@@ -201,15 +214,15 @@ public function testGetNamespaceGivesArray(): void
201214 public function testRemoveNamespace (): void
202215 {
203216 $ this ->loader ->addNamespace ('My\App ' , __DIR__ );
204- $ this ->assertSame (__FILE__ , $ this ->loader -> loadClass ('My\App\AutoloaderTest ' ));
217+ $ this ->assertSame (__FILE__ , ( $ this ->classLoader ) ('My\App\AutoloaderTest ' ));
205218
206219 $ this ->loader ->removeNamespace ('My\App ' );
207- $ this ->assertFalse ((bool ) $ this ->loader -> loadClass ('My\App\AutoloaderTest ' ));
220+ $ this ->assertFalse (($ this ->classLoader ) ('My\App\AutoloaderTest ' ));
208221 }
209222
210223 public function testloadClassNonNamespaced (): void
211224 {
212- $ this ->assertFalse ($ this ->loader -> loadClass ('Modules ' ));
225+ $ this ->assertFalse (( $ this ->classLoader ) ('Modules ' ));
213226 }
214227
215228 public function testSanitizationContailsSpecialChars (): void
0 commit comments