Implemented Priority for componentPaths.

This commit is contained in:
Abel Hoogeveen 2019-01-21 17:19:09 +01:00
parent 436e270710
commit f364245d90
No known key found for this signature in database
GPG Key ID: 96C2234920BF4292
20 changed files with 215 additions and 520 deletions

View File

@ -45,7 +45,7 @@ trait ComponentPathsTrait
* *
* @var array $componentPaths * @var array $componentPaths
*/ */
protected $componentPaths = array(); protected $componentPaths = [];
/** /**
* Set the directories. Automatically gets invoked if componentPaths are added by FuzeWorks\Configurator. * Set the directories. Automatically gets invoked if componentPaths are added by FuzeWorks\Configurator.
@ -54,42 +54,47 @@ trait ComponentPathsTrait
*/ */
public function setDirectories(array $componentPaths) public function setDirectories(array $componentPaths)
{ {
$this->componentPaths = array_merge($this->componentPaths, $componentPaths); $this->componentPaths = $componentPaths;
} }
/** /**
* Add a path where objects for this component can be found * Add a path where objects for this component can be found
* *
* @param string $componentPath * @param string $componentPath
* @param int $priority
*/ */
public function addComponentPath($componentPath) public function addComponentPath($componentPath, $priority = Priority::NORMAL)
{ {
if (!in_array($componentPath, $this->componentPaths)) if (!isset($this->componentPaths[$priority]))
{ $this->componentPaths[$priority] = [];
$this->componentPaths[] = $componentPath;
} if (!in_array($componentPath, $this->componentPaths[$priority]))
$this->componentPaths[$priority][] = $componentPath;
} }
/** /**
* Remove a path where objects for this component can be found * Remove a path where objects for this component can be found
* *
* @param string $componentPath * @param string $componentPath
* @param int $priority
*/ */
public function removeComponentPath($componentPath) public function removeComponentPath($componentPath, $priority = Priority::NORMAL)
{ {
if (($key = array_search($componentPath, $this->componentPaths)) !== false) if (!isset($this->componentPaths[$priority]))
{ return;
unset($this->componentPaths[$key]);
} if (($key = array_search($componentPath, $this->componentPaths[$priority])) !== false)
unset($this->componentPaths[$priority][$key]);
} }
/** /**
* Get a list of all current componentPaths * Get a list of all current componentPaths
* *
* @param int $priority
* @return array of paths where objects for this component can be found * @return array of paths where objects for this component can be found
*/ */
public function getComponentPaths(): array public function getComponentPaths($priority = Priority::NORMAL): array
{ {
return $this->componentPaths; return (isset($this->componentPaths[$priority]) ? $this->componentPaths[$priority] : []);
} }
} }

View File

@ -66,11 +66,6 @@ class Config
*/ */
public static $configOverrides = []; public static $configOverrides = [];
public function __construct()
{
$this->componentPaths = Core::$appDirs;
}
/** /**
* Retrieve a config file object * Retrieve a config file object
* *
@ -81,20 +76,24 @@ class Config
*/ */
public function getConfig(string $configName, array $configPaths = []): ConfigORM public function getConfig(string $configName, array $configPaths = []): ConfigORM
{ {
// First determine what directories to use
$directories = (empty($configPaths) ? $this->componentPaths : $configPaths);
// Determine the config name // Determine the config name
$configName = strtolower($configName); $configName = strtolower($configName);
// If it's already loaded, return the existing object // If it's already loaded, return the existing object
if (isset($this->cfg[$configName])) if (isset($this->cfg[$configName]))
{ {
return $this->cfg[$configName]; return $this->cfg[$configName];
} }
// First determine what directories to use
$paths = [];
if (!empty($configPaths))
$paths[3] = $configPaths;
else
$paths = $this->componentPaths;
// Otherwise try and load a new one // Otherwise try and load a new one
$this->cfg[$configName] = $this->loadConfigFile($configName, $directories); $this->cfg[$configName] = $this->loadConfigFile($configName, $paths);
return $this->cfg[$configName]; return $this->cfg[$configName];
} }
@ -148,29 +147,34 @@ class Config
// If cancelled, load empty config // If cancelled, load empty config
if ($event->isCancelled()) if ($event->isCancelled())
{
return new ConfigORM(); return new ConfigORM();
}
// Cycle through all directories // Cycle through all priorities if they exist
foreach ($event->configPaths as $configPath) for ($i=Priority::getHighestPriority(); $i<=Priority::getLowestPriority(); $i++)
{ {
// If file exists, load it and break the loop if (!isset($event->configPaths[$i]))
$file = $configPath . DS . 'config.'.strtolower($event->configName).'.php'; continue;
if (file_exists($file))
// Cycle through all directories
foreach ($event->configPaths[$i] as $configPath)
{ {
// Load object // If file exists, load it and break the loop
$configORM = (new ConfigORM())->load($file); $file = $configPath . DS . 'config.'.strtolower($event->configName).'.php';
if (file_exists($file))
// Override config values if they exist
if (isset(self::$configOverrides[$event->configName]))
{ {
foreach (self::$configOverrides[$event->configName] as $configKey => $configValue) // Load object
$configORM->{$configKey} = $configValue; $configORM = (new ConfigORM())->load($file);
}
// Return object // Override config values if they exist
return $configORM; if (isset(self::$configOverrides[$event->configName]))
{
foreach (self::$configOverrides[$event->configName] as $configKey => $configValue)
$configORM->{$configKey} = $configValue;
}
// Return object
return $configORM;
}
} }
} }

