Implemented new and renewed core tests.

The layout tests have been updated, and Core and events tests have been added.
This commit is contained in:
Abel Hoogeveen 2016-05-25 17:01:59 +02:00
parent 4af73f3b58
commit 0adcdb7449
18 changed files with 350 additions and 46 deletions

View File

@ -144,6 +144,8 @@ class Events
* The Event gets created, passed around and then returned to the issuer.
*
* @param mixed $input Object for direct event, string for system event or notifierEvent
* @todo Implement Application Events
* @todo Implement Directory input for Events from other locations (like Modules)
*
* @return \FuzeWorks\Event The Event
*/

View File

@ -228,6 +228,11 @@ class Layout
{
$directory = preg_replace('#/+#', '/', (!is_null($directory) ? $directory : self::$directory).'/');
if (strpbrk($directory, "\\/?%*:|\"<>") === TRUE || strpbrk($string, "\\/?%*:|\"<>") === TRUE)
{
throw new LayoutException('Could not get file. Invalid file string', 1);
}
if (!file_exists($directory)) {
throw new LayoutException('Could not get file. Directory does not exist', 1);
}
@ -363,6 +368,10 @@ class Layout
*/
public static function getTitle()
{
if (!isset(self::$assigned_variables['title']))
{
return false;
}
return self::$assigned_variables['title'];
}
@ -494,6 +503,12 @@ class Layout
if (!is_null(self::$current_engine)) {
self::$current_engine->reset();
}
// Unload the engines
self::$engines = array();
self::$engines_loaded = false;
self::$file_extensions = array();
self::$current_engine = null;
self::$assigned_variables = array();
self::$directory = 'Application/Views';
@ -800,7 +815,7 @@ class JSONEngine implements TemplateEngine
public function reset()
{
$this->assigned_variables = array();
$this->string_return = true;
self::$string_return = true;
}
public function test($param1, $param2, $param3)

View File

@ -7,6 +7,7 @@
},
"require-dev": {
"phpunit/phpunit": "4.7.*",
"apigen/apigen": "^4.1"
"apigen/apigen": "^4.1",
"mikey179/vfsStream": "1.1.*"
}
}

View File

@ -48,5 +48,6 @@ abstract class CoreTestAbstract extends PHPUnit_Framework_TestCase
{
Events::$listeners = array();
Layout::reset();
Events::enable();
}
}

View File

@ -31,8 +31,9 @@
*/
// Load the abstract
use \FuzeWorks\Config;
use \FuzeWorks\Core;
use FuzeWorks\Config;
use FuzeWorks\Core;
use FuzeWorks\Logger;
require_once 'abstract.coreTestAbstract.php';
require_once 'Core/System/class.core.php';
@ -45,3 +46,19 @@ $cfg = Config::get('error');
$cfg->debug = false;
$cfg->error_reporting = false;
$cfg->commit();
restore_error_handler();
restore_exception_handler();
// Display all errors
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
// Load the vfsStream class either through PEAR installed library or through composer
if ( ! class_exists('vfsStream') && file_exists('vendor/autoload.php'))
{
include_once 'vendor/autoload.php';
class_alias('org\bovigo\vfs\vfsStream', 'vfsStream');
class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory');
class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper');
}

View File

