Merge branch 'Dev' into 'master'

Layout Unit Tests

Implemented the unit tests for the layout manager.

See merge request !41
This commit is contained in:
Abel Hoogeveen 2015-10-13 20:35:09 +02:00
commit ea3661d3be
14 changed files with 351 additions and 15 deletions

View File

@ -29,7 +29,6 @@
*/
namespace FuzeWorks;
use \Smarty;
use \FuzeWorks\TemplateEngine\JSONEngine;
use \FuzeWorks\TemplateEngine\PHPEngine;
use \FuzeWorks\TemplateEngine\SmartyEngine;
@ -43,6 +42,18 @@ use \FuzeWorks\TemplateEngine\TemplateEngine;
*/
class Layout {
/**
* The file to be loaded by the layout manager
* @var null|string
*/
public static $file = null;
/**
* The directory of the file to be loaded by the layout manager
* @var null|string
*/
public static $directory = null;
/**
* All assigned currently assigned to the template
* @var Array Associative Assigned Variable Array
@ -82,12 +93,12 @@ class Layout {
* Remember that doing so will result in a LayoutException when multiple compatible files are found.
* @param String $file File to load
* @param string $directory Directory to load it from
* @return Boolean true on success
* @return void
* @throws LayoutException On error
*/
public static function view($file, $directory = 'Application/Views') {
echo self::get($file, $directory);
return true;
return;
}
@ -111,13 +122,13 @@ class Layout {
// First retrieve the filepath
if (is_null(self::$current_engine)) {
$file = self::getFileFromString($file, $directory, array_keys(self::$file_extensions));
self::setFileFromString($file, $directory, array_keys(self::$file_extensions));
} else {
$file = self::getFileFromString($file, $directory, self::$current_engine->getFileExtensions());
self::setFileFromString($file, $directory, self::$current_engine->getFileExtensions());
}
// Then assign some basic variables for the template
self::$assigned_variables['viewDir'] = Config::get('main')->SITE_URL . preg_replace('#/+#','/', substr($directory . "/", -strlen($directory . "/") ) );
self::$assigned_variables['viewDir'] = Config::get('main')->SITE_URL . preg_replace('#/+#','/', substr(self::$directory . "/", -strlen(self::$directory . "/") ) );
self::$assigned_variables['siteURL'] = Config::get('main')->SITE_URL;
self::$assigned_variables['siteLogo'] = Config::get('main')->SITE_LOGO_URL;
self::$assigned_variables['serverName'] = Config::get('main')->SERVER_NAME;
@ -127,13 +138,13 @@ class Layout {
// Select an engine if one is not already selected
if (is_null(self::$current_engine)) {
self::$current_engine = self::getEngineFromExtension( self::getExtensionFromFile($file) );
self::$current_engine = self::getEngineFromExtension( self::getExtensionFromFile(self::$file) );
}
self::$current_engine->setDirectory($directory);
self::$current_engine->setDirectory(self::$directory);
// And run an Event to see what other parts have to say about it
$event = Events::fireEvent('layoutLoadViewEvent', $file, $directory, self::$current_engine, self::$assigned_variables);
$event = Events::fireEvent('layoutLoadViewEvent', self::$file, self::$directory, self::$current_engine, self::$assigned_variables);
// The event has been cancelled
if($event->isCancelled()){
@ -148,7 +159,10 @@ class Layout {
Logger::stopLevel();
// And finally run it
return self::$current_engine->get($event->file, self::$assigned_variables);
if (file_exists($event->file))
return self::$current_engine->get($event->file, self::$assigned_variables);
throw new LayoutException("The requested file was not found", 1);
}
/**
@ -227,12 +241,60 @@ class Layout {
// And choose what to output
if (!$fileSelected) {
Logger::logWarning("Could not select template. No matching file found.");
throw new LayoutException("Could not select template. No matching file found.");
}
return $selectedFile;
}
/**
* Converts a view string to a file using the directory and the used extensions.
* It also sets the file variable of this class.
*
* It will detect wether the file exists and choose a file according to the provided extensions
* @param String $string The string used by a controller. eg: 'dashboard/home'
* @param String $directory The directory to search in for the template
* @param array $extensions Extensions to use for this template. Eg array('php', 'tpl') etc.
* @return String Filepath of the template
* @throws LayoutException On error
*/
public static function setFileFromString($string, $directory, $extensions = array()) {
self::$file = self::getFileFromString($string, $directory, $extensions);
self::$directory = preg_replace('#/+#','/',(!is_null($directory) ? $directory : "Application/Views") . "/");
}
/**
* Get the current file to be loaded
* @return null|string Path to the file
*/
public static function getFile() {
return self::$file;
}
/**
* Set the file to be loaded
* @param string $file Path to the file
*/
public static function setFile($file) {
self::$file = $file;
}
/**
* Get the directory of the file to be loaded
* @return null|string Path to the directory
*/
public static function getDirectory() {
return self::$directory;
}
/**
* Set the directory of the file to be loaded
* @param string $directory Path to the directory
*/
public static function setDirectory($directory) {
self::$directory = $directory;
}
/**
* Assign a variable for the template
* @param String $key Key of the variable
@ -295,7 +357,7 @@ class Layout {
* @return boolean true on success
* @throws \FuzeWorks\LayoutException On error
*/
public static function registerEngine($engineClass, $engineName, $engineFileExtensions) {
public static function registerEngine($engineClass, $engineName, $engineFileExtensions = array()) {
// First check if the engine already exists
if (isset(self::$engines[$engineName])) {
throw new LayoutException("Could not register engine. Engine '".$engineName."' already registered", 1);
@ -332,7 +394,7 @@ class Layout {
/**
* Load the template engines by sending a layoutLoadEngineEvent
*/
private static function loadTemplateEngines() {
public static function loadTemplateEngines() {
if (!self::$engines_loaded) {
Events::fireEvent('layoutLoadEngineEvent');
// Load the engines provided in this file
@ -375,6 +437,7 @@ class Layout {
namespace FuzeWorks\TemplateEngine;
use \FuzeWorks\LayoutException;
use \Smarty;
/**
* Interface that all Template Engines must follow

View File

@ -258,7 +258,6 @@ class Router {
*
* Determines what callable should be loaded and what data matches the route regex.
*
* @todo: Add path to routerRouteEvent
* @param boolean $loadCallable Immediate load the callable when it's route matches
*/
public static function route($loadCallable = true)
@ -311,7 +310,6 @@ class Router {
* @param array Preg matches with the routing path
* @param string The route that matched
* @return boolean Whether or not the callable was satisfied
* @todo add route to routerLoadCallableEvent
*/
public static function loadCallable($matches = array(), $route){

View File

@ -29,6 +29,7 @@
*/
use FuzeWorks\Events;
use FuzeWorks\Layout;
/**
* Class CoreTestAbstract
@ -39,9 +40,12 @@ abstract class CoreTestAbstract extends PHPUnit_Framework_TestCase
{
/**
* Remove all listeners before the next test starts
*
* Reset the layout manager
*/
public function tearDown(){
Events::$listeners = array();
Layout::reset();
}
}

149
tests/core_layoutTest.php Normal file
View File

@ -0,0 +1,149 @@
<?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 - 2015, 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\Core;
use \FuzeWorks\Layout;
use \FuzeWorks\Events;
use \FuzeWorks\TemplateEngine\TemplateEngine;
/**
* Class LayoutTest
*
* This test will test the layout manager and the default TemplateEngines
*/
class LayoutTest extends CoreTestAbstract
{
public function testGetFileExtensions() {
// Test getting php files
$this->assertEquals('php', Layout::getExtensionFromFile('class.test.php'));
$this->assertEquals('php', Layout::getExtensionFromFile('class.test.org.php'));
}
/**
* @depends testGetFileExtensions
*/
public function testGetFilePath(){
// Extensions to be used in this test
$extensions = array('php', 'json');
// Basic path
Layout::setFileFromString('test', 'tests/layout/testGetFilePath/', $extensions);
$this->assertEquals('tests/layout/testGetFilePath/view.test.php', Layout::getFile());
$this->assertEquals('tests/layout/testGetFilePath/', Layout::getDirectory());
// Alternate file extension
Layout::setFileFromString('JSON', 'tests/layout/testGetFilePath/', $extensions);
$this->assertEquals('tests/layout/testGetFilePath/view.JSON.json', Layout::getFile());
$this->assertEquals('tests/layout/testGetFilePath/', Layout::getDirectory());
// Complex deeper path
Layout::setFileFromString('Deeper/test', 'tests/layout/testGetFilePath/', $extensions);
$this->assertEquals('tests/layout/testGetFilePath/Deeper/view.test.php', Layout::getFile());
$this->assertEquals('tests/layout/testGetFilePath/', Layout::getDirectory());
}
/**
* @expectedException \FuzeWorks\LayoutException
*/
public function testMissingDirectory() {
// Directory that does not exist
Layout::setFileFromString('test', 'tests/layout/doesNotExist/', array('php'));
}
/**
* @expectedException \FuzeWorks\LayoutException
*/
public function testMissingFile() {
Layout::setFileFromString('test', 'tests/layout/testMissingFile/', array('php'));
}
/**
* @expectedException \FuzeWorks\LayoutException
*/
public function testUnknownFileExtension() {
Layout::setFileFromString('test', 'tests/layout/testUnknownFileExtension/', array('php'));
}
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'));
}
/**
* @depends testGetEngineFromExtension
* @expectedException \FuzeWorks\LayoutException
*/
public function testGetEngineFromExtensionFail() {
Layout::getEngineFromExtension('faulty');
}
/**
* @depends testGetEngineFromExtension
*/
public function testCustomEngine() {
// Create the engine
$mock = $this->getMockBuilder('\FuzeWorks\TemplateEngine\TemplateEngine')->getMock();
// Add the methods
$mock->method('get')->willReturn('output');
// And listen for usage
$mock->expects($this->once())->method('get')->with('tests/layout/testCustomEngine/view.test.test');
// Register the engine
Layout::registerEngine($mock, 'Custom', array('test'));
// And run the engine
$this->assertEquals('output', Layout::get('test', 'tests/layout/testCustomEngine/'));
}
public function testPHPEngine() {
// Directory of these tests
$directory = 'tests/layout/testEngines/';
$this->assertEquals('PHP Template Check', Layout::get('php', $directory));
}
public function testJSONEngine() {
// Directory of these tests
$directory = 'tests/layout/testEngines/';
$this->assertEquals('JSON Template Check', json_decode(Layout::get('json', $directory), true)[0]);
}
}

View File

@ -1,4 +1,33 @@
<?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 - 2015, 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\Core;
use \FuzeWorks\Models;

View File

@ -0,0 +1,91 @@
<?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 - 2015, 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\Events;
use \FuzeWorks\Layout;
use \FuzeWorks\EventPriority;
/**
* Class LayoutLoadViewEventTest
*/
class LayoutLoadViewEventTest extends CoreTestAbstract{
/**
* Check if the event is fired when it should be
*/
public function test_basic(){
$mock = $this->getMock('MockEvent', array('mockMethod'));
$mock->expects($this->once())->method('mockMethod');
Events::addListener(array($mock, 'mockMethod'), 'layoutLoadViewEvent', EventPriority::NORMAL);
// And run the test
Layout::get('home');
}
/**
* Intercept and change the event
* @expectedException \FuzeWorks\LayoutException
*/
public function test_change(){
Events::addListener(array($this, 'listener_change'), 'layoutLoadViewEvent', EventPriority::NORMAL);
Layout::get('home');
}
// Change title from new to other
public function listener_change($event){
// This controller should not exist
$this->assertEquals('Application/Views/view.home.php', $event->file);
$this->assertEquals('Application/Views/', $event->directory);
// It should exist now
$event->file = 'Application/Views/view.test.not_found';
return $event;
}
/**
* Cancel events
*/
public function test_cancel(){
// Listen for the event and cancel it
Events::addListener(array($this, 'listener_cancel'), 'layoutLoadViewEvent', EventPriority::NORMAL);
$this->assertFalse(Layout::get('home'));
}
// Cancel all calls
public function listener_cancel($event){
$event->setCancelled(true);
}
}

View File

@ -0,0 +1 @@
["JSON Template Check"]

View File

@ -0,0 +1 @@
<?php echo "PHP Template Check"; ?>