View File

@ -75,7 +75,7 @@ class Configurator
* *
* @var array of directories * @var array of directories
*/ */
protected $directories = ['app' => []]; protected $directories = [];
/** /**
* Array of ComponentClass methods to be invoked once ComponentClass is loaded * Array of ComponentClass methods to be invoked once ComponentClass is loaded
@ -124,12 +124,20 @@ class Configurator
* Add a directory to FuzeWorks * Add a directory to FuzeWorks
* *
* @param string $directory * @param string $directory
* @param string $category Optional. Defaults to 'app * @param string $category Optional
* @param int $priority
* @return $this * @return $this
*/ */
public function addDirectory(string $directory, string $category = 'app'): Configurator public function addDirectory(string $directory, string $category, $priority = Priority::NORMAL): Configurator
{ {
$this->directories[$category][] = $directory; if (!isset($this->directories[$category]))
$this->directories[$category] = [];
if (!isset($this->directories[$category][$priority]))
$this->directories[$category][$priority] = [];
if (!in_array($directory, $this->directories[$category][$priority]))
$this->directories[$category][$priority][] = $directory;
return $this; return $this;
} }
@ -318,7 +326,6 @@ class Configurator
// First set all the fixed directories // First set all the fixed directories
Core::$tempDir = $this->parameters['tempDir']; Core::$tempDir = $this->parameters['tempDir'];
Core::$logDir = $this->parameters['logDir']; Core::$logDir = $this->parameters['logDir'];
Core::$appDirs = $this->directories['app'];
// Then prepare the debugger // Then prepare the debugger
$debug = ($this->parameters['debugEnabled'] && $this->parameters['debugMatch'] ? true : false); $debug = ($this->parameters['debugEnabled'] && $this->parameters['debugMatch'] ? true : false);
@ -366,14 +373,14 @@ class Configurator
} }
} }
// And add all directories to the components // Add directories to Components
foreach ($this->directories as $component => $directories) { foreach ($this->directories as $component => $priorityArray)
{
Logger::logDebug("Adding directories for '" . $component . "'"); Logger::logDebug("Adding directories for '" . $component . "'");
if ($component == 'app')
continue;
if (method_exists($container->{$component}, 'setDirectories')) if (method_exists($container->{$component}, 'setDirectories'))
$container->{$component}->setDirectories($directories); {
$container->{$component}->setDirectories($priorityArray);
}
} }
$container->init(); $container->init();

View File

