11<?php namespace CodeIgniter \Autoloader ;
22
33use Config \Autoload ;
4- use Tests \Support \Autoloader \MockAutoloader ;
5-
6- //--------------------------------------------------------------------
74
85class AutoloaderTest extends \CIUnitTestCase
96{
10-
7+ /**
8+ * @var \CodeIgniter\Autoloader\Autoloader
9+ */
1110 protected $ loader ;
1211
12+ protected $ filesPath = SUPPORTPATH . 'Autoloader/ ' ;
13+
1314 //--------------------------------------------------------------------
1415
1516 public function setUp ()
@@ -19,27 +20,35 @@ public function setUp()
1920 $ config = new Autoload ();
2021
2122 $ config ->classmap = [
22- 'FirstClass ' => '/app/dir/First.php ' ,
23- 'Name\Spaced\Class ' => '/app/namespace/Class.php ' ,
23+ 'UnnamespacedClass ' => SUPPORTPATH . 'Autoloader/UnnamespacedClass.php ' ,
24+ 'OtherClass ' => APPPATH . 'Controllers/Home.php ' ,
25+ 'Name\Spaced\Class ' => APPPATH . 'Controllers/Home.php ' ,
2426 ];
2527 $ config ->psr4 = [
26- 'App\Controllers ' => ' /application/Controllers ' ,
27- 'App\Libraries ' => ' /application/somewhere ' ,
28+ 'App ' => APPPATH ,
29+ 'CodeIgniter ' => BASEPATH ,
2830 ];
2931
30- $ this ->loader = new MockAutoloader ();
31- $ this ->loader ->initialize ($ config );
32+ //\var_dump($config);exit;
33+
34+ $ this ->loader = new Autoloader ();
35+ $ this ->loader ->initialize ($ config )->register ();
36+ }
37+
38+ public function testLoadStoredClass ()
39+ {
40+ $ this ->assertInstanceOf ('UnnamespacedClass ' , new \UnnamespacedClass ());
41+ }
42+
43+ public function testInitializeWithInvalidArguments ()
44+ {
45+ $ this ->expectException (\InvalidArgumentException::class);
46+
47+ $ config = new Autoload ();
48+ $ config ->classmap = [];
49+ $ config ->psr4 = [];
3250
33- $ this ->loader ->setFiles ([
34- '/application/Controllers/Classname.php ' ,
35- '/application/somewhere/Classname.php ' ,
36- '/app/dir/First.php ' ,
37- '/app/namespace/Class.php ' ,
38- '/my/app/Class.php ' ,
39- APPPATH . 'Libraries/someLibrary.php ' ,
40- APPPATH . 'Models/someModel.php ' ,
41- APPPATH . 'Models/Some/CoolModel.php ' ,
42- ]);
51+ (new Autoloader ())->initialize ($ config );
4352 }
4453
4554 //--------------------------------------------------------------------
@@ -75,30 +84,30 @@ public function testServiceAutoLoader()
7584
7685 public function testExistingFile ()
7786 {
78- $ actual = $ this ->loader ->loadClass ('App\Controllers\Classname ' );
79- $ expected = ' /application/ Controllers/Classname .php ' ;
87+ $ actual = $ this ->loader ->loadClass ('App\Controllers\Home ' );
88+ $ expected = APPPATH . ' Controllers/Home .php ' ;
8089 $ this ->assertSame ($ expected , $ actual );
8190
82- $ actual = $ this ->loader ->loadClass ('App\Libraries\Classname ' );
83- $ expected = ' /application/somewhere/Classname .php ' ;
91+ $ actual = $ this ->loader ->loadClass ('CodeIgniter\Helpers\array_helper ' );
92+ $ expected = BASEPATH . ' Helpers/array_helper .php ' ;
8493 $ this ->assertSame ($ expected , $ actual );
8594 }
8695
8796 //--------------------------------------------------------------------
8897
8998 public function testMatchesWithPreceedingSlash ()
9099 {
91- $ actual = $ this ->loader ->loadClass ('\App\Controllers\Classname ' );
92- $ expected = ' /application/ Controllers/Classname .php ' ;
100+ $ actual = $ this ->loader ->loadClass ('\App\Controllers\Home ' );
101+ $ expected = APPPATH . ' Controllers/Home .php ' ;
93102 $ this ->assertSame ($ expected , $ actual );
94103 }
95104
96105 //--------------------------------------------------------------------
97106
98107 public function testMatchesWithFileExtension ()
99108 {
100- $ actual = $ this ->loader ->loadClass ('\App\Controllers\Classname .php ' );
101- $ expected = ' /application/ Controllers/Classname .php ' ;
109+ $ actual = $ this ->loader ->loadClass ('\App\Controllers\Home .php ' );
110+ $ expected = APPPATH . ' Controllers/Home .php ' ;
102111 $ this ->assertSame ($ expected , $ actual );
103112 }
104113
@@ -122,73 +131,70 @@ public function testInitializeException()
122131 $ config ->classmap = [];
123132 $ config ->psr4 = [];
124133
125- $ this ->loader = new MockAutoloader ();
134+ $ this ->loader = new Autoloader ();
126135 $ this ->loader ->initialize ($ config );
127136 }
128137
129138 public function testAddNamespaceWorks ()
130139 {
131140 $ this ->assertFalse ($ this ->loader ->loadClass ('My\App\Class ' ));
132141
133- $ this ->loader ->addNamespace ('My\App ' , ' /my/app ' );
142+ $ this ->loader ->addNamespace ('My\App ' , __DIR__ );
134143
135- $ actual = $ this ->loader ->loadClass ('My\App\Class ' );
136- $ expected = ' /my/app/Class.php ' ;
144+ $ actual = $ this ->loader ->loadClass ('My\App\AutoloaderTest ' );
145+ $ expected = __FILE__ ;
137146
138147 $ this ->assertSame ($ expected , $ actual );
139148 }
140149
141150 public function testAddNamespaceMultiplePathsWorks ()
142151 {
143- $ this ->loader ->addNamespace ('My\App ' , '/my/app ' );
144- $ this ->loader ->addNamespace ('My\App ' , '/test/app ' );
145- $ this ->loader ->setFiles ([
146- '/my/app/Class.php ' ,
147- '/test/app/ClassTest.php ' ,
148- ]);
152+ $ this ->loader ->addNamespace ('My\App ' , APPPATH . 'Config ' );
153+ $ this ->loader ->addNamespace ('My\App ' , __DIR__ );
149154
150- $ actual = $ this ->loader ->loadClass ('My\App\ClassTest ' );
151- $ expected = ' /test/app/ClassTest .php ' ;
155+ $ actual = $ this ->loader ->loadClass ('My\App\App ' );
156+ $ expected = APPPATH . ' Config/App .php ' ;
152157 $ this ->assertSame ($ expected , $ actual );
153158
154- $ actual = $ this ->loader ->loadClass ('My\App\Class ' );
155- $ expected = ' /my/app/Class.php ' ;
159+ $ actual = $ this ->loader ->loadClass ('My\App\AutoloaderTest ' );
160+ $ expected = __FILE__ ;
156161 $ this ->assertSame ($ expected , $ actual );
157162 }
158163
159164 public function testAddNamespaceStingToArray ()
160165 {
161- $ this ->loader ->addNamespace ('App\Controllers ' , ' /application/Controllers ' );
166+ $ this ->loader ->addNamespace ('App\Controllers ' , __DIR__ );
162167
163- $ this ->assertSame ('/application/Controllers/Classname.php ' , $ this ->loader ->loadClass ('App\Controllers\Classname ' ));
168+ $ this ->assertSame (
169+ __FILE__ ,
170+ $ this ->loader ->loadClass ('App\Controllers\AutoloaderTest ' )
171+ );
164172 }
165173
166174 //--------------------------------------------------------------------
167175
168176 public function testRemoveNamespace ()
169177 {
170- $ this ->loader ->addNamespace ('My\App ' , ' /my/app ' );
171- $ this ->assertSame (' /my/app/Class.php ' , $ this ->loader ->loadClass ('My\App\Class ' ));
178+ $ this ->loader ->addNamespace ('My\App ' , __DIR__ );
179+ $ this ->assertSame (__FILE__ , $ this ->loader ->loadClass ('My\App\AutoloaderTest ' ));
172180
173181 $ this ->loader ->removeNamespace ('My\App ' );
174- $ this ->assertFalse ((bool ) $ this ->loader ->loadClass ('My\App\Class ' ));
182+ $ this ->assertFalse ((bool ) $ this ->loader ->loadClass ('My\App\AutoloaderTest ' ));
175183 }
176184
177185 //--------------------------------------------------------------------
178186
179187 public function testLoadLegacy ()
180188 {
181189 // should not be able to find a folder
182- $ this ->assertFalse ((bool ) $ this ->loader ->loadClass ('someLibraries ' ));
183- // should be able to find these because we said so in the MockAutoloader
184- $ this ->assertTrue ((bool ) $ this ->loader ->loadClass ('someLibrary ' ));
185- $ this ->assertTrue ((bool ) $ this ->loader ->loadClass ('someModel ' ));
190+ $ this ->assertFalse ((bool ) $ this ->loader ->loadClass (__DIR__ ));
191+ // should be able to find these because we said so in the Autoloader
192+ $ this ->assertTrue ((bool ) $ this ->loader ->loadClass ('Home ' ));
186193 // should not be able to find these - don't exist
187194 $ this ->assertFalse ((bool ) $ this ->loader ->loadClass ('anotherLibrary ' ));
188195 $ this ->assertFalse ((bool ) $ this ->loader ->loadClass ('\nester\anotherLibrary ' ));
189- $ this ->assertFalse ((bool ) $ this ->loader ->loadClass ('\Shouldnt\Find\This ' ));
190196 // should not be able to find these legacy classes - namespaced
191- $ this ->assertFalse ($ this ->loader ->loadClass ('\Some\CoolModel ' ));
197+ $ this ->assertFalse ($ this ->loader ->loadClass ('Controllers\Home ' ));
192198 }
193199
194200 //--------------------------------------------------------------------
0 commit comments