@ -30,6 +30,8 @@
* @version Version 0.0.1
*/
use FuzeWorks\Core;
/**
* Class CoreTest.
*
@ -39,13 +41,29 @@ class coreTest extends CoreTestAbstract
{
public function testCanLoadStartupFiles()
{
// Assert
$this->assertTrue(class_exists('\FuzeWorks\Config'));
$this->assertTrue(class_exists('\FuzeWorks\Logger'));
$this->assertTrue(class_exists('\FuzeWorks\Events'));
$this->assertTrue(class_exists('\FuzeWorks\Router'));
$this->assertTrue(class_exists('\FuzeWorks\Layout'));
$this->assertTrue(class_exists('\FuzeWorks\Models'));
$this->assertTrue(class_exists('FuzeWorks\Core'));
$this->assertTrue(class_exists('FuzeWorks\Config'));
$this->assertTrue(class_exists('FuzeWorks\Logger'));
$this->assertTrue(class_exists('FuzeWorks\Events'));
$this->assertTrue(class_exists('FuzeWorks\Router'));
$this->assertTrue(class_exists('FuzeWorks\Layout'));
$this->assertTrue(class_exists('FuzeWorks\Models'));
$this->assertTrue(class_exists('FuzeWorks\Database'));
$this->assertTrue(class_exists('FuzeWorks\Factory'));
$this->assertTrue(class_exists('FuzeWorks\Helpers'));
$this->assertTrue(class_exists('FuzeWorks\Input'));
$this->assertTrue(class_exists('FuzeWorks\Language'));
$this->assertTrue(class_exists('FuzeWorks\Libraries'));
$this->assertTrue(class_exists('FuzeWorks\Output'));
$this->assertTrue(class_exists('FuzeWorks\Security'));
$this->assertTrue(class_exists('FuzeWorks\URI'));
$this->assertTrue(class_exists('FuzeWorks\UTF8'));
}
public function testIsPHP()
{
$this->assertTrue(Core::isPHP('1.2.0'));
$this->assertFalse(Core::isphp('9999.9.9'));
}
}

166
tests/core_eventsTest.php Normal file
View File

@ -0,0 +1,166 @@
<?php
/**
* FuzeWorks.
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
*
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
*
* @version Version 0.0.1
*/
use FuzeWorks\Router;
use FuzeWorks\Events;
use FuzeWorks\EventPriority;
/**
* Class EventTest.
*
* This test will test Events
*/
class eventsTest extends CoreTestAbstract
{
public function testFireEvent()
{
$mock = $this->getMock('MockEventListener', array('mockMethod'));
$mock->expects($this->once())->method('mockMethod');
Events::addListener(array($mock, 'mockMethod'), 'mockEvent', EventPriority::NORMAL);
Events::fireEvent('mockEvent');
}
/**
* @depends testFireEvent
*/
public function testObjectEvent()
{
$event = $this->getMock('MockEvent');
$listener = $this->getMock('MockEventListener', array('mockListener'));
$listener->expects($this->once())->method('mockListener')->with($this->equalTo($event));
Events::addListener(array($listener, 'mockListener'), get_class($event), EventPriority::NORMAL);
Events::fireEvent($event);
}
/**
* @depends testObjectEvent
*/
public function testVariablePassing()
{
$event = $this->getMock('MockEvent');
$event->key = 'value';
$eventName = get_class($event);
Events::addListener(function($event) {
$this->assertEquals('value', $event->key);
}, $eventName, EventPriority::NORMAL);
Events::fireEvent($event);
}
/**
* @depends testVariablePassing
*/
public function testVariableChanging()
{
// First prepare the event
$event = $this->getMock('MockEvent');
$event->key = 1;
$eventName = get_class($event);
// The first listener, should be called first due to HIGH priority
Events::addListener(function($event) {
$this->assertEquals(1, $event->key);
$event->key = 2;
return $event;
}, $eventName, EventPriority::HIGH);
// The second listener, should be called second due to LOW priority
Events::addListener(function($event) {
$this->assertEquals(2, $event->key);
$event->key = 3;
return $event;
}, $eventName, EventPriority::LOW);
// Fire the event and test if the key is the result of the last listener
Events::fireEvent($event);
$this->assertEquals(3, $event->key);
}
/**
* @depends testFireEvent
*/
public function testRemoveListener()
{
// First add the listener, expect it to be never called
$listener = $this->getMock('MockEventListener', array('mockListener'));
$listener->expects($this->never())->method('mockListener');
Events::addListener(array($listener, 'mockListener'), 'mockEvent', EventPriority::NORMAL);
// Now try and remove it
Events::removeListener(array($listener, 'mockListener'), 'mockEvent', EventPriority::NORMAL);
// And now fire the event
Events::fireEvent('mockEvent');
}
public function testDisable()
{
// First add the listener, expect it to be never called
$listener = $this->getMock('MockEventListener', array('mockListener'));
$listener->expects($this->never())->method('mockListener');
Events::addListener(array($listener, 'mockListener'), 'mockEvent', EventPriority::NORMAL);
// Disable the event syste,
Events::disable();
// And now fire the event
Events::fireEvent('mockEvent');
}
public function testReEnable()
{
// First add the listener, expect it to be never called
$listener = $this->getMock('MockEventListener', array('mockListener'));
$listener->expects($this->once())->method('mockListener');
Events::addListener(array($listener, 'mockListener'), 'mockEvent', EventPriority::NORMAL);
// Disable the event syste,
Events::disable();
// And now fire the event
Events::fireEvent('mockEvent');
// Re-enable it
Events::enable();
// And fire it again, this time expecting to hit the listener
Events::fireEvent('mockEvent');
}
}