@ -71,8 +71,6 @@ class Core
public static $logDir; public static $logDir;
public static $appDirs = [];
/** /**
* The HTTP status code of the current request * The HTTP status code of the current request
* *

View File

@ -69,11 +69,6 @@ class Helpers
*/ */
protected $helpers = []; protected $helpers = [];
public function __construct()
{
$this->componentPaths = Core::$appDirs;
}
/** /**
* Load a helper. * Load a helper.
* *
@ -81,14 +76,14 @@ class Helpers
* or from one of the helperPaths (which you can add). * or from one of the helperPaths (which you can add).
* *
* @param string $helperName Name of the helper * @param string $helperName Name of the helper
* @param array $helperDirectories * @param array $helperPaths
* @return bool Whether the helper was successfully loaded (true if yes) * @return bool Whether the helper was successfully loaded (true if yes)
* @throws HelperException * @throws HelperException
*/ */
public function load(string $helperName, array $helperDirectories = []): bool public function load(string $helperName, array $helperPaths = []): bool
{ {
// Determine what directories should be checked // Determine what directories should be checked
$helperPaths = (empty($helperDirectories) ? $this->componentPaths : $helperDirectories); $helperPaths = (empty($helperPaths) ? $this->componentPaths : [3 => $helperPaths]);
// Check it is already loaded // Check it is already loaded
if (isset($this->helpers[$helperName])) if (isset($this->helpers[$helperName]))
@ -115,27 +110,33 @@ class Helpers
} }
// Iterate over helperPaths and attempt to load if helper exists // Iterate over helperPaths and attempt to load if helper exists
foreach ($event->helperPaths as $helperPath) for ($i=Priority::getHighestPriority(); $i<=Priority::getLowestPriority(); $i++)
{ {
$file = $helperPath . DS . $event->helperName . '.php'; if (!isset($event->helperPaths[$i]))
$subfile = $helperPath . DS . $event->helperName . DS . $event->helperName . '.php'; continue;
if (file_exists($file))
{
// Load and register
include_once($file);
$this->helpers[$event->helperName] = true;
Logger::log("Loaded helper '".$event->helperName."'");
return true;
}
// If php file not in main directory, check subdirectories foreach ($event->helperPaths[$i] as $helperPath)
elseif (file_exists($subfile))
{ {
// Load and register $file = $helperPath . DS . $event->helperName . '.php';
include_once($subfile); $subfile = $helperPath . DS . $event->helperName . DS . $event->helperName . '.php';
$this->helpers[$event->helperName] = true; if (file_exists($file))
Logger::log("Loaded helper '".$event->helperName."''"); {
return true; // Load and register
include_once($file);
$this->helpers[$event->helperName] = true;
Logger::log("Loaded helper '".$event->helperName."'");
return true;
}
// If php file not in main directory, check subdirectories
elseif (file_exists($subfile))
{
// Load and register
include_once($subfile);
$this->helpers[$event->helperName] = true;
Logger::log("Loaded helper '".$event->helperName."''");
return true;
}
} }
} }

View File

