Finished FuzeWorks\Configurator.

All unit tests are implemented and system should be done now.
This commit is contained in:
Abel Hoogeveen 2018-10-05 21:19:27 +02:00
parent 57517511a4
commit 1721869932
No known key found for this signature in database
GPG Key ID: 96C2234920BF4292
8 changed files with 397 additions and 12 deletions

View File

@ -35,6 +35,7 @@
*/
namespace FuzeWorks;
use FuzeWorks\Exception\ConfiguratorException;
use FuzeWorks\Exception\InvalidArgumentException;
use Tracy\Debugger;
@ -145,10 +146,11 @@ class Configurator
/**
* Sets the default timezone.
* @param string $timezone
* @return Configurator
* @throws InvalidArgumentException
*/
public function setTimeZone($timezone)
public function setTimeZone(string $timezone)
{
if (!date_default_timezone_set($timezone))
throw new InvalidArgumentException("Could not set timezone. Invalid timezone provided.", 1);
@ -189,7 +191,7 @@ class Configurator
* Provide a string from where debug mode can be accessed.
* Can be the following type of addresses:
* @todo
* @param string $address
* @param string|array $address
* @return Configurator
* @throws InvalidArgumentException
*/
@ -270,7 +272,7 @@ class Configurator
// Then prepare the debugger
$debug = ($this->parameters['debugEnabled'] && $this->parameters['debugMatch'] ? true : false);
if (!defined('ENVIRONMENT'))
define('ENVIRONMENT', ($debug ? 'DEVELOPMENT' : 'PRODUCTION'));
define('ENVIRONMENT', ($debug ? 'DEVELOPMENT' : 'PRODUCTION')); // @codeCoverageIgnore
// And enable Tracy Debugger
if (class_exists('Tracy\Debugger', true))
@ -288,6 +290,9 @@ class Configurator
// Add all components
foreach ($this->components as $componentName => $componentClass) {
if (!class_exists($componentClass))
throw new ConfiguratorException("Could not load component '".$componentName."'. Class '".$componentClass."' does not exist.", 1);
$container->setInstance($componentName, new $componentClass());
}

View File

@ -0,0 +1,49 @@
<?php
/**
* FuzeWorks Framework Core.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2018 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 - 2018, Techfuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 0.0.1
*
* @version Version 1.2.0
*/
namespace FuzeWorks\Exception;
/**
* Class ConfiguratorException.
*
* @author TechFuze <contact@techfuze.net>
* @copyright Copyright (c) 2013 - 2018, Techfuze. (http://techfuze.net)
*/
class ConfiguratorException extends Exception
{
}
?>

View File

@ -36,6 +36,7 @@
namespace FuzeWorks;
use FuzeWorks\Exception\ConfiguratorException;
use FuzeWorks\Exception\Exception;
/**
@ -278,9 +279,8 @@ class Logger {
$logs = self::$Logs;
require(dirname(__DIR__) . DS . 'Layout' . DS . 'layout.logger_cli.php');
$contents = ob_get_clean();
$file = Core::$logDir . DS . 'Logs' . DS . 'log_latest.php';
if (is_writable($file))
{
$file = Core::$logDir . DS . 'log_latest.php';
if (is_writable(dirname($file))) {
file_put_contents($file, '<?php ' . $contents);
}
}

View File

@ -90,7 +90,7 @@ $id = 1;
printf($mask, 'Id', 'Title', 'Runtime');
printf($mask, $id, getColoredString('FuzeWorks debug log', 'black', 'light_gray'), '0 ms');
foreach ($this->assigned_variables['Logs'] as $log) {
foreach ($logs as $log) {
$id++;
$string = '';

View File

@ -0,0 +1,57 @@
<?php
/**
* FuzeWorks Framework Core.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2018 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 - 2018, 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\Component;
use FuzeWorks\iComponent;
class TestAddComponentDirectoryComponent implements iComponent
{
public function getClasses(): array
{
return ['testaddcomponentdirectory' => 'FuzeWorks\Component\TestAddComponentDirectory'];
}
}
class TestAddComponentDirectory
{
public $variable = 5;
public $directories = [];
public function setDirectories(array $directories)
{
$this->directories = $directories;
}
}

View File

@ -41,7 +41,7 @@ class TestAddComponentFailComponent implements iComponent
public function getClasses(): array
{
return ['test' => 'FuzeWorks\Component\Test'];
return ['test' => 'FuzeWorks\Component\TestAddComponentNotExist'];
}
}

View File

@ -35,7 +35,8 @@
*/
use FuzeWorks\Configurator;
use FuzeWorks\iComponent;
use FuzeWorks\Core;
use FuzeWorks\Logger;
/**
* Class ConfiguratorTest.
@ -58,6 +59,13 @@ class configuratorTest extends CoreTestAbstract
$this->configurator->setTimeZone('Europe/Amsterdam');
}
public function tearDown()
{
Core::$appDirs = [dirname(__DIR__) . '/application'];
Core::$tempDir = dirname(__DIR__) . '/temp';
Core::$logDir = dirname(__DIR__) . '/temp';
}
public function testGetConfiguratorClass()
{
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator);
@ -68,6 +76,8 @@ class configuratorTest extends CoreTestAbstract
$this->assertInstanceOf('FuzeWorks\Factory', $this->configurator->createContainer());
}
/* ---------------------------------- Components ------------------------------------------------ */
/**
* @depends testCreateContainer
*/
@ -76,7 +86,7 @@ class configuratorTest extends CoreTestAbstract
// Load the component
require_once 'tests'.DS.'components'.DS.'testAddComponent'.DS.'TestAddComponent.php';
$component = new FuzeWorks\Component\TestComponent();
$this->configurator->addComponent($component);
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->addComponent($component));
// Create container and test if component is added and has known properties
$container = $this->configurator->createContainer()->init();
@ -87,14 +97,269 @@ class configuratorTest extends CoreTestAbstract
/**
* @depends testAddComponent
* @expectedException FuzeWorks\Exception\ConfiguratorException
*/
public function testAddComponentFail()
{
// Load the component
require_once 'tests'.DS.'components'.DS.'testAddComponent'.DS.'TestAddComponent.php';
$this->assertTrue(true);
require_once 'tests'.DS.'components'.DS.'testAddComponentFail'.DS.'TestAddComponentFail.php';
$component = new FuzeWorks\Component\TestAddComponentFailComponent;
$this->configurator->addComponent($component);
// Create container
$this->configurator->createContainer()->init();
}
/* ---------------------------------- Directories ----------------------------------------------- */
/**
* @depends testCreateContainer
*/
public function testSetLogDirectory()
{
// Create mock filesystem
$fs = vfsStream::setup('testSetLogDirectory');
// Set the directory
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setLogDirectory(vfsStream::url('testSetLogDirectory')));
// Create container and test if properly set
$this->configurator->createContainer()->init();
$this->assertEquals(Core::$logDir, vfsStream::url('testSetLogDirectory'));
// Create a log and write off to file
Logger::log('Test log for the file');
Logger::logToFile();
// Assert if exist
$this->assertTrue($fs->hasChild('log_latest.php'));
}
/**
* @depends testSetLogDirectory
* @expectedException \FuzeWorks\Exception\InvalidArgumentException
*/
public function testSetLogDirectoryNotDirectory()
{
// Set the directory
$this->configurator->setLogDirectory('not_exist');
}
/**
* @depends testCreateContainer
*/
public function testSetTempDirectory()
{
// Create mock filesystem
vfsStream::setup('testSetTempDirectory');
// Set the directory
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setTempDirectory(vfsStream::url('testSetTempDirectory')));
// Create container and test if properly set
$this->configurator->createContainer()->init();
$this->assertEquals(Core::$tempDir, vfsStream::url('testSetTempDirectory'));
}
/**
* @depends testSetTempDirectory
* @expectedException \FuzeWorks\Exception\InvalidArgumentException
*/
public function testSetTempDirectoryNotDirectory()
{
// Set the directory
$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()->init();
$this->assertEquals(Core::$appDirs, [vfsStream::url('testAddAppDirectory')]);
}
/**
* @depends testCreateContainer
* @depends testAddComponent
*/
public function testAddComponentDirectory()
{
// Create mock filesystem
vfsStream::setup('testAddComponentDirectory');
// Add the component
require_once 'tests'.DS.'components'.DS.'testAddComponentDirectory'.DS.'TestAddComponentDirectory.php';
$component = new FuzeWorks\Component\TestAddComponentDirectoryComponent();
$this->configurator->addComponent($component);
// Add the directory
$this->configurator->addDirectory(vfsStream::url('testAddComponentDirectory'), 'testaddcomponentdirectory');
// Create container and test if component is added and has known properties
$container = $this->configurator->createContainer()->init();
$this->assertTrue(property_exists($container, 'testaddcomponentdirectory'));
$this->assertInstanceOf('FuzeWorks\Component\TestAddComponentDirectory', $container->testaddcomponentdirectory);
$this->assertEquals(5, $container->testaddcomponentdirectory->variable);
// Verify directory is set
$this->assertEquals($container->testaddcomponentdirectory->directories, [vfsStream::url('testAddComponentDirectory')]);
}
/* ---------------------------------- Parameters ------------------------------------------------ */
/**
* @depends testCreateContainer
*/
public function testSetTimezone()
{
// Set timezone and verify returns
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setTimeZone('Europe/Amsterdam'));
// Test if properly set
$this->assertEquals('Europe/Amsterdam', ini_get('date.timezone'));
}
/**
* @depends testSetTimezone
* @expectedException \FuzeWorks\Exception\InvalidArgumentException
*/
public function testSetTimezoneInvalid()
{
$this->configurator->setTimeZone('Europe/Amsterdamned');
}
/**
* @depends testCreateContainer
*/
public function testSetParameter()
{
// Set a value that can be verified and test return object
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setParameters(['tempDir' => 'fake_directory']));
// Create container and verify
$this->configurator->createContainer()->init();
$this->assertEquals('fake_directory', Core::$tempDir);
}
/* ---------------------------------- Debugging ------------------------------------------------- */
/**
* @depends testCreateContainer
*/
public function testEnableDebugMode()
{
// Enable debug mode and verify return object
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->enableDebugMode());
// No match has been found yet. Verify that debug is still deactivated
$this->assertFalse($this->configurator->isDebugMode());
// Set a debug address, all in this case; also verify return type
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setDebugAddress('ALL'));
// Match should be found. Verify that debug is activated
$this->assertTrue($this->configurator->isDebugMode());
// Load the container and verify that tracy runs in debug mode
$this->configurator->createContainer()->init();
$this->assertTrue(\Tracy\Debugger::isEnabled());
}
/**
* @depends testEnableDebugMode
*/
public function testDisableDebugMode()
{
// First enable so we can disable
$this->assertFalse($this->configurator->enableDebugMode(false)->isDebugMode());
// Create the container and verify that tracy debug has been disabled
$this->configurator->createContainer()->init();
// Tracy can't be disabled once it's been enabled. Therefor this won't be tested
}
/**
* @depends testEnableDebugMode
*/
public function testSetDebugAddress()
{
// First test return value
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setDebugAddress('ALL'));
// Test address ALL and ENABLED
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress('ALL')->isDebugMode());
// Test address NONE and ENABLED
$this->assertFalse($this->configurator->enableDebugMode()->setDebugAddress('NONE')->isDebugMode());
// Test custom addresses
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress([])->isDebugMode());
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress('192.168.1.1')->isDebugMode());
$_SERVER['REMOTE_ADDR'] = '::1';
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress([])->isDebugMode());
$_SERVER['REMOTE_ADDR'] = '192.168.1.1';
$this->assertFalse($this->configurator->enableDebugMode()->setDebugAddress([])->isDebugMode());
$this->assertFalse($this->configurator->enableDebugMode()->setDebugAddress('192.168.1.1.0')->isDebugMode());
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress('192.168.1.1')->isDebugMode());
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress('a,192.168.1.1,b')->isDebugMode());
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress('a 192.168.1.1 b')->isDebugMode());
// Test for HTTP_X_FORWARDED_FOR
unset($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['REMOTE_ADDR']);
$this->assertFalse($this->configurator->enableDebugMode()->setDebugAddress([])->isDebugMode());
$this->assertFalse($this->configurator->enableDebugMode()->setDebugAddress('127.0.0.1')->isDebugMode());
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress(php_uname('n'))->isDebugMode());
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress([php_uname('n')])->isDebugMode());
// Test for cookie based authentication
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = '192.168.1.1';
$_COOKIE[Configurator::COOKIE_SECRET] = '*secret*';
$this->assertFalse($this->configurator->enableDebugMode()->setDebugAddress([])->isDebugMode());
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress('192.168.1.1')->isDebugMode());
$this->assertFalse($this->configurator->enableDebugMode()->setDebugAddress('abc@192.168.1.1')->isDebugMode());
$this->assertTrue($this->configurator->enableDebugMode()->setDebugAddress('*secret*@192.168.1.1')->isDebugMode());
$_COOKIE[Configurator::COOKIE_SECRET] = ['*secret*'];
$this->assertFalse($this->configurator->enableDebugMode()->setDebugAddress('*secret*@192.168.1.1')->isDebugMode());
// Unset
unset($_COOKIE[Configurator::COOKIE_SECRET], $_SERVER['REMOTE_ADDR']);
}
/**
* @depends testEnableDebugMode
* @expectedException \FuzeWorks\Exception\InvalidArgumentException
*/
public function testSetDebugAddressInvalidArgument()
{
$this->configurator->setDebugAddress(null);
}
/**
* @depends testEnableDebugMode
*/
public function testSetDebugEmail()
{
// Set email and verify return value
$this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setDebugEmail('test@email.com'));
// Create container and test Tracy for set address
$this->configurator->createContainer()->init();
$this->assertEquals('test@email.com', \Tracy\Debugger::$email);
}
}

View File

@ -43,6 +43,7 @@ use FuzeWorks\Exception\HelperException;
use FuzeWorks\Exception\InvalidArgumentException;
use FuzeWorks\Exception\LibraryException;
use FuzeWorks\Exception\LoggerException;
use FuzeWorks\Exception\ConfiguratorException;
/**
* Class ExceptionTest.
@ -124,4 +125,12 @@ class exceptionTestTest extends CoreTestAbstract
throw new LoggerException("Exception Test Run", 1);
}
/**
* @expectedException FuzeWorks\Exception\ConfiguratorException
*/
public function testConfiguratorException()
{
throw new ConfiguratorException("Exception Test Run", 1);
}
}