View File

@ -29,7 +29,7 @@
*
* @version Version 0.0.1
*/
use \FuzeWorks\Layout;
use FuzeWorks\Layout;
/**
* Class LayoutTest.
@ -43,16 +43,14 @@ class layoutTest extends CoreTestAbstract
// Test getting php files
$this->assertEquals('php', Layout::getExtensionFromFile('class.test.php'));
$this->assertEquals('php', Layout::getExtensionFromFile('class.test.org.php'));
$this->assertEquals('random', Layout::getExtensionFromFile('class.test.something.random'));
}
/**
* @depends testGetFileExtensions
*
* @todo Add malformed paths
*/
public function testGetFilePath()
{
// Extensions to be used in this test
$extensions = array('php', 'json');
@ -73,7 +71,19 @@ class layoutTest extends CoreTestAbstract
}
/**
* @expectedException \FuzeWorks\LayoutException
* @depends testGetFilePath
* @expectedException FuzeWorks\LayoutException
*/
public function testMalformedPaths()
{
// Extensions to be used in this test
$extensions = array('php', 'json');
Layout::setFileFromString('test?\/<>', 'test|?/*<>', $extensions);
}
/**
* @expectedException FuzeWorks\LayoutException
*/
public function testMissingDirectory()
{
@ -82,7 +92,7 @@ class layoutTest extends CoreTestAbstract
}
/**
* @expectedException \FuzeWorks\LayoutException
* @expectedException FuzeWorks\LayoutException
*/
public function testMissingFile()
{
@ -90,26 +100,65 @@ class layoutTest extends CoreTestAbstract
}
/**
* @expectedException \FuzeWorks\LayoutException
* @expectedException FuzeWorks\LayoutException
*/
public function testUnknownFileExtension()
{
Layout::setFileFromString('test', 'tests/layout/testUnknownFileExtension/', array('php'));
}
public function testLayoutGet()
{
// Directory of these tests
$directory = 'tests/layout/testLayoutGet/';
$this->assertEquals('Retrieved Data', Layout::get('test', $directory));
}
public function testLayoutView()
{
// Directory of these tests
$directory = 'tests/layout/testLayoutGet/';
ob_start();
Layout::view('test', $directory);
$output = ob_get_contents();
ob_end_clean();
$this->assertEquals('Retrieved Data', $output);
}
public function testReset()
{
// First the the variables
Layout::setTitle('Test Title');
Layout::setDirectory('tests/layout/testLayoutGet');
// Test if they are actually set
$this->assertEquals('Test Title', Layout::getTitle());
$this->assertEquals('tests/layout/testLayoutGet', Layout::getDirectory());
// Reset the layout system
Layout::reset();
// Test for default values
$this->assertFalse(Layout::getTitle());
$this->assertEquals('Application/Views', Layout::getDirectory());
}
public function testGetEngineFromExtension()
{
Layout::loadTemplateEngines();
// Test all the default engines
$this->assertInstanceOf('\FuzeWorks\TemplateEngine\PHPEngine', Layout::getEngineFromExtension('php'));
$this->assertInstanceOf('\FuzeWorks\TemplateEngine\JSONEngine', Layout::getEngineFromExtension('json'));
$this->assertInstanceOf('\FuzeWorks\TemplateEngine\SmartyEngine', Layout::getEngineFromExtension('tpl'));
$this->assertInstanceOf('FuzeWorks\TemplateEngine\PHPEngine', Layout::getEngineFromExtension('php'));
$this->assertInstanceOf('FuzeWorks\TemplateEngine\JSONEngine', Layout::getEngineFromExtension('json'));
$this->assertInstanceOf('FuzeWorks\TemplateEngine\SmartyEngine', Layout::getEngineFromExtension('tpl'));
}
/**
* @depends testGetEngineFromExtension
* @expectedException \FuzeWorks\LayoutException
* @expectedException FuzeWorks\LayoutException
*/
public function testGetEngineFromExtensionFail()
{
@ -121,9 +170,8 @@ class layoutTest extends CoreTestAbstract
*/
public function testCustomEngine()
{
// Create the engine
$mock = $this->getMockBuilder('\FuzeWorks\TemplateEngine\TemplateEngine')->getMock();
$mock = $this->getMockBuilder('FuzeWorks\TemplateEngine\TemplateEngine')->getMock();
// Add the methods
$mock->method('get')->willReturn('output');
@ -138,21 +186,52 @@ class layoutTest extends CoreTestAbstract
$this->assertEquals('output', Layout::get('test', 'tests/layout/testCustomEngine/'));
}
public function testPHPEngine()
/**
* @depends testCustomEngine
* @expectedException FuzeWorks\LayoutException
*/
public function testInvalidCustomEngine()
{
$mock = $this->getMock('MockEngine');
// Directory of these tests
$directory = 'tests/layout/testEngines/';
$this->assertEquals('PHP Template Check', Layout::get('php', $directory));
// Does not implement FuzeWorks\TemplateEngine\TemplateEngine, this should fail
Layout::registerEngine($mock, 'Custom', array('test'));
}
public function testJSONEngine()
public function testEnginesLoadView()
{
// Directory of these tests
$directory = 'tests/layout/testEngines/';
$directory = 'tests/layout/testEngines/';
// First the PHP Engine
$this->assertEquals('PHP Template Check', Layout::get('php', $directory));
Layout::reset();
// Then the JSON Engine
$this->assertEquals('JSON Template Check', json_decode(Layout::get('json', $directory), true)[0]);
Layout::reset();
// And the Smarty Engine
$this->assertEquals('Smarty Template Check', Layout::get('smarty', $directory));
}
public function testEngineVariables()
{
// Directory of these tests
$directory = 'tests/layout/testEngineVariables/';
// First the PHP Engine
Layout::assign('key', 'value');
$this->assertEquals('value', Layout::get('php', $directory));
Layout::reset();
// Then the JSON Engine
Layout::assign('key', 'value');
$this->assertEquals('value', json_decode(Layout::get('json', $directory), true)['data']['key']);
Layout::reset();
// And the Smarty Engine
Layout::assign('key', 'value');
$this->assertEquals('value', Layout::get('smarty', $directory));
}
}