@ -72,7 +72,6 @@ class Libraries
public function __construct() public function __construct()
{ {
$this->factory = Factory::getInstance(); $this->factory = Factory::getInstance();
$this->componentPaths = Core::$appDirs;
} }
/** /**
@ -112,11 +111,11 @@ class Libraries
* *
* @param string $libraryName * @param string $libraryName
* @param array $parameters * @param array $parameters
* @param array $altDirectories * @param array $libraryPaths
* @return object * @return object
* @throws LibraryException * @throws LibraryException
*/ */
public function get(string $libraryName, array $parameters = [], array $altDirectories = []) public function get(string $libraryName, array $parameters = [], array $libraryPaths = [])
{ {
// Test for empty string // Test for empty string
if (empty($libraryName)) if (empty($libraryName))
@ -136,24 +135,30 @@ class Libraries
return $this->initLibrary($libraryName, $this->libraryClasses[$libraryNameLowerCase], $parameters); return $this->initLibrary($libraryName, $this->libraryClasses[$libraryNameLowerCase], $parameters);
// Try and load from the alternate directory if provided // Try and load from the alternate directory if provided
$paths = (empty($altDirectories) ? $this->componentPaths : $altDirectories); $libraryPaths = (empty($libraryPaths) ? $this->componentPaths : [3 => $libraryPaths]);
// Try and find the library in the libraryPaths // Try and find the library in the libraryPaths
foreach ($paths as $path) for ($i=Priority::getHighestPriority(); $i<=Priority::getLowestPriority(); $i++)
{ {
// First look if a .php file exists in the libraryPath if (!isset($libraryPaths[$i]))
$classFile = $path . DS . $libraryFilename . '.php'; continue;
if (file_exists($classFile))
{
require_once($classFile);
return $this->initLibrary($libraryName, $libraryClassname);
}
$classFile = $path . DS . $libraryFilename . DS . $libraryFilename . '.php'; foreach ($libraryPaths[$i] as $path)
if (file_exists($classFile))
{ {
require_once($classFile); // First look if a .php file exists in the libraryPath
return $this->initLibrary($libraryName, $libraryClassname); $classFile = $path . DS . $libraryFilename . '.php';
if (file_exists($classFile))
{
require_once($classFile);
return $this->initLibrary($libraryName, $libraryClassname);
}
$classFile = $path . DS . $libraryFilename . DS . $libraryFilename . '.php';
if (file_exists($classFile))
{
require_once($classFile);
return $this->initLibrary($libraryName, $libraryClassname);
}
} }
} }

View File

@ -95,7 +95,6 @@ class Plugins
public function __construct() public function __construct()
{ {
$this->cfg = Factory::getInstance()->config->plugins; $this->cfg = Factory::getInstance()->config->plugins;
$this->componentPaths = Core::$appDirs;
} }
/** /**
@ -104,49 +103,55 @@ class Plugins
public function loadHeadersFromPluginPaths() public function loadHeadersFromPluginPaths()
{ {
// Cycle through all pluginPaths // Cycle through all pluginPaths
foreach ($this->componentPaths as $pluginPath) { for ($i=Priority::getHighestPriority(); $i<=Priority::getLowestPriority(); $i++)
{
// If directory does not exist, skip it if (!isset($this->componentPaths[$i]))
if (!file_exists($pluginPath) || !is_dir($pluginPath)) continue;
{
continue;
}
// Fetch the contents of the path foreach ($this->componentPaths[$i] as $pluginPath) {
$pluginPathContents = array_diff(scandir($pluginPath), array('..', '.'));
// Now go through each entry in the plugin folder
foreach ($pluginPathContents as $pluginFolder) {
// @codeCoverageIgnoreStart
if (!is_dir($pluginPath . DS . $pluginFolder))
{
continue;
}
// @codeCoverageIgnoreEnd
// If a header file exists, use it // If directory does not exist, skip it
$file = $pluginPath . DS . $pluginFolder . DS . 'header.php'; if (!file_exists($pluginPath) || !is_dir($pluginPath))
$pluginFolder = ucfirst($pluginFolder); {
$className = '\FuzeWorks\Plugins\\'.$pluginFolder.'Header'; continue;
if (file_exists($file)) }
{
// Load the header file // Fetch the contents of the path
require_once($file); $pluginPathContents = array_diff(scandir($pluginPath), array('..', '.'));
$header = new $className();
if (!$header instanceof iPluginHeader) // Now go through each entry in the plugin folder
foreach ($pluginPathContents as $pluginFolder) {
// @codeCoverageIgnoreStart
if (!is_dir($pluginPath . DS . $pluginFolder))
{ {
continue; continue;
} }
// @codeCoverageIgnoreEnd
// Load the header // If a header file exists, use it
$this->loadHeader($header); $file = $pluginPath . DS . $pluginFolder . DS . 'header.php';
} $pluginFolder = ucfirst($pluginFolder);
$className = '\FuzeWorks\Plugins\\'.$pluginFolder.'Header';
if (file_exists($file))
{
// Load the header file
require_once($file);
$header = new $className();
if (!$header instanceof iPluginHeader)
{
continue;
}
// If it doesn't exist, skip it // Load the header
continue; $this->loadHeader($header);
} }
} // If it doesn't exist, skip it
continue;
}
}
}
} }
/** /**

View File

@ -47,9 +47,6 @@ foreach ($logs as $log) {
} elseif ($log['type'] == 'ERROR') { } elseif ($log['type'] == 'ERROR') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#f56954;">[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . ' $string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#f56954;">[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '
<span style="float: right">' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>'; <span style="float: right">' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
} elseif ($log['type'] == 'EXCEPTION') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#f56954;">[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '
<span style="float: right">' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
} elseif ($log['type'] == 'WARNING') { } elseif ($log['type'] == 'WARNING') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#f39c12;">[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . ' $string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#f39c12;">[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '
<span style="float: right">' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>'; <span style="float: right">' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';

View File

@ -34,6 +34,7 @@
* @version Version 1.2.0 * @version Version 1.2.0
*/ */
namespace FuzeWorks\Component; namespace FuzeWorks\Component;
use FuzeWorks\ComponentPathsTrait;
use FuzeWorks\Configurator; use FuzeWorks\Configurator;
use FuzeWorks\Factory; use FuzeWorks\Factory;
use FuzeWorks\iComponent; use FuzeWorks\iComponent;
@ -46,7 +47,7 @@ class TestAddComponentDirectoryComponent implements iComponent
return ['testaddcomponentdirectory' => 'FuzeWorks\Component\TestAddComponentDirectory']; return ['testaddcomponentdirectory' => 'FuzeWorks\Component\TestAddComponentDirectory'];
} }
public function onAddComponent(Configurator $configurator): Configurator public function onAddComponent(Configurator $configurator)
{ {
return $configurator; return $configurator;
} }
@ -64,11 +65,7 @@ class TestAddComponentDirectoryComponent implements iComponent
class TestAddComponentDirectory class TestAddComponentDirectory
{ {
public $variable = 5; use ComponentPathsTrait;
public $directories = [];
public function setDirectories(array $directories) public $variable = 5;
{
$this->directories = $directories;
}
} }

View File

@ -34,5 +34,5 @@
* @version Version 1.2.0 * @version Version 1.2.0
*/ */
return array( return array(
'key' => 'value' 'key' => 'value'
); );

View File

