Skip to content

Commit

Permalink
Merge pull request #54 from fr3nch13/2.x-dev
Browse files Browse the repository at this point in the history
Add support for existing test_app or use ours.
  • Loading branch information
fr3nch13 authored Aug 21, 2024
2 parents 9052937 + 46dc1f4 commit fdfd519
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
"cs-check": "php -d memory_limit=-1 ./vendor/bin/phpcs --colors -p -s --extensions=php ./src ./tests ./config",
"cs-checkstyle": "php -d memory_limit=-1 ./vendor/bin/phpcs --report=checkstyle --extensions=php ./src ./tests ./config",
"cs-fix": "php -d memory_limit=-1 ./vendor/bin/phpcbf --colors --extensions=php ./src ./tests ./config",
"phpstan": "php -d memory_limit=-1 ./vendor/bin/phpstan --no-progress -vvv --xdebug",
"phpstan": "php -d memory_limit=-1 ./vendor/bin/phpstan --no-progress -vvv",
"phpstan-github": "php -d memory_limit=-1 ./vendor/bin/phpstan --no-progress -vvv --error-format=github",
"test": "php -d memory_limit=-1 ./vendor/bin/phpunit --colors=always",
"test": "php -d memory_limit=-1 ./vendor/bin/phpunit --colors=always --testdox",
"coverage": "php -d memory_limit=-1 -d xdebug.mode=coverage ./vendor/bin/phpunit --log-junit tmp/coverage/unitreport.xml --coverage-html tmp/coverage",
"coverage-clover": "php -d memory_limit=-1 -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-clover=tmp/coverage.xml",
"coverage-text": "php -d memory_limit=-1 -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-text --colors=never"
Expand Down
8 changes: 0 additions & 8 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ public function bootstrap(PluginApplicationInterface $app): void
if (PHP_SAPI === 'cli') {
$this->bootstrapCli($app);
}

/*
* Only try to load DebugKit in development mode
* Debug Kit should not be installed on a production system
*/
if (Configure::read('debug')) {
$app->addPlugin(\DebugKit\Plugin::class);
}
}

/**
Expand Down
2 changes: 0 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
* installed as a dependency of an application.
*/

$root = dirname(__DIR__);
chdir($root);
require_once __DIR__ . DS . 'plugin_bootstrap.php';
31 changes: 30 additions & 1 deletion tests/plugin_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
throw new Exception("Cannot find the root of the application, unable to run tests");
};

define('PLUGIN_BOOTSTRAP', __FILE__);

if (!defined('PLUGIN_ROOT')) {
define('PLUGIN_ROOT', $findRoot(getcwd()));
define('PLUGIN_ROOT', $findRoot(__DIR__));
}

if (!defined('DS')) {
Expand Down Expand Up @@ -206,7 +208,34 @@
];

if (Configure::check('Tests.DbConfig')) {
// read the dbconfig from before the .env file is read.
$dbconfig = Configure::read('Tests.DbConfig');
// since the .env files is read AFTER this is set,
// in the root's bootstrap.php
// overwrite it with he env value if it's not null
$db_settings = [
'className' => 'CI_DB_CLASSNAME',
'driver' => 'CI_DB_DRIVER',
'persistent' => 'CI_DB_PERSISTENT',
'host' => 'CI_DB_HOST',
'port' => 'CI_DB_PORT',
'username' => 'CI_DB_USERNAME',
'password' => 'CI_DB_PASSWORD',
'database' => 'CI_DB_DATABASE',
'encoding' => 'CI_DB_ENCODING',
'timezone' => 'CI_DB_TIMEZONE',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => true,
'url' => 'CI_DB_URL',
];

foreach ($db_settings as $db_key => $db_constant) {
if (is_string($db_constant) && env($db_constant, null) !== null) {
$dbconfig[$db_key] = env($db_constant);
}
}
}

ConnectionManager::setConfig('default', $dbconfig);
Expand Down
28 changes: 18 additions & 10 deletions tests/test_app/config/paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,26 @@
* When using custom settings be sure to use the DS and do not add a trailing DS.
*/

/**
* If this is somehow included without the plugin_bootstrap.php
* We need to throw an error
*/
if (!defined('PLUGIN_BOOTSTRAP')) {
$this_relative_path = str_replace(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR, '', __FILE__);
throw new \Exception('This file shouldn\'t be included by anything other than the plugin_bootstrap.php File: ' . $this_relative_path);
}

/**
* The full path to the directory which holds "src", WITHOUT a trailing DS.
*/
if (!defined('ROOT')) {
define('ROOT', dirname(__DIR__));
if (is_dir(PLUGIN_ROOT . DS . 'tests' . DS . 'test_app')) {
// their test app
define('ROOT', PLUGIN_ROOT . DS . 'tests' . DS . 'test_app');
} else {
// our test app.
define('ROOT', dirname(__DIR__));
}
}

/**
Expand All @@ -44,7 +59,7 @@
* Path to the config directory.
*/
if (!defined('CONFIG')) {
define('CONFIG', __DIR__ . DS);
define('CONFIG', ROOT . DS . 'config' . DS);
}

/**
Expand All @@ -58,7 +73,7 @@
* Path to the tests directory.
*/
if (!defined('TESTS')) {
define('TESTS', ROOT . DS . 'tests' . DS);
define('TESTS', PLUGIN_ROOT . DS . 'tests' . DS);
}

/**
Expand All @@ -82,13 +97,6 @@
define('CACHE', TMP . 'cache' . DS);
}

/**
* The root of the plugin.
*/
if (!defined('PLUGIN_ROOT')) {
define('PLUGIN_ROOT', dirname(__DIR__));
}

/**
* The absolute path to the "cake" directory, WITHOUT a trailing DS.
*
Expand Down
19 changes: 16 additions & 3 deletions tests/test_app/src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function bootstrap(): void
* Debug Kit should not be installed on a production system
*/
if (Configure::read('debug')) {
$this->addPlugin(\DebugKit\Plugin::class);
$this->addOptionalPlugin(\DebugKit\Plugin::class);
}

// Load more plugins here
Expand All @@ -72,7 +72,15 @@ public function bootstrap(): void
// Load plugins defined in Configure. see the plugin's tests/bootstrap.php
if (Configure::check('Tests.Plugins')) {
foreach (Configure::read('Tests.Plugins') as $value) {
$this->addPlugin($value);
// make sure hooks still works for older plugin style.
// see: https://book.cakephp.org/4/en/plugins.html#plugin-hook-configuration
// about using Plugin.php
$this->addPlugin($value, [
'bootstrap' => true,
'routes' => true,
'middleware' => true,
'console' => true,
]);
}
}
}
Expand Down Expand Up @@ -130,7 +138,12 @@ protected function bootstrapCli(): void
// Load plugins defined in Configure. see the plugin's tests/bootstrap.php
if (Configure::check('Tests.PluginsCli')) {
foreach (Configure::read('Tests.PluginsCli') as $value) {
$this->addPlugin($value);
$this->addPlugin($value, [
'bootstrap' => true,
'routes' => true,
'middleware' => true,
'console' => true,
]);
}
}
}
Expand Down

0 comments on commit fdfd519

Please sign in to comment.