Skip to content

Commit 9068e3b

Browse files
committed
Update FileLocatorTest
1 parent d25f49c commit 9068e3b

1 file changed

Lines changed: 87 additions & 65 deletions

File tree

Lines changed: 87 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,130 @@
11
<?php namespace CodeIgniter\Autoloader;
22

3-
use Tests\Support\Config\MockAutoload;
4-
use Tests\Support\Autoloader\MockFileLocator;
5-
63
class FileLocatorTest extends \CIUnitTestCase
74
{
8-
95
/**
10-
* @var MockFileLocator
6+
* @var \CodeIgniter\Autoloader\FileLocator
117
*/
12-
protected $loader;
8+
protected $locator;
139

1410
//--------------------------------------------------------------------
1511

1612
public function setUp()
1713
{
1814
parent::setUp();
1915

20-
$config = new MockAutoload();
21-
$config->psr4 = [
22-
'App\Libraries' => '/application/somewhere',
23-
'App' => '/application',
24-
'Sys' => BASEPATH,
25-
'Blog' => '/modules/blog',
16+
$autoloader = new Autoloader();
17+
$autoloader->initialize(new \Config\Autoload());
18+
$autoloader->addNamespace([
19+
'Unknown' => '/i/do/not/exist',
2620
'Tests/Support' => TESTPATH . '_support/',
27-
];
21+
'App' => APPPATH,
22+
'CodeIgniter' => [
23+
BASEPATH,
24+
TESTPATH,
25+
],
26+
'Errors' => APPPATH . 'Views/errors',
27+
'System' => SUPPORTPATH . 'Autoloader/system',
28+
]);
2829

29-
$this->loader = new MockFileLocator($config);
30+
$this->locator = new FileLocator($autoloader);
31+
}
3032

31-
$this->loader->setFiles([
32-
APPPATH . 'index.php',
33-
APPPATH . 'Views/index.php',
34-
APPPATH . 'Views/admin/users/create.php',
35-
'/modules/blog/Views/index.php',
36-
'/modules/blog/Views/admin/posts.php',
37-
]);
33+
//--------------------------------------------------------------------
34+
35+
public function testLocateFileWorksWithLegacyStructure()
36+
{
37+
$file = 'Controllers/Home';
38+
39+
$expected = APPPATH . 'Controllers/Home.php';
40+
41+
$this->assertEquals($expected, $this->locator->locateFile($file));
42+
}
43+
44+
//--------------------------------------------------------------------
45+
46+
public function testLocateFileWithLegacyStructureNotFound()
47+
{
48+
$file = 'Unknown';
49+
50+
$this->assertEquals('', $this->locator->locateFile($file));
3851
}
3952

4053
//--------------------------------------------------------------------
4154

4255
public function testLocateFileWorksInApplicationDirectory()
4356
{
44-
$file = 'index';
57+
$file = 'welcome_message';
4558

46-
$expected = APPPATH . 'Views/index.php';
59+
$expected = APPPATH . 'Views/welcome_message.php';
4760

48-
$this->assertEquals($expected, $this->loader->locateFile($file, 'Views'));
61+
$this->assertEquals($expected, $this->locator->locateFile($file, 'Views'));
4962
}
5063

5164
//--------------------------------------------------------------------
5265

5366
public function testLocateFileWorksInApplicationDirectoryWithoutFolder()
5467
{
55-
$file = 'index';
68+
$file = 'bootstrap';
5669

57-
$expected = APPPATH . 'index.php';
70+
$expected = BASEPATH . 'bootstrap.php';
5871

59-
$this->assertEquals($expected, $this->loader->locateFile($file));
72+
$this->assertEquals($expected, $this->locator->locateFile($file));
6073
}
6174

6275
//--------------------------------------------------------------------
6376

6477
public function testLocateFileWorksInNestedApplicationDirectory()
6578
{
66-
$file = 'admin/users/create';
79+
$file = 'Controllers/Home';
6780

68-
$expected = APPPATH . 'Views/admin/users/create.php';
81+
$expected = APPPATH . 'Controllers/Home.php';
6982

70-
$this->assertEquals($expected, $this->loader->locateFile($file, 'Views'));
83+
$this->assertEquals($expected, $this->locator->locateFile($file, 'Controllers'));
7184
}
7285

7386
//--------------------------------------------------------------------
7487

7588
public function testLocateFileReplacesFolderName()
7689
{
77-
$file = '\Blog\Views/admin/posts.php';
90+
$file = '\App\Views/errors/html/error_404.php';
7891

79-
$expected = '/modules/blog/Views/admin/posts.php';
92+
$expected = APPPATH . 'Views/errors/html/error_404.php';
8093

81-
$this->assertEquals($expected, $this->loader->locateFile($file, 'Views'));
94+
$this->assertEquals($expected, $this->locator->locateFile($file, 'Views'));
8295
}
8396

8497
//--------------------------------------------------------------------
8598

8699
public function testLocateFileReplacesFolderNameLegacy()
87100
{
88-
$file = 'Views/index.php';
101+
$file = 'Views/welcome_message.php';
89102

90-
$expected = APPPATH . 'Views/index.php';
103+
$expected = APPPATH . 'Views/welcome_message.php';
91104

92-
$this->assertEquals($expected, $this->loader->locateFile($file, 'Views'));
105+
$this->assertEquals($expected, $this->locator->locateFile($file, 'Views'));
93106
}
94107

95108
//--------------------------------------------------------------------
96109

97110
public function testLocateFileCanFindNamespacedView()
98111
{
99-
$file = '\Blog\index';
112+
$file = '\Errors\error_404';
100113

101-
$expected = '/modules/blog/Views/index.php';
114+
$expected = APPPATH . 'Views/errors/html/error_404.php';
102115

103-
$this->assertEquals($expected, $this->loader->locateFile($file, 'Views'));
116+
$this->assertEquals($expected, $this->locator->locateFile($file, 'html'));
104117
}
105118

106119
//--------------------------------------------------------------------
107120

108121
public function testLocateFileCanFindNestedNamespacedView()
109122
{
110-
$file = '\Blog\admin/posts.php';
123+
$file = '\Errors\html/error_404';
111124

112-
$expected = '/modules/blog/Views/admin/posts.php';
125+
$expected = APPPATH . 'Views/errors/html/error_404.php';
113126

114-
$this->assertEquals($expected, $this->loader->locateFile($file, 'Views'));
127+
$this->assertEquals($expected, $this->locator->locateFile($file, 'html'));
115128
}
116129

117130
//--------------------------------------------------------------------
@@ -120,16 +133,16 @@ public function testLocateFileReturnsEmptyWithBadNamespace()
120133
{
121134
$file = '\Blogger\admin/posts.php';
122135

123-
$this->assertEquals('', $this->loader->locateFile($file, 'Views'));
136+
$this->assertEquals('', $this->locator->locateFile($file, 'Views'));
124137
}
125138

126139
//--------------------------------------------------------------------
127140

128141
public function testSearchSimple()
129142
{
130-
$expected = rtrim(APPPATH, '/') . '/Config/App.php';
143+
$expected = APPPATH . 'Config/App.php';
131144

132-
$foundFiles = $this->loader->search('Config/App.php');
145+
$foundFiles = $this->locator->search('Config/App.php');
133146

134147
$this->assertEquals($expected, $foundFiles[0]);
135148
}
@@ -138,9 +151,9 @@ public function testSearchSimple()
138151

139152
public function testSearchWithFileExtension()
140153
{
141-
$expected = rtrim(APPPATH, '/') . '/Config/App.php';
154+
$expected = APPPATH . 'Config/App.php';
142155

143-
$foundFiles = $this->loader->search('Config/App', 'php');
156+
$foundFiles = $this->locator->search('Config/App', 'php');
144157

145158
$this->assertEquals($expected, $foundFiles[0]);
146159
}
@@ -149,20 +162,20 @@ public function testSearchWithFileExtension()
149162

150163
public function testSearchWithMultipleFilesFound()
151164
{
152-
$foundFiles = $this->loader->search('index', 'html');
165+
$foundFiles = $this->locator->search('index', 'html');
153166

154-
$expected = rtrim(APPPATH, '/') . '/index.html';
167+
$expected = APPPATH . 'index.html';
155168
$this->assertContains($expected, $foundFiles);
156169

157-
$expected = rtrim(BASEPATH, '/') . '/index.html';
170+
$expected = BASEPATH . 'index.html';
158171
$this->assertContains($expected, $foundFiles);
159172
}
160173

161174
//--------------------------------------------------------------------
162175

163176
public function testSearchForFileNotExist()
164177
{
165-
$foundFiles = $this->loader->search('Views/Fake.html');
178+
$foundFiles = $this->locator->search('Views/Fake.html');
166179

167180
$this->assertArrayNotHasKey(0, $foundFiles);
168181
}
@@ -171,7 +184,7 @@ public function testSearchForFileNotExist()
171184

172185
public function testListFilesSimple()
173186
{
174-
$files = $this->loader->listFiles('Config/');
187+
$files = $this->locator->listFiles('Config/');
175188

176189
$expectedWin = APPPATH . 'Config\App.php';
177190
$expectedLin = APPPATH . 'Config/App.php';
@@ -182,7 +195,7 @@ public function testListFilesSimple()
182195

183196
public function testListFilesWithFileAsInput()
184197
{
185-
$files = $this->loader->listFiles('Config/App.php');
198+
$files = $this->locator->listFiles('Config/App.php');
186199

187200
$this->assertEmpty($files);
188201
}
@@ -191,7 +204,7 @@ public function testListFilesWithFileAsInput()
191204

192205
public function testListFilesFromMultipleDir()
193206
{
194-
$files = $this->loader->listFiles('Filters/');
207+
$files = $this->locator->listFiles('Filters/');
195208

196209
$expectedWin = APPPATH . 'Filters\DebugToolbar.php';
197210
$expectedLin = APPPATH . 'Filters/DebugToolbar.php';
@@ -206,7 +219,7 @@ public function testListFilesFromMultipleDir()
206219

207220
public function testListFilesWithPathNotExist()
208221
{
209-
$files = $this->loader->listFiles('Fake/');
222+
$files = $this->locator->listFiles('Fake/');
210223

211224
$this->assertEmpty($files);
212225
}
@@ -215,38 +228,47 @@ public function testListFilesWithPathNotExist()
215228

216229
public function testListFilesWithoutPath()
217230
{
218-
$files = $this->loader->listFiles('');
231+
$files = $this->locator->listFiles('');
219232

220233
$this->assertEmpty($files);
221234
}
222235

223236
public function testFindQNameFromPathSimple()
224237
{
225-
$ClassName = $this->loader->findQualifiedNameFromPath('system/HTTP/Header.php');
226-
$expected = '\Sys\HTTP\Header';
238+
$ClassName = $this->locator->findQualifiedNameFromPath(BASEPATH . 'HTTP/Header.php');
239+
$expected = '\CodeIgniter\HTTP\Header';
227240

228241
$this->assertEquals($expected, $ClassName);
229242
}
230243

231-
public function testFindQNameFromPathWithNumericNamespace()
244+
public function testFindQNameFromPathWithFileNotExist()
232245
{
233-
$ClassName = $this->loader->findQualifiedNameFromPath('application/Config/App.php');
246+
$ClassName = $this->locator->findQualifiedNameFromPath('modules/blog/Views/index.php');
234247

235248
$this->assertNull($ClassName);
236249
}
237250

238-
public function testFindQNameFromPathWithFileNotExist()
251+
public function testFindQNameFromPathWithoutCorrespondingNamespace()
239252
{
240-
$ClassName = $this->loader->findQualifiedNameFromPath('modules/blog/Views/index.php');
253+
$ClassName = $this->locator->findQualifiedNameFromPath('/etc/hosts');
241254

242255
$this->assertNull($ClassName);
243256
}
244257

245-
public function testFindQNameFromPathWithoutCorrespondingNamespace()
258+
public function testGetClassNameFromClassFile()
246259
{
247-
$ClassName = $this->loader->findQualifiedNameFromPath('tests/system/CodeIgniterTest.php');
260+
$this->assertEquals(
261+
__CLASS__,
262+
$this->locator->getClassname(__FILE__)
263+
);
264+
}
248265

249-
$this->assertNull($ClassName);
266+
public function testGetClassNameFromNonClassFile()
267+
{
268+
$this->assertEquals(
269+
'',
270+
$this->locator->getClassname(BASEPATH . 'bootstrap.php')
271+
);
250272
}
251273

252274
}

0 commit comments

Comments
 (0)