@ -64,14 +64,28 @@ class configTest extends CoreTestAbstract
/** /**
* @depends testGetConfigClass * @depends testGetConfigClass
* @covers \FuzeWorks\Config::getConfig
* @covers \FuzeWorks\Config::loadConfigFile
*/ */
public function testLoadConfig() public function testLoadConfig()
{ {
$this->assertInstanceOf('FuzeWorks\ConfigORM\ConfigORM', $this->config->getConfig('error')); $this->assertInstanceOf('FuzeWorks\ConfigORM\ConfigORM', $this->config->getConfig('error'));
} }
/**
* @depends testLoadConfig
* @covers \FuzeWorks\Config::getConfig
* @covers \FuzeWorks\Config::loadConfigFile
*/
public function testLoadConfigWithAltDirectory()
{
$config = $this->config->getConfig('TestLoadConfigWithAltDirectory', ['test'.DS.'config'.DS.'TestLoadConfigWithAltDirectory'.DS.'SubDirectory']);
$this->assertEquals('value', $config->key);
}
/** /**
* @depends testLoadConfig * @depends testLoadConfig
* @covers \FuzeWorks\Config::loadConfigFile
* @expectedException FuzeWorks\Exception\ConfigException * @expectedException FuzeWorks\Exception\ConfigException
*/ */
public function testFileNotFound() public function testFileNotFound()
@ -81,6 +95,7 @@ class configTest extends CoreTestAbstract
/** /**
* @depends testLoadConfig * @depends testLoadConfig
* @covers \FuzeWorks\Config::loadConfigFile
*/ */
public function testLoadConfigCancel() public function testLoadConfigCancel()
{ {
@ -97,6 +112,7 @@ class configTest extends CoreTestAbstract
/** /**
* @depends testLoadConfig * @depends testLoadConfig
* @covers \FuzeWorks\Config::loadConfigFile
*/ */
public function testLoadConfigIntercept() public function testLoadConfigIntercept()
{ {
@ -152,44 +168,8 @@ class configTest extends CoreTestAbstract
} }
/** /**
* @expectedException FuzeWorks\Exception\ConfigException * @covers \FuzeWorks\Config::getConfig
*/ */
public function testAddComponentPathFail()
{
// Now test if the config can be loaded (hint: it can not)
$this->config->getConfig('testAddComponentPath');
}
/**
* @depends testAddComponentPathFail
*/
public function testaddComponentPath()
{
// Add the configPath
$this->config->addComponentPath('test'.DS.'config'.DS.'TestAddComponentPath');
// And try to load it again
$this->assertInstanceOf('FuzeWorks\ConfigORM\ConfigORM', $this->config->getConfig('testAddComponentPath'));
}
public function testremoveComponentPath()
{
// Test if the path does NOT exist
$this->assertFalse(in_array('test'.DS.'config'.DS.'TestRemoveComponentPath', $this->config->getComponentPaths()));
// Add it
$this->config->addComponentPath('test'.DS.'config'.DS.'TestRemoveComponentPath');
// Assert if it's there
$this->assertTrue(in_array('test'.DS.'config'.DS.'TestRemoveComponentPath', $this->config->getComponentPaths()));
// Remove it
$this->config->removeComponentPath('test'.DS.'config'.DS.'TestRemoveComponentPath');
// And test if it's gone again
$this->assertFalse(in_array('test'.DS.'config'.DS.'TestRemoveComponentPath', $this->config->getComponentPaths()));
}
public function testSameConfigObject() public function testSameConfigObject()
{ {
$config = $this->config->getConfig('testsameconfigobject', array('test'.DS.'config'.DS.'TestSameConfigObject')); $config = $this->config->getConfig('testsameconfigobject', array('test'.DS.'config'.DS.'TestSameConfigObject'));
@ -206,15 +186,4 @@ class configTest extends CoreTestAbstract
$this->assertEquals($config2->key, 'other_value'); $this->assertEquals($config2->key, 'other_value');
} }
public function testSetDirectories()
{
// Add the directory
$directory = 'test' . DS . 'config';
$this->config->setDirectories([$directory]);
// Assert expectations
$expected = array_merge(\FuzeWorks\Core::$appDirs, [$directory]);
$this->assertEquals($expected, $this->config->getComponentPaths());
}
} }

View File

