2015-10-12 15:28:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* FuzeWorks.
|
2015-10-12 15:28:38 +00:00
|
|
|
*
|
|
|
|
* 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
|
2016-05-07 17:22:09 +00:00
|
|
|
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
2015-10-12 15:28:38 +00:00
|
|
|
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
|
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2015-10-12 15:28:38 +00:00
|
|
|
* @link http://fuzeworks.techfuze.net
|
|
|
|
* @since Version 0.0.1
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2015-10-12 15:28:38 +00:00
|
|
|
* @version Version 0.0.1
|
|
|
|
*/
|
2016-05-25 15:01:59 +00:00
|
|
|
use FuzeWorks\Layout;
|
2015-10-12 15:28:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* Class LayoutTest.
|
2015-10-12 15:28:38 +00:00
|
|
|
*
|
2015-10-12 15:45:56 +00:00
|
|
|
* This test will test the layout manager and the default TemplateEngines
|
2015-10-12 15:28:38 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
class layoutTest extends CoreTestAbstract
|
2015-10-12 15:28:38 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testGetFileExtensions()
|
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
// Test getting php files
|
|
|
|
$this->assertEquals('php', Layout::getExtensionFromFile('class.test.php'));
|
|
|
|
$this->assertEquals('php', Layout::getExtensionFromFile('class.test.org.php'));
|
2016-05-25 15:01:59 +00:00
|
|
|
$this->assertEquals('random', Layout::getExtensionFromFile('class.test.something.random'));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGetFileExtensions
|
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testGetFilePath()
|
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-25 15:01:59 +00:00
|
|
|
* @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
|
2015-10-12 15:28:38 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testMissingDirectory()
|
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
// Directory that does not exist
|
|
|
|
Layout::setFileFromString('test', 'tests/layout/doesNotExist/', array('php'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-25 15:01:59 +00:00
|
|
|
* @expectedException FuzeWorks\LayoutException
|
2015-10-12 15:28:38 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testMissingFile()
|
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
Layout::setFileFromString('test', 'tests/layout/testMissingFile/', array('php'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-25 15:01:59 +00:00
|
|
|
* @expectedException FuzeWorks\LayoutException
|
2015-10-12 15:28:38 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testUnknownFileExtension()
|
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
Layout::setFileFromString('test', 'tests/layout/testUnknownFileExtension/', array('php'));
|
|
|
|
}
|
|
|
|
|
2016-05-25 15:01:59 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testGetEngineFromExtension()
|
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
Layout::loadTemplateEngines();
|
|
|
|
|
|
|
|
// Test all the default engines
|
2016-05-25 15:01:59 +00:00
|
|
|
$this->assertInstanceOf('FuzeWorks\TemplateEngine\PHPEngine', Layout::getEngineFromExtension('php'));
|
|
|
|
$this->assertInstanceOf('FuzeWorks\TemplateEngine\JSONEngine', Layout::getEngineFromExtension('json'));
|
|
|
|
$this->assertInstanceOf('FuzeWorks\TemplateEngine\SmartyEngine', Layout::getEngineFromExtension('tpl'));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGetEngineFromExtension
|
2016-05-25 15:01:59 +00:00
|
|
|
* @expectedException FuzeWorks\LayoutException
|
2015-10-12 15:28:38 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testGetEngineFromExtensionFail()
|
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
Layout::getEngineFromExtension('faulty');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGetEngineFromExtension
|
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testCustomEngine()
|
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
// Create the engine
|
2016-05-25 15:01:59 +00:00
|
|
|
$mock = $this->getMockBuilder('FuzeWorks\TemplateEngine\TemplateEngine')->getMock();
|
2015-10-12 15:28:38 +00:00
|
|
|
|
|
|
|
// 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/'));
|
|
|
|
}
|
|
|
|
|
2016-05-25 15:01:59 +00:00
|
|
|
/**
|
|
|
|
* @depends testCustomEngine
|
|
|
|
* @expectedException FuzeWorks\LayoutException
|
|
|
|
*/
|
|
|
|
public function testInvalidCustomEngine()
|
2016-05-07 17:22:09 +00:00
|
|
|
{
|
2016-05-25 15:01:59 +00:00
|
|
|
$mock = $this->getMock('MockEngine');
|
2015-10-12 15:28:38 +00:00
|
|
|
|
2016-05-25 15:01:59 +00:00
|
|
|
// Does not implement FuzeWorks\TemplateEngine\TemplateEngine, this should fail
|
|
|
|
Layout::registerEngine($mock, 'Custom', array('test'));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 15:01:59 +00:00
|
|
|
public function testEnginesLoadView()
|
2016-05-07 17:22:09 +00:00
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
// Directory of these tests
|
2016-05-25 15:01:59 +00:00
|
|
|
$directory = 'tests/layout/testEngines/';
|
|
|
|
|
|
|
|
// First the PHP Engine
|
|
|
|
$this->assertEquals('PHP Template Check', Layout::get('php', $directory));
|
|
|
|
Layout::reset();
|
2015-10-12 15:28:38 +00:00
|
|
|
|
2016-05-25 15:01:59 +00:00
|
|
|
// Then the JSON Engine
|
2015-10-12 15:28:38 +00:00
|
|
|
$this->assertEquals('JSON Template Check', json_decode(Layout::get('json', $directory), true)[0]);
|
2016-05-25 15:01:59 +00:00
|
|
|
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));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|