mirror of
https://github.com/e107inc/e107.git
synced 2025-07-25 00:41:52 +02:00
Issue #5444 Plugin's can now contain Unit and Acceptance tests.
This commit is contained in:
47
e107_plugins/_blank/tests/unit/_blank_eventTest.php
Normal file
47
e107_plugins/_blank/tests/unit/_blank_eventTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\_blank;
|
||||
use Codeception\Test\Unit;
|
||||
|
||||
/* To use, run these commands from the root directory of e107 in CLI:
|
||||
|
||||
cd e107_tests
|
||||
vendor/bin/codecept run unit ../e107_plugins/_blank/tests/unit
|
||||
|
||||
OR with debug options:
|
||||
|
||||
vendor/bin/codecept run unit ../e107_plugins/_blank/tests/unit --steps --debug
|
||||
*/
|
||||
|
||||
|
||||
class _blank_eventTest extends Unit
|
||||
{
|
||||
|
||||
/** @var _blank_event */
|
||||
protected $ep;
|
||||
|
||||
public function testMyfunction()
|
||||
{
|
||||
|
||||
$value = "THIS IS THE BLANK TEST";
|
||||
self::assertSame($value, "THIS IS THE BLANK TEST");
|
||||
}
|
||||
|
||||
protected function _before()
|
||||
{
|
||||
require_once(dirname(__FILE__) . '/../../e_event.php');
|
||||
|
||||
try
|
||||
{
|
||||
$this->ep = $this->make('_blank_event');
|
||||
}
|
||||
|
||||
catch(Exception $e)
|
||||
{
|
||||
self::fail($e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -2,65 +2,152 @@
|
||||
use Codeception\Util\Autoload;
|
||||
use Codeception\Configuration;
|
||||
|
||||
// Load unit tests namespace
|
||||
Autoload::addNamespace('', codecept_root_dir() . '/tests/unit');
|
||||
class E107TestSuiteBootstrap
|
||||
{
|
||||
const ENABLE_LOGGING = false; // Toggle logging (set to true to enable)
|
||||
|
||||
// Load parameters
|
||||
define('PARAMS_GENERATOR', realpath(codecept_root_dir() . "/lib/config.php"));
|
||||
$params = include(PARAMS_GENERATOR);
|
||||
private $logFile;
|
||||
private $pluginsDir;
|
||||
|
||||
// Define APP_PATH
|
||||
$app_path = $params['app_path'] ?: codecept_root_dir() . "/e107";
|
||||
if (substr($app_path, 0, 1) !== '/') {
|
||||
$app_path = codecept_root_dir() . "/{$app_path}";
|
||||
}
|
||||
define('APP_PATH', realpath($app_path));
|
||||
define('PARAMS_SERIALIZED', serialize($params));
|
||||
public function __construct()
|
||||
{
|
||||
$this->logFile = codecept_output_dir() . '/bootstrap.log';
|
||||
|
||||
// Debug logging
|
||||
$logFile = codecept_output_dir() . '/bootstrap.log';
|
||||
file_put_contents($logFile, '');
|
||||
file_put_contents($logFile, "Time: " . date(DATE_ATOM) . "\n", FILE_APPEND);
|
||||
file_put_contents($logFile, "Root Dir: " . codecept_root_dir() . "\n", FILE_APPEND);
|
||||
file_put_contents($logFile, "App Path: " . APP_PATH . "\n", FILE_APPEND);
|
||||
if (defined('e_PLUGIN')) {
|
||||
file_put_contents($logFile, "e_PLUGIN already defined as: " . e_PLUGIN . "\n", FILE_APPEND);
|
||||
} else {
|
||||
file_put_contents($logFile, "e_PLUGIN not defined yet\n", FILE_APPEND);
|
||||
// Let e107 define e_PLUGIN later; avoid redefinition
|
||||
// define('e_PLUGIN', APP_PATH . '/e107_plugins/');
|
||||
}
|
||||
if (self::ENABLE_LOGGING)
|
||||
{
|
||||
file_put_contents($this->logFile, ""); // Clear log on start
|
||||
}
|
||||
|
||||
// Dynamic plugin test autoloading
|
||||
$pluginsDir = realpath(codecept_root_dir() . '/../e107_plugins/');
|
||||
$pluginTestDirs = [];
|
||||
|
||||
file_put_contents($logFile, "Plugins Dir: $pluginsDir\n", FILE_APPEND);
|
||||
|
||||
if ($pluginsDir && is_dir($pluginsDir)) {
|
||||
$dirs = glob($pluginsDir . '/*/tests', GLOB_ONLYDIR);
|
||||
file_put_contents($logFile, "Glob Pattern: $pluginsDir/*/tests\n", FILE_APPEND);
|
||||
file_put_contents($logFile, "Found Dirs: " . (empty($dirs) ? 'None' : implode(', ', $dirs)) . "\n", FILE_APPEND);
|
||||
foreach ($dirs as $testDir) {
|
||||
$pluginName = basename(dirname($testDir));
|
||||
$relativePath = '../e107_plugins/' . $pluginName . '/tests';
|
||||
$pluginTestDirs[] = $relativePath;
|
||||
Autoload::addNamespace("Tests\\" . ucfirst($pluginName), $testDir);
|
||||
file_put_contents($logFile, "Added namespace: Tests\\" . ucfirst($pluginName) . " => $testDir\n", FILE_APPEND);
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
private function log($message)
|
||||
{
|
||||
if (self::ENABLE_LOGGING)
|
||||
{
|
||||
file_put_contents($this->logFile, $message . "\n", FILE_APPEND);
|
||||
}
|
||||
}
|
||||
|
||||
private function initialize()
|
||||
{
|
||||
// Log initial environment
|
||||
$this->log("Time: " . date(DATE_ATOM));
|
||||
$this->log("PHP Version: " . PHP_VERSION);
|
||||
$this->log("Root Dir: " . codecept_root_dir());
|
||||
|
||||
// Load core unit tests namespace (e107_tests/tests/unit/)
|
||||
Autoload::addNamespace('Tests\Unit', codecept_root_dir() . '/tests/unit');
|
||||
$this->log("Added core unit namespace: Tests\\Unit => " . codecept_root_dir() . '/tests/unit');
|
||||
|
||||
// Load parameters
|
||||
define('PARAMS_GENERATOR', realpath(codecept_root_dir() . "/lib/config.php"));
|
||||
$params = include(PARAMS_GENERATOR);
|
||||
|
||||
// Define APP_PATH
|
||||
$app_path = $params['app_path'] ?: codecept_root_dir() . "/e107";
|
||||
if (substr($app_path, 0, 1) !== '/')
|
||||
{
|
||||
$app_path = codecept_root_dir() . "/$app_path";
|
||||
}
|
||||
define('APP_PATH', realpath($app_path));
|
||||
define('PARAMS_SERIALIZED', serialize($params));
|
||||
|
||||
// Log App Path after definition
|
||||
$this->log("App Path: " . APP_PATH);
|
||||
|
||||
// e_PLUGIN status
|
||||
if (defined('e_PLUGIN'))
|
||||
{
|
||||
$this->log("e_PLUGIN already defined as: " . e_PLUGIN);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->log("e_PLUGIN not defined yet");
|
||||
}
|
||||
|
||||
// Set plugins directory once
|
||||
$this->pluginsDir = realpath(codecept_root_dir() . '/../e107_plugins/');
|
||||
$this->log("Plugins Dir: $this->pluginsDir");
|
||||
|
||||
// Load test types
|
||||
$this->loadUnitTests();
|
||||
$this->loadAcceptanceTests();
|
||||
|
||||
// Include required e107 file
|
||||
include(codecept_root_dir() . "/lib/PriorityCallbacks.php");
|
||||
|
||||
$this->log("e_PLUGIN after initialization: " . (defined('e_PLUGIN') ? e_PLUGIN : 'not defined'));
|
||||
}
|
||||
|
||||
private function loadUnitTests()
|
||||
{
|
||||
$pluginUnitDirs = [];
|
||||
|
||||
if ($this->pluginsDir && is_dir($this->pluginsDir))
|
||||
{
|
||||
$unitDirs = glob($this->pluginsDir . '/*/tests/unit', GLOB_ONLYDIR);
|
||||
$separator = DIRECTORY_SEPARATOR; // \ on Windows, / on Linux
|
||||
$unitGlobPattern = str_replace('/', $separator, $this->pluginsDir . '/*/tests/unit');
|
||||
$this->log("Unit Glob Pattern: $unitGlobPattern");
|
||||
$this->log("Found Unit Dirs: " . (empty($unitDirs) ? 'None' : ''));
|
||||
if (!empty($unitDirs))
|
||||
{
|
||||
foreach ($unitDirs as $dir)
|
||||
{
|
||||
$this->log("\t" . $dir);
|
||||
}
|
||||
}
|
||||
foreach ($unitDirs as $testDir)
|
||||
{
|
||||
$pluginName = basename(dirname($testDir, 2)); // Two levels up from /unit
|
||||
$relativePath = '../e107_plugins/' . $pluginName . '/tests/unit';
|
||||
$pluginUnitDirs[] = $relativePath;
|
||||
Autoload::addNamespace("Tests\\Unit\\" . ucfirst($pluginName), $testDir);
|
||||
$this->log("Added unit namespace: Tests\\Unit\\" . ucfirst($pluginName) . " => $testDir");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->log("Plugins Dir not found or not a directory");
|
||||
}
|
||||
$this->log("Included Unit Dirs: " . json_encode($pluginUnitDirs, JSON_PRETTY_PRINT));
|
||||
}
|
||||
|
||||
private function loadAcceptanceTests()
|
||||
{
|
||||
$pluginAcceptanceDirs = [];
|
||||
|
||||
if ($this->pluginsDir && is_dir($this->pluginsDir))
|
||||
{
|
||||
$acceptanceDirs = glob($this->pluginsDir . '/*/tests/acceptance', GLOB_ONLYDIR);
|
||||
$separator = DIRECTORY_SEPARATOR; // \ on Windows, / on Linux
|
||||
$acceptanceGlobPattern = str_replace('/', $separator, $this->pluginsDir . '/*/tests/acceptance');
|
||||
$this->log("Acceptance Glob Pattern: $acceptanceGlobPattern");
|
||||
$this->log("Found Acceptance Dirs: " . (empty($acceptanceDirs) ? 'None' : ''));
|
||||
if (!empty($acceptanceDirs))
|
||||
{
|
||||
foreach ($acceptanceDirs as $dir)
|
||||
{
|
||||
$this->log("\t" . $dir);
|
||||
}
|
||||
}
|
||||
foreach ($acceptanceDirs as $testDir)
|
||||
{
|
||||
$pluginName = basename(dirname($testDir, 2));
|
||||
$relativePath = '../e107_plugins/' . $pluginName . '/tests/acceptance';
|
||||
$pluginAcceptanceDirs[] = $relativePath;
|
||||
Autoload::addNamespace("Tests\\Acceptance\\" . ucfirst($pluginName), $testDir);
|
||||
$this->log("Added acceptance namespace: Tests\\Acceptance\\" . ucfirst($pluginName) . " => $testDir");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->log("Plugins Dir not found or not a directory");
|
||||
}
|
||||
$this->log("Included Acceptance Dirs: " . json_encode($pluginAcceptanceDirs, JSON_PRETTY_PRINT));
|
||||
}
|
||||
} else {
|
||||
file_put_contents($logFile, "Plugins Dir not found or not a directory\n", FILE_APPEND);
|
||||
}
|
||||
file_put_contents($logFile, "Included: " . json_encode($pluginTestDirs, JSON_PRETTY_PRINT) . "\n", FILE_APPEND);
|
||||
file_put_contents($logFile, "e_PLUGIN after scan: " . (defined('e_PLUGIN') ? e_PLUGIN : 'not defined') . "\n", FILE_APPEND);
|
||||
|
||||
// Skip Configuration::append to avoid config errors
|
||||
// if (!empty($pluginTestDirs)) {
|
||||
// Configuration::append(['include' => $pluginTestDirs]);
|
||||
// codecept_debug("Dynamic includes added: " . json_encode($pluginTestDirs));
|
||||
// } else {
|
||||
// codecept_debug("No plugin test directories found");
|
||||
// }
|
||||
|
||||
// Include required e107 file
|
||||
include(codecept_root_dir() . "/lib/PriorityCallbacks.php");
|
||||
new E107TestSuiteBootstrap;
|
Reference in New Issue
Block a user