@ -63,7 +63,6 @@ class configuratorTest extends CoreTestAbstract
public function tearDown() public function tearDown()
{ {
Core::$appDirs = [dirname(__DIR__) . '/application'];
Core::$tempDir = dirname(__DIR__) . '/temp'; Core::$tempDir = dirname(__DIR__) . '/temp';
Core::$logDir = dirname(__DIR__) . '/temp'; Core::$logDir = dirname(__DIR__) . '/temp';
} }
@ -192,22 +191,6 @@ class configuratorTest extends CoreTestAbstract
$this->configurator->setTempDirectory('not_exist'); $this->configurator->setTempDirectory('not_exist');
} }
/**
* @depends testCreateContainer
*/
public function testAddAppDirectory()
{
// Create mock filesystem
vfsStream::setup('testAddAppDirectory');
// Add the directory
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->addDirectory(vfsStream::url('testAddAppDirectory')));
// Create container and test if properly set
$this->configurator->createContainer();
$this->assertEquals(Core::$appDirs, [vfsStream::url('testAddAppDirectory')]);
}
/** /**
* @depends testCreateContainer * @depends testCreateContainer
* @depends testAddComponent * @depends testAddComponent
@ -232,7 +215,7 @@ class configuratorTest extends CoreTestAbstract
$this->assertEquals(5, $container->testaddcomponentdirectory->variable); $this->assertEquals(5, $container->testaddcomponentdirectory->variable);
// Verify directory is set // Verify directory is set
$this->assertEquals($container->testaddcomponentdirectory->directories, [vfsStream::url('testAddComponentDirectory')]); $this->assertEquals([vfsStream::url('testAddComponentDirectory')], $container->testaddcomponentdirectory->getComponentPaths());
} }
/* ---------------------------------- Deferred Invocation --------------------------------------- */ /* ---------------------------------- Deferred Invocation --------------------------------------- */

View File

@ -48,11 +48,19 @@ class coreTest extends CoreTestAbstract
// Assert // Assert
$this->assertTrue(class_exists('FuzeWorks\Core')); $this->assertTrue(class_exists('FuzeWorks\Core'));
$this->assertTrue(class_exists('FuzeWorks\Config')); $this->assertTrue(class_exists('FuzeWorks\Config'));
$this->assertTrue(class_exists('FuzeWorks\Logger')); $this->assertTrue(class_exists('FuzeWorks\Configurator'));
$this->assertTrue(trait_exists('FuzeWorks\ComponentPathsTrait'));
$this->assertTrue(class_exists('FuzeWorks\DeferredComponentClass'));
$this->assertTrue(class_exists('FuzeWorks\Event'));
$this->assertTrue(class_exists('FuzeWorks\Events')); $this->assertTrue(class_exists('FuzeWorks\Events'));
$this->assertTrue(class_exists('FuzeWorks\Factory')); $this->assertTrue(class_exists('FuzeWorks\Factory'));
$this->assertTrue(class_exists('FuzeWorks\Helpers')); $this->assertTrue(class_exists('FuzeWorks\Helpers'));
$this->assertTrue(interface_exists('FuzeWorks\iComponent'));
$this->assertTrue(interface_exists('FuzeWorks\iPluginHeader'));
$this->assertTrue(class_exists('FuzeWorks\Libraries')); $this->assertTrue(class_exists('FuzeWorks\Libraries'));
$this->assertTrue(class_exists('FuzeWorks\Logger'));
$this->assertTrue(class_exists('FuzeWorks\Plugins'));
$this->assertTrue(class_exists('FuzeWorks\Priority'));
} }
public function testIsPHP() public function testIsPHP()

View File

