|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of Blitz PHP framework. |
| 5 | + * |
| 6 | + * (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use BlitzPHP\Autoloader\Autoloader; |
| 13 | +use BlitzPHP\Autoloader\Locator; |
| 14 | +use BlitzPHP\Enums\Method; |
| 15 | +use Spec\BlitzPHP\App\Controllers\RestController; |
| 16 | + |
| 17 | +use function Kahlan\expect; |
| 18 | + |
| 19 | +describe('Autoloader / Locator', function() { |
| 20 | + beforeEach(function() { |
| 21 | + $this->autoloader = new Autoloader(config('autoload')); |
| 22 | + $this->autoloader->initialize(); |
| 23 | + $this->autoloader->addNamespace([ |
| 24 | + 'Unknown' => '/i/do/not/exist', |
| 25 | + 'Tests/Support' => TEST_PATH . '_support/', |
| 26 | + 'App' => APP_PATH, |
| 27 | + 'BlitzPHP' => [ |
| 28 | + TEST_PATH, |
| 29 | + SYST_PATH, |
| 30 | + ], |
| 31 | + 'Errors' => APP_PATH . 'Views/errors', |
| 32 | + 'System' => SUPPORT_PATH . 'Autoloader/system', |
| 33 | + 'Acme\SampleProject' => TEST_PATH . '_support', |
| 34 | + 'Acme\Sample' => TEST_PATH . '_support/does/not/exists', |
| 35 | + ]); |
| 36 | + |
| 37 | + $this->locator = new Locator($this->autoloader); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('locateFile()', function() { |
| 41 | + context('avec des fichiers non-namespacés', function() { |
| 42 | + it('trouve un fichier dans le répertoire App', function() { |
| 43 | + $file = 'Controllers/HomeController'; |
| 44 | + $expected = APP_PATH . 'Controllers' . DS . 'HomeController.php'; |
| 45 | + |
| 46 | + expect($this->locator->locateFile($file))->toBe($expected); |
| 47 | + }); |
| 48 | + |
| 49 | + it('retourne false quand un fichier non-namespacé n\'est pas trouvé', function() { |
| 50 | + $file = 'Unknown'; |
| 51 | + expect($this->locator->locateFile($file))->toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it('trouve un fichier avec un dossier dans le répertoire App', function() { |
| 55 | + $file = 'simple'; |
| 56 | + $expected = VIEW_PATH . 'simple.php'; |
| 57 | + |
| 58 | + expect($this->locator->locateFile($file, 'Views'))->toBe($expected); |
| 59 | + }); |
| 60 | + |
| 61 | + it('trouve un fichier sans dossier dans le répertoire App', function() { |
| 62 | + $file = 'Common'; |
| 63 | + $expected = APP_PATH . 'Common.php'; |
| 64 | + file_put_contents($expected, '<?php '); |
| 65 | + |
| 66 | + expect($this->locator->locateFile($file))->toBe($expected); |
| 67 | + unlink($expected); |
| 68 | + }); |
| 69 | + |
| 70 | + it('fonctionne dans un répertoire App imbriqué', function() { |
| 71 | + $file = 'Controllers/HomeController'; |
| 72 | + $expected = CONTROLLER_PATH . 'HomeController.php'; |
| 73 | + |
| 74 | + expect($this->locator->locateFile($file, 'Controllers'))->toBe($expected); |
| 75 | + }); |
| 76 | + |
| 77 | + it('trouve un fichier avec le nom du dossier dans le chemin', function() { |
| 78 | + $file = 'Views/simple.php'; |
| 79 | + $expected = VIEW_PATH . 'simple.php'; |
| 80 | + |
| 81 | + expect($this->locator->locateFile($file, 'Views'))->toBe($expected); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + context('avec des fichiers namespacés', function() { |
| 86 | + it('trouve une vue namespacée', function() { |
| 87 | + $file = '\Errors\error_404'; |
| 88 | + $expected = VIEW_PATH . 'errors' . DS . 'html' . DS . 'error_404.php'; |
| 89 | + @mkdir(dirname($expected), recursive: true); |
| 90 | + file_put_contents($expected, '<?php '); |
| 91 | + |
| 92 | + expect($this->locator->locateFile($file, 'html'))->toBe($expected); |
| 93 | + unlink($expected); |
| 94 | + }); |
| 95 | + |
| 96 | + it('trouve une vue namespacée imbriquée', function() { |
| 97 | + $file = '\Errors\html/error_404'; |
| 98 | + $expected = VIEW_PATH . 'errors' .DS . 'html' . DS . 'error_404.php'; |
| 99 | + @mkdir(dirname($expected), recursive: true); |
| 100 | + file_put_contents($expected, '<?php '); |
| 101 | + |
| 102 | + expect($this->locator->locateFile($file, 'html'))->toBe($expected); |
| 103 | + unlink($expected); |
| 104 | + }); |
| 105 | + |
| 106 | + it('trouve un fichier avec un namespace correct', function() { |
| 107 | + $file = 'Acme\SampleProject\View\Views\simple'; |
| 108 | + $expected = TEST_PATH . '_support' . DS . 'View' .DS . 'Views' . DS . 'simple.php'; |
| 109 | + @mkdir(dirname($expected), recursive: true); |
| 110 | + file_put_contents($expected, '<?php '); |
| 111 | + |
| 112 | + expect($this->locator->locateFile($file, 'Views'))->toBe($expected); |
| 113 | + unlink($expected); |
| 114 | + }); |
| 115 | + |
| 116 | + it('gère un fichier avec le nom du dossier dans un chemin namespacé', function() { |
| 117 | + $file = '\App\Views/errors/html/error_404.php'; |
| 118 | + $expected = VIEW_PATH . 'errors' . DS . 'html' . DS . 'error_404.php'; |
| 119 | + @mkdir(dirname($expected), recursive: true); |
| 120 | + file_put_contents($expected, '<?php '); |
| 121 | + |
| 122 | + expect($this->locator->locateFile($file, 'Views'))->toBe($expected); |
| 123 | + unlink($expected); |
| 124 | + }); |
| 125 | + |
| 126 | + it('retourne false quand le fichier n\'existe pas dans un namespace existant', function() { |
| 127 | + $file = '\App\Views/unexistence-file.php'; |
| 128 | + expect($this->locator->locateFile($file, 'Views'))->toBe(false); |
| 129 | + }); |
| 130 | + |
| 131 | + it('retourne false quand le namespace n\'existe pas', function() { |
| 132 | + $file = '\Blogger\admin/posts.php'; |
| 133 | + expect($this->locator->locateFile($file, 'Views'))->toBe(false); |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('search()', function() { |
| 139 | + it('trouve un fichier avec une recherche simple', function() { |
| 140 | + $expected = CONFIG_PATH . 'app.php'; |
| 141 | + $foundFiles = $this->locator->search('Config/app.php'); |
| 142 | + |
| 143 | + expect($foundFiles[0])->toBe($expected); |
| 144 | + }); |
| 145 | + |
| 146 | + it('trouve un fichier avec une extension spécifiée', function() { |
| 147 | + $expected = CONFIG_PATH . 'app.php'; |
| 148 | + $foundFiles = $this->locator->search('Config/app', 'php'); |
| 149 | + |
| 150 | + expect($foundFiles[0])->toBe($expected); |
| 151 | + }); |
| 152 | + |
| 153 | + it('trouve plusieurs fichiers quand ils existent', function() { |
| 154 | + $foundFiles = $this->locator->search('Controllers/RestController', 'php'); |
| 155 | + |
| 156 | + expect($foundFiles)->toContain(APP_PATH . 'Controllers' . DS . 'RestController.php'); |
| 157 | + expect($foundFiles)->toContain(SYST_PATH . 'Controllers' . DS . 'RestController.php'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('retourne un tableau vide quand le fichier n\'existe pas', function() { |
| 161 | + $foundFiles = $this->locator->search('Views/Fake.html'); |
| 162 | + |
| 163 | + expect($foundFiles)->toBeEmpty(); |
| 164 | + }); |
| 165 | + |
| 166 | + it('priorise les fichiers système sur les fichiers app', function() { |
| 167 | + $foundFiles = $this->locator->search('Controllers/RestController', 'php', false); |
| 168 | + |
| 169 | + expect($foundFiles)->toBe([ |
| 170 | + SYST_PATH . 'Controllers' . DS . 'RestController.php', |
| 171 | + APP_PATH . 'Controllers' . DS . 'RestController.php', |
| 172 | + ]); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('listNamespaceFiles()', function() { |
| 177 | + it('retourne un tableau vide avec un préfixe et un chemin vides', function() { |
| 178 | + expect($this->locator->listNamespaceFiles('', ''))->toBeEmpty(); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe('listFiles()', function() { |
| 183 | + it('liste les fichiers dans un répertoire', function() { |
| 184 | + $files = $this->locator->listFiles('Config/'); |
| 185 | + |
| 186 | + $expectedWin = APP_PATH . 'Config\app.php'; |
| 187 | + $expectedLin = APP_PATH . 'Config/app.php'; |
| 188 | + |
| 189 | + expect( |
| 190 | + in_array($expectedWin, $files, true) || |
| 191 | + in_array($expectedLin, $files, true) |
| 192 | + )->toBeTruthy(); |
| 193 | + }); |
| 194 | + |
| 195 | + it('ne contient pas de répertoires dans la liste des fichiers', function() { |
| 196 | + $files = $this->locator->listFiles('Views'); |
| 197 | + $directory = str_replace('/', DIRECTORY_SEPARATOR, VIEW_PATH . 'Components'); |
| 198 | + |
| 199 | + expect($files)->not->toContain($directory); |
| 200 | + }); |
| 201 | + |
| 202 | + it('retourne un tableau vide quand l\'entrée est un fichier', function() { |
| 203 | + $files = $this->locator->listFiles('Config/app.php'); |
| 204 | + |
| 205 | + expect($files)->toBeEmpty(); |
| 206 | + }); |
| 207 | + |
| 208 | + it('liste les fichiers depuis plusieurs répertoires', function() { |
| 209 | + $files = $this->locator->listFiles('Middlewares/'); |
| 210 | + |
| 211 | + $expectedWin1 = SYST_PATH . 'Middlewares\BodyParser.php'; |
| 212 | + $expectedLin1 = SYST_PATH . 'Middlewares/BodyParser.php'; |
| 213 | + $expectedWin2 = APP_PATH . 'Middlewares\CustomMiddleware.php'; |
| 214 | + $expectedLin2 = APP_PATH . 'Middlewares/CustomMiddleware.php'; |
| 215 | + |
| 216 | + expect( |
| 217 | + (in_array($expectedWin1, $files, true) || in_array($expectedLin1, $files, true)) |
| 218 | + && (in_array($expectedWin2, $files, true) || in_array($expectedLin2, $files, true)) |
| 219 | + )->toBeTruthy(); |
| 220 | + }); |
| 221 | + |
| 222 | + it('retourne un tableau vide quand le chemin n\'existe pas', function() { |
| 223 | + $files = $this->locator->listFiles('Fake/'); |
| 224 | + |
| 225 | + expect($files)->toBeEmpty(); |
| 226 | + }); |
| 227 | + |
| 228 | + it('retourne un tableau vide sans chemin', function() { |
| 229 | + $files = $this->locator->listFiles(''); |
| 230 | + |
| 231 | + expect($files)->toBeEmpty(); |
| 232 | + }); |
| 233 | + }); |
| 234 | + |
| 235 | + describe('findQualifiedNameFromPath()', function() { |
| 236 | + it('trouve le nom qualifié depuis un chemin simple', function() { |
| 237 | + $className = $this->locator->findQualifiedNameFromPath(SYST_PATH . 'Enums/Method.php'); |
| 238 | + $expected = Method::class; |
| 239 | + |
| 240 | + expect($className)->toBe($expected); |
| 241 | + }); |
| 242 | + |
| 243 | + it('retourne false quand le fichier n\'existe pas', function() { |
| 244 | + $className = $this->locator->findQualifiedNameFromPath('modules/blog/Views/index.php'); |
| 245 | + |
| 246 | + expect($className)->toBe(false); |
| 247 | + }); |
| 248 | + |
| 249 | + it('retourne false sans namespace correspondant', function() { |
| 250 | + $className = $this->locator->findQualifiedNameFromPath('/etc/hosts'); |
| 251 | + |
| 252 | + expect($className)->toBe(false); |
| 253 | + }); |
| 254 | + }); |
| 255 | + |
| 256 | + describe('getClassname()', function() { |
| 257 | + it('obtient le nom de la classe depuis un fichier de classe', function() { |
| 258 | + $className = $this->locator->getClassname(CONTROLLER_PATH . 'RestController.php'); |
| 259 | + |
| 260 | + expect($className)->toBe(RestController::class); |
| 261 | + }); |
| 262 | + |
| 263 | + it('retourne une chaîne vide depuis un fichier non-classe', function() { |
| 264 | + $className = $this->locator->getClassname(CONFIG_PATH . 'app.php'); |
| 265 | + |
| 266 | + expect($className)->toBe(''); |
| 267 | + }); |
| 268 | + |
| 269 | + it('retourne une chaîne vide depuis un répertoire', function() { |
| 270 | + $className = $this->locator->getClassname(SYST_PATH); |
| 271 | + |
| 272 | + expect($className)->toBe(''); |
| 273 | + }); |
| 274 | + }); |
| 275 | +}); |
0 commit comments