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
|
|
|
*
|
2016-07-23 15:06:19 +00:00
|
|
|
* @link http://techfuze.net/fuzeworks
|
2015-10-12 15:28:38 +00:00
|
|
|
* @since Version 0.0.1
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2016-07-23 15:06:19 +00:00
|
|
|
* @version Version 1.0.0
|
2015-10-12 15:28:38 +00:00
|
|
|
*/
|
2016-05-25 15:01:59 +00:00
|
|
|
use FuzeWorks\Layout;
|
2016-06-07 13:09:41 +00:00
|
|
|
use FuzeWorks\Factory;
|
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
|
|
|
{
|
2017-07-14 14:14:47 +00:00
|
|
|
|
|
|
|
protected $factory;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
// Load the factory first
|
|
|
|
$this->factory = Factory::getInstance();
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testGetFileExtensions()
|
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
// Test getting php files
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->assertEquals('php', $this->factory->layout->getExtensionFromFile('class.test.php'));
|
|
|
|
$this->assertEquals('php', $this->factory->layout->getExtensionFromFile('class.test.org.php'));
|
|
|
|
$this->assertEquals('random', $this->factory->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
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->factory->layout->setFileFromString('test', 'tests'.DS.'layout'.DS.'testGetFilePath', $extensions);
|
|
|
|
$this->assertEquals('tests'.DS.'layout'.DS.'testGetFilePath'.DS.'layout.test.php', $this->factory->layout->getFile());
|
|
|
|
$this->assertEquals('tests'.DS.'layout'.DS.'testGetFilePath'.DS, $this->factory->layout->getDirectory());
|
2015-10-12 15:28:38 +00:00
|
|
|
|
|
|
|
// Alternate file extension
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->factory->layout->setFileFromString('JSON', 'tests'.DS.'layout'.DS.'testGetFilePath', $extensions);
|
|
|
|
$this->assertEquals('tests'.DS.'layout'.DS.'testGetFilePath'.DS.'layout.JSON.json', $this->factory->layout->getFile());
|
|
|
|
$this->assertEquals('tests'.DS.'layout'.DS.'testGetFilePath'.DS, $this->factory->layout->getDirectory());
|
2015-10-12 15:28:38 +00:00
|
|
|
|
|
|
|
// Complex deeper path
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->factory->layout->setFileFromString('Deeper/test', 'tests'.DS.'layout'.DS.'testGetFilePath', $extensions);
|
|
|
|
$this->assertEquals('tests'.DS.'layout'.DS.'testGetFilePath'.DS.'Deeper'.DS.'layout.test.php', $this->factory->layout->getFile());
|
|
|
|
$this->assertEquals('tests'.DS.'layout'.DS.'testGetFilePath'.DS, $this->factory->layout->getDirectory());
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-25 15:01:59 +00:00
|
|
|
* @depends testGetFilePath
|
2016-07-11 10:44:13 +00:00
|
|
|
* @expectedException FuzeWorks\Exception\LayoutException
|
2016-05-25 15:01:59 +00:00
|
|
|
*/
|
|
|
|
public function testMalformedPaths()
|
|
|
|
{
|
|
|
|
// Extensions to be used in this test
|
|
|
|
$extensions = array('php', 'json');
|
|
|
|
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->setFileFromString('test?\/<>', 'test|?/*<>', $extensions);
|
2016-05-25 15:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-11 10:44:13 +00:00
|
|
|
* @expectedException FuzeWorks\Exception\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
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->factory->layout->setFileFromString('test', 'tests'.DS.'layout'.DS.'doesNotExist'.DS, array('php'));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-11 10:44:13 +00:00
|
|
|
* @expectedException FuzeWorks\Exception\LayoutException
|
2015-10-12 15:28:38 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testMissingFile()
|
|
|
|
{
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->factory->layout->setFileFromString('test', 'tests'.DS.'layout'.DS.'testMissingFile'.DS, array('php'));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-11 10:44:13 +00:00
|
|
|
* @expectedException FuzeWorks\Exception\LayoutException
|
2015-10-12 15:28:38 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testUnknownFileExtension()
|
|
|
|
{
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->factory->layout->setFileFromString('test', 'tests'.DS.'layout'.DS.'testUnknownFileExtension'.DS, array('php'));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 15:01:59 +00:00
|
|
|
public function testLayoutGet()
|
|
|
|
{
|
|
|
|
// Directory of these tests
|
2017-12-21 21:18:23 +00:00
|
|
|
$directory = 'tests'.DS.'layout'.DS.'testLayoutGet'.DS;
|
2016-05-25 15:01:59 +00:00
|
|
|
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->assertEquals('Retrieved Data', $this->factory->layout->get('test', $directory));
|
2016-05-25 15:01:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 12:45:02 +00:00
|
|
|
public function testLayoutDisplay()
|
2016-05-25 15:01:59 +00:00
|
|
|
{
|
|
|
|
// Directory of these tests
|
2017-12-21 21:18:23 +00:00
|
|
|
$directory = 'tests'.DS.'layout'.DS.'testLayoutGet'.DS;
|
2016-05-25 15:01:59 +00:00
|
|
|
|
|
|
|
ob_start();
|
2017-09-14 12:45:02 +00:00
|
|
|
$this->factory->layout->display('test', $directory);
|
2016-06-07 13:09:41 +00:00
|
|
|
Factory::getInstance()->output->_display();
|
2016-05-25 15:01:59 +00:00
|
|
|
$output = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
$this->assertEquals('Retrieved Data', $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReset()
|
|
|
|
{
|
|
|
|
// First the the variables
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->setTitle('Test Title');
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->factory->layout->setDirectory('tests'.DS.'layout'.DS.'testLayoutGet');
|
2016-05-25 15:01:59 +00:00
|
|
|
|
|
|
|
// Test if they are actually set
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->assertEquals('Test Title', $this->factory->layout->getTitle());
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->assertEquals('tests'.DS.'layout'.DS.'testLayoutGet', $this->factory->layout->getDirectory());
|
2016-05-25 15:01:59 +00:00
|
|
|
|
|
|
|
// Reset the layout system
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->reset();
|
2016-05-25 15:01:59 +00:00
|
|
|
|
|
|
|
// Test for default values
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->assertFalse($this->factory->layout->getTitle());
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->assertTrue(strpos($this->factory->layout->getDirectory(), 'application' . DS . 'Layout') !== false);
|
2016-05-25 15:01:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testGetEngineFromExtension()
|
|
|
|
{
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->loadTemplateEngines();
|
2015-10-12 15:28:38 +00:00
|
|
|
|
|
|
|
// Test all the default engines
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->assertInstanceOf('FuzeWorks\TemplateEngine\PHPEngine', $this->factory->layout->getEngineFromExtension('php'));
|
|
|
|
$this->assertInstanceOf('FuzeWorks\TemplateEngine\JsonEngine', $this->factory->layout->getEngineFromExtension('json'));
|
|
|
|
$this->assertInstanceOf('FuzeWorks\TemplateEngine\SmartyEngine', $this->factory->layout->getEngineFromExtension('tpl'));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGetEngineFromExtension
|
2016-07-11 10:44:13 +00:00
|
|
|
* @expectedException FuzeWorks\Exception\LayoutException
|
2015-10-12 15:28:38 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testGetEngineFromExtensionFail()
|
|
|
|
{
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->getEngineFromExtension('faulty');
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
2017-12-21 21:18:23 +00:00
|
|
|
$mock->expects($this->once())->method('get')->with('tests'.DS.'layout'.DS.'testCustomEngine'.DS.'layout.test.test');
|
2015-10-12 15:28:38 +00:00
|
|
|
|
|
|
|
// Register the engine
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->registerEngine($mock, 'Custom', array('test'));
|
2015-10-12 15:28:38 +00:00
|
|
|
|
|
|
|
// And run the engine
|
2017-12-21 21:18:23 +00:00
|
|
|
$this->assertEquals('output', $this->factory->layout->get('test', 'tests'.DS.'layout'.DS.'testCustomEngine'));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 15:01:59 +00:00
|
|
|
/**
|
|
|
|
* @depends testCustomEngine
|
2016-07-11 10:44:13 +00:00
|
|
|
* @expectedException FuzeWorks\Exception\LayoutException
|
2016-05-25 15:01:59 +00:00
|
|
|
*/
|
|
|
|
public function testInvalidCustomEngine()
|
2016-05-07 17:22:09 +00:00
|
|
|
{
|
2017-07-14 14:14:47 +00:00
|
|
|
$mock = $this->getMockBuilder(MockEngine::class)->getMock();
|
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
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->registerEngine($mock, 'Custom', array('test'));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 12:45:02 +00:00
|
|
|
public function testEnginesLoadLayout()
|
2016-05-07 17:22:09 +00:00
|
|
|
{
|
2015-10-12 15:28:38 +00:00
|
|
|
// Directory of these tests
|
2017-12-21 21:18:23 +00:00
|
|
|
$directory = 'tests'.DS.'layout'.DS.'testEngines'.DS;
|
2016-05-25 15:01:59 +00:00
|
|
|
|
|
|
|
// First the PHP Engine
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->assertEquals('PHP Template Check', $this->factory->layout->get('php', $directory));
|
|
|
|
$this->factory->layout->reset();
|
2015-10-12 15:28:38 +00:00
|
|
|
|
2016-05-25 15:01:59 +00:00
|
|
|
// Then the JSON Engine
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->assertEquals('JSON Template Check', json_decode($this->factory->layout->get('json', $directory), true)[0]);
|
|
|
|
$this->factory->layout->reset();
|
2016-05-25 15:01:59 +00:00
|
|
|
|
|
|
|
// And the Smarty Engine
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->assertEquals('Smarty Template Check', $this->factory->layout->get('smarty', $directory));
|
2016-05-25 15:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEngineVariables()
|
|
|
|
{
|
|
|
|
// Directory of these tests
|
2017-12-21 21:18:23 +00:00
|
|
|
$directory = 'tests'.DS.'layout'.DS.'testEngineVariables'.DS;
|
2016-05-25 15:01:59 +00:00
|
|
|
|
|
|
|
// First the PHP Engine
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->assign('key', 'value');
|
|
|
|
$this->assertEquals('value', $this->factory->layout->get('php', $directory));
|
|
|
|
$this->factory->layout->reset();
|
2016-05-25 15:01:59 +00:00
|
|
|
|
|
|
|
// Then the JSON Engine
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->assign('key', 'value');
|
|
|
|
$this->assertEquals('value', json_decode($this->factory->layout->get('json', $directory), true)['data']['key']);
|
|
|
|
$this->factory->layout->reset();
|
2016-05-25 15:01:59 +00:00
|
|
|
|
|
|
|
// And the Smarty Engine
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->assign('key', 'value');
|
|
|
|
$this->assertEquals('value', $this->factory->layout->get('smarty', $directory));
|
2015-10-12 15:28:38 +00:00
|
|
|
}
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
2017-07-14 14:14:47 +00:00
|
|
|
|
|
|
|
class MockEngine {
|
|
|
|
|
|
|
|
}
|