@ -55,7 +55,7 @@ class helperTest extends CoreTestAbstract
{ {
// Prepare class // Prepare class
$this->helpers = new Helpers(); $this->helpers = new Helpers();
$this->helpers->setDirectories(['test' . DS . 'helpers']); $this->helpers->setDirectories([3 => ['test' . DS . 'helpers']]);
} }
public function testGetHelpersClass() public function testGetHelpersClass()
@ -94,6 +94,22 @@ class helperTest extends CoreTestAbstract
$this->assertTrue(function_exists('testLoadHelperWithoutSubdirectory')); $this->assertTrue(function_exists('testLoadHelperWithoutSubdirectory'));
} }
/**
* @depends testLoadHelper
* @covers \FuzeWorks\Helpers::load
*/
public function testLoadHelperWithAltDirectory()
{
$this->assertFalse(function_exists('testLoadHelperWithAltDirectory'));
$this->assertTrue($this->helpers->load(
'TestLoadHelperWithAltDirectory',
['test' . DS . 'helpers' . DS . 'TestLoadHelperWithAltDirectory' . DS . 'SubDirectory']
));
$this->assertTrue(function_exists('testLoadHelperWithAltDirectory'));
}
/** /**
* @depends testLoadHelper * @depends testLoadHelper
* @covers \FuzeWorks\Helpers::load * @covers \FuzeWorks\Helpers::load
@ -149,71 +165,4 @@ class helperTest extends CoreTestAbstract
// Test if the function exists now // Test if the function exists now
$this->assertTrue(function_exists('testGetHelper')); $this->assertTrue(function_exists('testGetHelper'));
} }
/**
* @expectedException FuzeWorks\Exception\HelperException
* @covers \FuzeWorks\Helpers::load
*/
public function testAddComponentPathFail()
{
// First test if the function is not loaded yet
$this->assertFalse(function_exists('testAddComponentPathFunction'));
// Now test if the helper can be loaded (hint: it can not)
$this->helpers->load('TestAddComponentPathFail');
}
/**
* @depends testAddComponentPathFail
* @covers \FuzeWorks\Helpers::addComponentPath
* @covers \FuzeWorks\Helpers::getComponentPaths
*/
public function testAddComponentPath()
{
// Add the componentPath
$this->helpers->addComponentPath('test'.DS.'helpers'.DS.'TestAddComponentPath');
// And try to load it again
$this->assertTrue($this->helpers->load('TestAddComponentPath'));
// And test if the function is loaded
$this->assertTrue(function_exists('testAddComponentPathFunction'));
}
/**
* @covers \FuzeWorks\Helpers::removeComponentPath
* @covers \FuzeWorks\Helpers::getComponentPaths
*/
public function testRemoveComponentPath()
{
// Test if the path does NOT exist
$this->assertFalse(in_array('test'.DS.'helpers'.DS.'TestRemoveComponentPath', $this->helpers->getComponentPaths()));
// Add it
$this->helpers->addComponentPath('test'.DS.'helpers'.DS.'TestRemoveComponentPath');
// Assert if it's there
$this->assertTrue(in_array('test'.DS.'helpers'.DS.'TestRemoveComponentPath', $this->helpers->getComponentPaths()));
// Remove it
$this->helpers->removeComponentPath('test'.DS.'helpers'.DS.'TestRemoveComponentPath');
// And test if it's gone again
$this->assertFalse(in_array('test'.DS.'helpers'.DS.'TestRemoveComponentPath', $this->helpers->getComponentPaths()));
}
/**
* @covers \FuzeWorks\Helpers::setDirectories
* @covers \FuzeWorks\Helpers::getComponentPaths
*/
public function testSetDirectories()
{
// Add the directory
$directory = 'test' . DS . 'helpers';
$this->helpers->setDirectories([$directory]);
// Assert expectations
$expected = array_merge(\FuzeWorks\Core::$appDirs, ['test' . DS . 'helpers', $directory]);
$this->assertEquals($expected, $this->helpers->getComponentPaths());
}
} }

View File

