Implemented unit tests for the layout manager.
This commit is contained in:
parent
b8058572d3
commit
102b35a96b
|
@ -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()){
|
||||
|
@ -227,12 +238,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 +354,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 +391,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 +434,7 @@ class Layout {
|
|||
|
||||
namespace FuzeWorks\TemplateEngine;
|
||||
use \FuzeWorks\LayoutException;
|
||||
use \Smarty;
|
||||
|
||||
/**
|
||||
* Interface that all Template Engines must follow
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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 RouterTest
|
||||
*
|
||||
* This test will test the router
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
["JSON Template Check"]
|
|
@ -0,0 +1 @@
|
|||
<?php echo "PHP Template Check"; ?>
|
Loading…
Reference in New Issue