View File

@ -29,7 +29,7 @@
*
* @version Version 0.0.1
*/
use \FuzeWorks\Router;
use FuzeWorks\Router;
/**
* Class RouterTest.

View File

@ -29,9 +29,9 @@
*
* @version Version 0.0.1
*/
use \FuzeWorks\Core;
use \FuzeWorks\Events;
use \FuzeWorks\EventPriority;
use FuzeWorks\Core;
use FuzeWorks\Events;
use FuzeWorks\EventPriority;
/**
* Class CoreStartEventTest.

View File

@ -29,9 +29,9 @@
*
* @version Version 0.0.1
*/
use \FuzeWorks\Events;
use \FuzeWorks\Layout;
use \FuzeWorks\EventPriority;
use FuzeWorks\Events;
use FuzeWorks\Layout;
use FuzeWorks\EventPriority;
/**
* Class LayoutLoadViewEventTest.
@ -55,7 +55,7 @@ class layoutLoadViewEventTest extends CoreTestAbstract
/**
* Intercept and change the event.
*
* @expectedException \FuzeWorks\LayoutException
* @expectedException FuzeWorks\LayoutException
*/
public function test_change()
{

View File

@ -29,9 +29,9 @@
*
* @version Version 0.0.1
*/
use \FuzeWorks\Events;
use \FuzeWorks\Router;
use \FuzeWorks\EventPriority;
use FuzeWorks\Events;
use FuzeWorks\Router;
use FuzeWorks\EventPriority;
/**
* Class RouterRouteEventTest.

View File

@ -29,9 +29,9 @@
*
* @version Version 0.0.1
*/
use \FuzeWorks\Events;
use \FuzeWorks\Router;
use \FuzeWorks\EventPriority;
use FuzeWorks\Events;
use FuzeWorks\Router;
use FuzeWorks\EventPriority;
/**
* Class RouterSetPathEventTest.

View File

@ -0,0 +1,2 @@
<?php
echo $vars['key'];

View File

@ -0,0 +1 @@
{$key}

View File

@ -0,0 +1 @@
Smarty Template Check

View File

@ -0,0 +1 @@
Retrieved Data