@ -57,7 +57,7 @@ class libraryTest extends CoreTestAbstract
$this->libraries = new Libraries(); $this->libraries = new Libraries();
// And then set all paths // And then set all paths
$this->libraries->setDirectories(['test'.DS.'libraries']); $this->libraries->setDirectories([3 => ['test'.DS.'libraries']]);
} }
public function testLibrariesClass() public function testLibrariesClass()
@ -65,67 +65,6 @@ class libraryTest extends CoreTestAbstract
$this->assertInstanceOf('FuzeWorks\Libraries', $this->libraries); $this->assertInstanceOf('FuzeWorks\Libraries', $this->libraries);
} }
/* ---------------------------------- ComponentPaths ---------------------------------------------- */
/**
* @depends testLibrariesClass
*/
public function testSetDirectories()
{
// Test initial
$initial = array_merge(Core::$appDirs, ['test'.DS.'libraries']);
$this->assertEquals($initial, $this->libraries->getComponentPaths());
// Add path
$newPath = 'addPath';
$this->libraries->setDirectories([$newPath]);
$initial[] = $newPath;
$this->assertEquals($initial, $this->libraries->getComponentPaths());
}
/**
* @expectedException FuzeWorks\Exception\LibraryException
*/
public function testAddComponentPathFail()
{
// First test if the library is not loaded yet
$this->assertFalse(class_exists('TestAddComponentPathFail', false));
// Now test if the library can be loaded (hint: it can not)
$this->libraries->get('TestAddComponentPathFail');
}
/**
* @depends testAddComponentPathFail
*/
public function testAddComponentPath()
{
// Add the componentPath
$this->libraries->removeComponentPath('test'.DS.'libraries');
$this->libraries->addComponentPath('test'.DS.'libraries'.DS.'TestAddComponentPath');
// And try to load it again
$this->assertInstanceOf('Application\Library\TestAddComponentPath', $this->libraries->get('TestAddComponentPath'));
}
public function testRemoveComponentPath()
{
// Test if the path does NOT exist
$this->assertFalse(in_array('test'.DS.'libraries'.DS.'TestRemoveComponentPath', $this->libraries->getComponentPaths()));
// Add it
$this->libraries->addComponentPath('test'.DS.'libraries'.DS.'TestRemoveComponentPath');
// Assert if it's there
$this->assertTrue(in_array('test'.DS.'libraries'.DS.'TestRemoveComponentPath', $this->libraries->getComponentPaths()));
// Remove it
$this->libraries->removeComponentPath('test'.DS.'libraries'.DS.'TestRemoveComponentPath');
// And test if it's gone again
$this->assertFalse(in_array('test'.DS.'libraries'.DS.'TestRemoveComponentPath', $this->libraries->getComponentPaths()));
}
/* ---------------------------------- Load library from directories ------------------- */ /* ---------------------------------- Load library from directories ------------------- */
/** /**
@ -186,7 +125,7 @@ class libraryTest extends CoreTestAbstract
// Prepare the config file // Prepare the config file
$libraryName = 'TestGetLibraryParametersFromConfig'; $libraryName = 'TestGetLibraryParametersFromConfig';
$libraryDir = 'test'.DS.'libraries'.DS.'TestGetLibraryParametersFromConfig'; $libraryDir = 'test'.DS.'libraries'.DS.'TestGetLibraryParametersFromConfig';
$config = Factory::getInstance()->config->getConfig(strtolower($libraryName), [$libraryDir]); Factory::getInstance()->config->getConfig(strtolower($libraryName), [$libraryDir]);
// Load the library // Load the library
$lib = $this->libraries->get('TestGetLibraryParametersFromConfig'); $lib = $this->libraries->get('TestGetLibraryParametersFromConfig');

View File

@ -180,48 +180,6 @@ class pluginTest extends CoreTestAbstract
$this->plugins->get('testRunInvalidDirectory'); $this->plugins->get('testRunInvalidDirectory');
} }
public function testAddComponentPath()
{
// Add the componentPath
$this->plugins->addComponentPath('test'.DS.'plugins'.DS.'TestAddComponentPath');
// And try to load it again
$this->plugins->loadHeadersFromPluginPaths();
$this->assertInstanceOf('Application\Plugin\ActualPlugin', $this->plugins->get('ActualPlugin'));
}
/**
* @depends testAddComponentPath
*/
public function testRemoveComponentPath()
{
// Test if the path does NOT exist
$this->assertFalse(in_array('test'.DS.'plugins'.DS.'testRemoveComponentPath', $this->plugins->getComponentPaths()));
// Add it
$this->plugins->addComponentPath('test'.DS.'plugins'.DS.'testRemoveComponentPath');
// Assert if it's there
$this->assertTrue(in_array('test'.DS.'plugins'.DS.'testRemoveComponentPath', $this->plugins->getComponentPaths()));
// Remove it
$this->plugins->removeComponentPath('test'.DS.'plugins'.DS.'testRemoveComponentPath');
// And test if it's gone again
$this->assertFalse(in_array('test'.DS.'plugins'.DS.'testRemoveComponentPath', $this->plugins->getComponentPaths()));
}
public function testSetDirectories()
{
// Add the directory
$appDir = Core::$appDirs[0];
$directory = 'test' . DS . 'helpers';
$expected = [$appDir, 'test'.DS.'plugins', $directory];
$this->plugins->setDirectories([$directory]);
$this->assertEquals($expected, $this->plugins->getComponentPaths());
}
public function tearDown() public function tearDown()
{ {
$factory = Factory::getInstance(); $factory = Factory::getInstance();

View File

@ -33,11 +33,12 @@
* *
* @version Version 1.2.0 * @version Version 1.2.0
*/ */
if ( ! function_exists('testAddComponentPathFunction'))
if ( ! function_exists('testLoadHelperWithAltDirectory'))
{ {
function testAddComponentPathFunction($someParameter) function testLoadHelperWithAltDirectory($someParameter)
{ {
return 'SomeResult'; return 'SomeResult';
} }
} }

View File

@ -1,40 +0,0 @@
<?php
/**
* FuzeWorks Framework Core.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2019 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
*/
namespace Application\Library;
class TestAddComponentPath {
}

View File

@ -1,41 +0,0 @@
<?php
/**
* FuzeWorks Framework Core.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2019 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
*/
namespace Application\Plugin;
class ActualPlugin
{
}

View File

@ -1,50 +0,0 @@
<?php
/**
* FuzeWorks Framework Core.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2019 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
*/
namespace FuzeWorks\Plugins;
use FuzeWorks\iPluginHeader;
class ActualPluginHeader implements iPluginHeader
{
public function getName(): string
{
return "ActualPlugin";
}
public function init()
{
}
}