Skip to content

Commit

Permalink
Loading a Module (either MVC or CLI) now throws an exception if the p…
Browse files Browse the repository at this point in the history
…ath does not exists regardless of whether the class is already loaded.
  • Loading branch information
SidRoberts authored and niden committed May 15, 2019
1 parent bd1b05f commit dca688f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- `Phalcon\Logger\Formatter\Line::__construct()` service parameters must be a string or omitted.
- `Phalcon\Logger\Formatter\Json::__construct()` service parameters must be a string or omitted.
- Removed deprecated code from `Phalcon\Forms\Form::getMessages()`.
- Loading a Module (either MVC or CLI) now throws an exception if the path does not exists regardless of whether the class is already loaded.

## Fixed
- Fixed `Mvc\Collection::isInitialized()` now works as intended. [#13931](https://github.com/phalcon/cphalcon/pull/13931)
Expand Down
12 changes: 6 additions & 6 deletions phalcon/Cli/Console.zep
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ class Console extends BaseApplication
}

if fetch path, module["path"] {
if !class_exists(className, false) {
if unlikely !file_exists(path) {
throw new Exception(
"Module definition path '" . path . "' doesn't exist"
);
}
if unlikely !file_exists(path) {
throw new Exception(
"Module definition path '" . path . "' doesn't exist"
);
}

if !class_exists(className, false) {
require path;
}
}
Expand Down
12 changes: 6 additions & 6 deletions phalcon/Mvc/Application.zep
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ class Application extends BaseApplication
* If developer specify a path try to include the file
*/
if fetch path, module["path"] {
if !class_exists(className, false) {
if unlikely !file_exists(path) {
throw new Exception(
"Module definition path '" . path . "' doesn't exist"
);
}
if unlikely !file_exists(path) {
throw new Exception(
"Module definition path '" . path . "' doesn't exist"
);
}

if !class_exists(className, false) {
require path;
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/cli/Cli/Console/GetModuleCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ public function cliConsoleGetModule(CliTester $I)
$console->registerModules(
[
'frontend' => [
'className' => 'Phalcon\\Test\\Modules\\Frontend\\Module',
'path' => __DIR__ . '/../../../_data/modules/frontend/Module.php',
'className' => \Phalcon\Test\Modules\Frontend\Module::class,
'path' => dataDir('fixtures/modules/frontend/Module.php'),
],
'backend' => [
'className' => 'Phalcon\\Test\\Modules\\Backend\\Module',
'path' => __DIR__ . '/../../../_data/modules/backend/Module.php',
'className' => \Phalcon\Test\Modules\Backend\Module::class,
'path' => dataDir('fixtures/modules/backend/Module.php'),
],
]
);

$expected = [
'className' => 'Phalcon\\Test\\Modules\\Frontend\\Module',
'path' => __DIR__ . '/../../../_data/modules/frontend/Module.php',
'className' => \Phalcon\Test\Modules\Frontend\Module::class,
'path' => dataDir('fixtures/modules/frontend/Module.php'),
];

$I->assertEquals(
Expand Down
33 changes: 30 additions & 3 deletions tests/cli/Cli/Console/RegisterModulesCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function cliConsoleRegisterModules(CliTester $I)
[
'frontend' => [
'className' => Module::class,
'path' => __DIR__ . '/../../../_data/modules/frontend/Module.php',
'path' => dataDir('fixtures/modules/frontend/Module.php'),
],
]
);
Expand All @@ -58,7 +58,7 @@ public function cliConsoleRegisterModules(CliTester $I)
[
'backend' => [
'className' => \Phalcon\Test\Modules\Backend\Module::class,
'path' => __DIR__ . '/../../../_data/modules/backend/Module.php',
'path' => dataDir('fixtures/modules/backend/Module.php'),
],
]
);
Expand All @@ -77,7 +77,7 @@ public function cliConsoleRegisterModules(CliTester $I)
[
'frontend' => [
'className' => Module::class,
'path' => __DIR__ . '/../../../_data/modules/frontend/Module.php',
'path' => dataDir('fixtures/modules/frontend/Module.php'),
],
],
$merge = true
Expand All @@ -98,4 +98,31 @@ public function cliConsoleRegisterModules(CliTester $I)
$console->getModules()
);
}

public function badPathThrowsAnException(CliTester $I)
{
$console = $this->newCliConsole();

$console->registerModules(
[
'frontend' => [
'path' => dataDir('not-a-real-file.php'),
'className' => \Phalcon\Test\Modules\Frontend\Module::class,
],
]
);

$I->expectException(
new \Phalcon\Cli\Console\Exception(
"Module definition path 'not-a-real-file.php' doesn't exist"
),
function () use ($console) {
$console->handle(
[
'task' => 'echo',
]
);
}
);
}
}
38 changes: 38 additions & 0 deletions tests/integration/Mvc/Application/RegisterModulesCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,42 @@ function () use ($view) {
$response->getContent()
);
}

public function badPathThrowsAnException(IntegrationTester $I)
{
Di::reset();

$di = new FactoryDefault();

$di->set(
'router',
function () {
$router = new Router(false);

return $router;
}
);

$application = new Application();

$application->registerModules(
[
'frontend' => [
'path' => dataDir('not-a-real-file.php'),
'className' => \Phalcon\Test\Modules\Frontend\Module::class,
],
]
);

$application->setDI($di);

$I->expectException(
new \Phalcon\Mvc\Application\Exception(
"Module definition path 'not-a-real-file.php' doesn't exist"
),
function () use ($application) {
$response = $application->handle('/');
}
);
}
}

0 comments on commit dca688f

Please sign in to comment.