Skip to content

Commit 2b04972

Browse files
authored
Merge pull request #7535 from kenjis/test-FileLocator
docs: improve doc comment and tests for locateFile()
2 parents 505041c + d8a1565 commit 2b04972

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

system/Autoloader/FileLocator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ public function __construct(Autoloader $autoloader)
3333
* Attempts to locate a file by examining the name for a namespace
3434
* and looking through the PSR-4 namespaced files that we know about.
3535
*
36-
* @param string $file The namespaced file to locate
37-
* @param string|null $folder The folder within the namespace that we should look for the file.
36+
* @param string $file The relative file path or namespaced file to
37+
* locate. If not namespaced, search in the app
38+
* folder.
39+
* @param string|null $folder The folder within the namespace that we should
40+
* look for the file. If $file does not contain
41+
* this value, it will be appended to the namespace
42+
* folder.
3843
* @param string $ext The file extension the file should have.
3944
*
4045
* @return false|string The path to the file, or false if not found.

tests/system/Autoloader/FileLocatorTest.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,64 +51,67 @@ protected function setUp(): void
5151
$this->locator = new FileLocator($autoloader);
5252
}
5353

54-
public function testLocateFileWorksWithLegacyStructure()
54+
public function testLocateFileNotNamespacedFindsInAppDirectory()
5555
{
56-
$file = 'Controllers/Home';
56+
$file = 'Controllers/Home'; // not namespaced
5757

5858
$expected = APPPATH . 'Controllers/Home.php';
5959

6060
$this->assertSame($expected, $this->locator->locateFile($file));
6161
}
6262

63-
public function testLocateFileWithLegacyStructureNotFound()
63+
public function testLocateFileNotNamespacedNotFound()
6464
{
65-
$file = 'Unknown';
65+
$file = 'Unknown'; // not namespaced
6666

6767
$this->assertFalse($this->locator->locateFile($file));
6868
}
6969

70-
public function testLocateFileWorksInApplicationDirectory()
70+
public function testLocateFileNotNamespacedFindsWithFolderInAppDirectory()
7171
{
72-
$file = 'welcome_message';
72+
$file = 'welcome_message'; // not namespaced
7373

7474
$expected = APPPATH . 'Views/welcome_message.php';
7575

7676
$this->assertSame($expected, $this->locator->locateFile($file, 'Views'));
7777
}
7878

79-
public function testLocateFileWorksInApplicationDirectoryWithoutFolder()
79+
public function testLocateFileNotNamespacedFindesWithoutFolderInAppDirectory()
8080
{
81-
$file = 'Common';
81+
$file = 'Common'; // not namespaced
8282

8383
$expected = APPPATH . 'Common.php';
8484

8585
$this->assertSame($expected, $this->locator->locateFile($file));
8686
}
8787

88-
public function testLocateFileWorksInNestedApplicationDirectory()
88+
public function testLocateFileNotNamespacedWorksInNestedAppDirectory()
8989
{
90-
$file = 'Controllers/Home';
90+
$file = 'Controllers/Home'; // not namespaced
9191

9292
$expected = APPPATH . 'Controllers/Home.php';
9393

94+
// This works because $file contains `Controllers`.
9495
$this->assertSame($expected, $this->locator->locateFile($file, 'Controllers'));
9596
}
9697

97-
public function testLocateFileReplacesFolderName()
98+
public function testLocateFileWithFolderNameInFile()
9899
{
99100
$file = '\App\Views/errors/html/error_404.php';
100101

101102
$expected = APPPATH . 'Views/errors/html/error_404.php';
102103

104+
// This works because $file contains `Views`.
103105
$this->assertSame($expected, $this->locator->locateFile($file, 'Views'));
104106
}
105107

106-
public function testLocateFileReplacesFolderNameLegacy()
108+
public function testLocateFileNotNamespacedWithFolderNameInFile()
107109
{
108-
$file = 'Views/welcome_message.php';
110+
$file = 'Views/welcome_message.php'; // not namespaced
109111

110112
$expected = APPPATH . 'Views/welcome_message.php';
111113

114+
// This works because $file contains `Views`.
112115
$this->assertSame($expected, $this->locator->locateFile($file, 'Views'));
113116
}
114117

@@ -118,6 +121,7 @@ public function testLocateFileCanFindNamespacedView()
118121

119122
$expected = APPPATH . 'Views/errors/html/error_404.php';
120123

124+
// The namespace `Errors` (APPPATH . 'Views/errors') + the folder (`html`) + `error_404`
121125
$this->assertSame($expected, $this->locator->locateFile($file, 'html'));
122126
}
123127

0 commit comments

Comments
 (0)