factory = Factory::getInstance(); $this->layout = Factory::getInstance()->layouts; $this->layout->reset(); } /** * @covers \FuzeWorks\Layout::init * @covers \FuzeWorks\LayoutComponent::getClasses */ public function testComponent() { // Load the component $component = new FuzeWorks\LayoutComponent(); // Prepare container $configurator = new \FuzeWorks\Configurator(); $configurator->addComponent($component); $configurator->addDirectory(dirname(__DIR__) . '/application'); // Create container $container = $configurator->createContainer(); // Init container $container = $container->init(); $this->assertTrue(property_exists($container, 'layouts')); $this->assertInstanceOf('FuzeWorks\Layout', $container->layouts); } /** * @covers \FuzeWorks\Layout::setFile * @covers \FuzeWorks\Layout::getFile * @covers \FuzeWorks\Layout::setDirectory * @covers \FuzeWorks\Layout::getDirectory */ public function testFileAndDirectory() { // File test $file = 'test.php'; $this->layout->setFile($file); $this->assertEquals($file, $this->layout->getFile()); // Directory test $directory = 'test'.DS.'templates'.DS.'testFileAndDirectory'; $this->layout->setDirectory($directory); $this->assertEquals($directory, $this->layout->getDirectory()); } /** * @covers \FuzeWorks\Layout::getExtensionFromFile */ public function testGetFileExtensions() { // Test getting php files $this->assertEquals('php', $this->layout->getExtensionFromFile('class.test.php')); $this->assertEquals('php', $this->layout->getExtensionFromFile('class.test.org.php')); $this->assertEquals('random', $this->layout->getExtensionFromFile('class.test.something.random')); } /** * @depends testGetFileExtensions * @covers \FuzeWorks\Layout::setFileFromString * @covers \FuzeWorks\Layout::getFileFromString */ public function testGetFilePath() { // Extensions to be used in this test $extensions = array('php', 'json'); // Basic path $this->layout->setFileFromString('test', 'test'.DS.'templates'.DS.'testGetFilePath', $extensions); $this->assertEquals('test'.DS.'templates'.DS.'testGetFilePath'.DS.'layout.test.php', $this->layout->getFile()); $this->assertEquals('test'.DS.'templates'.DS.'testGetFilePath'.DS, $this->layout->getDirectory()); // Alternate file extension $this->layout->setFileFromString('JSON', 'test'.DS.'templates'.DS.'testGetFilePath', $extensions); $this->assertEquals('test'.DS.'templates'.DS.'testGetFilePath'.DS.'layout.JSON.json', $this->layout->getFile()); $this->assertEquals('test'.DS.'templates'.DS.'testGetFilePath'.DS, $this->layout->getDirectory()); // Complex deeper path $this->layout->setFileFromString('Deeper/test', 'test'.DS.'templates'.DS.'testGetFilePath', $extensions); $this->assertEquals('test'.DS.'templates'.DS.'testGetFilePath'.DS.'Deeper'.DS.'layout.test.php', $this->layout->getFile()); $this->assertEquals('test'.DS.'templates'.DS.'testGetFilePath'.DS, $this->layout->getDirectory()); } /** * @depends testGetFilePath * @expectedException FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::setFileFromString * @covers \FuzeWorks\Layout::getFileFromString */ public function testMalformedPaths() { // Extensions to be used in this test $extensions = array('php', 'json'); $this->layout->setFileFromString('test?\/<>', 'test|?/*<>', $extensions); } /** * @expectedException FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::setFileFromString * @covers \FuzeWorks\Layout::getFileFromString */ public function testMissingDirectory() { // Directory that does not exist $this->layout->setFileFromString('test', 'test'.DS.'templates'.DS.'doesNotExist'.DS, array('php')); } /** * @expectedException FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::setFileFromString * @covers \FuzeWorks\Layout::getFileFromString */ public function testMissingFile() { $this->layout->setFileFromString('test', 'test'.DS.'templates'.DS.'testMissingFile'.DS, array('php')); } /** * @expectedException FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::setFileFromString * @covers \FuzeWorks\Layout::getFileFromString */ public function testUnknownFileExtension() { $this->layout->setFileFromString('test', 'test'.DS.'templates'.DS.'testUnknownFileExtension'.DS, array('php')); } /** * @covers \FuzeWorks\Layout::get */ public function testLayoutGet() { // Directory of these tests $directory = 'test'.DS.'templates'.DS.'testLayoutGet'.DS; $this->assertEquals('Retrieved Data', $this->layout->get('test', $directory)); } /** * @covers \FuzeWorks\Layout::get */ public function testLayoutGetRepeat() { $directory = 'test'.DS.'templates'.DS.'testLayoutGetRepeat'.DS; $this->assertEquals('First Data', $this->layout->get('first', $directory)); $this->assertEquals('Second Data', $this->layout->get('second', $directory)); } /** * @covers \FuzeWorks\Layout::get * @covers \FuzeWorks\Event\LayoutLoadEvent::init */ public function testLayoutGetCancelledEvent() { $directory = 'test'.DS.'templates'.DS.'testLayoutGetCancelledEvent'; Events::addListener(function($event){ $event->setCancelled(true); }, 'layoutLoadEvent', EventPriority::NORMAL); $this->assertEquals('cancelled', $this->layout->get('test', $directory)); } /** * @expectedException FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::get * @covers \FuzeWorks\Event\LayoutLoadEvent::init */ public function testLayoutGetEventWrongFile() { $directory = 'test'.DS.'templates'.DS.'testLayoutGetEventWrongFile'; Events::addListener(function($event){ $event->file = 'does_not_exist'; }, 'layoutLoadEvent', EventPriority::NORMAL); $this->layout->get('test', $directory); } /** * @covers \FuzeWorks\Layout::display * @covers \FuzeWorks\Event\LayoutDisplayEvent::init */ public function testLayoutDisplayEventAndDisplay() { // Directory of these tests $directory = 'test'.DS.'templates'.DS.'testLayoutGet'.DS; Events::addListener(function($event){ $this->assertEquals('Retrieved Data', $event->contents); }, 'layoutDisplayEvent', EventPriority::NORMAL); ob_start(); $this->layout->display('test', $directory); $this->assertEquals('Retrieved Data', ob_get_contents()); ob_end_clean(); } /** * @covers \FuzeWorks\Layout::reset * @covers \FuzeWorks\Layout::setTitle * @covers \FuzeWorks\Layout::getTitle */ public function testReset() { // First the the variables $this->layout->setTitle('Test Title'); $this->layout->setDirectory('test'.DS.'templates'.DS.'testLayoutGet'); // Test if they are actually set $this->assertEquals('Test Title', $this->layout->getTitle()); $this->assertEquals('test'.DS.'templates'.DS.'testLayoutGet', $this->layout->getDirectory()); // Reset the layout system $this->layout->reset(); // Test for default values $this->assertFalse($this->layout->getTitle()); $this->assertTrue(strpos($this->layout->getDirectory(), 'application' . DS . 'Layout') !== false); } /** * @covers \FuzeWorks\Layout::getEngineFromExtension */ public function testGetEngineFromExtension() { $this->layout->loadTemplateEngines(); // Test all the default engines $this->assertInstanceOf('FuzeWorks\TemplateEngine\PHPEngine', $this->layout->getEngineFromExtension('php')); $this->assertInstanceOf('FuzeWorks\TemplateEngine\JsonEngine', $this->layout->getEngineFromExtension('json')); $this->assertInstanceOf('FuzeWorks\TemplateEngine\SmartyEngine', $this->layout->getEngineFromExtension('tpl')); } /** * @depends testGetEngineFromExtension * @expectedException FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::getEngineFromExtension */ public function testGetEngineFromExtensionFail() { $this->layout->getEngineFromExtension('faulty'); } /** * @covers \FuzeWorks\Layout::loadTemplateEngines */ public function testLoadTemplateEngines() { // Load first try $this->assertTrue($this->layout->loadTemplateEngines()); // Try second time $this->assertFalse($this->layout->loadTemplateEngines()); // Reset $this->layout->reset(); $this->assertTrue($this->layout->loadTemplateEngines()); } /** * @covers \FuzeWorks\Layout::loadTemplateEngines * @expectedException \FuzeWorks\Exception\LayoutException */ public function testLoadLoadEngineEvent() { Events::addListener(function($event){ $this->assertInstanceOf('\FuzeWorks\Event\NotifierEvent', $event); throw new \FuzeWorks\Exception\EventException('Forcing failure in loadTemplateEngines()'); }, 'layoutLoadEngineEvent', EventPriority::NORMAL); $this->layout->loadTemplateEngines(); } /** * @depends testGetEngineFromExtension * @covers \FuzeWorks\Layout::registerEngine */ 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('test'.DS.'templates'.DS.'testCustomEngine'.DS.'layout.test.test'); // Register the engine $this->layout->registerEngine($mock, 'Custom', array('test')); // And run the engine $this->assertEquals('output', $this->layout->get('test', 'test'.DS.'templates'.DS.'testCustomEngine')); } /** * @depends testCustomEngine * @expectedException \FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::registerEngine */ public function testExistingCustomEngine() { // Create mock $mock = $this->getMockBuilder('FuzeWorks\TemplateEngine\TemplateEngine')->getMock(); $mock->method('get')->willReturn('output'); // And register $this->assertTrue($this->layout->registerEngine($mock, 'Custom', ['test'])); // And re-register $this->layout->registerEngine($mock, 'Custom', ['othertest']); } /** * @depends testCustomEngine * @expectedException \FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::registerEngine */ public function testCustomEngineWithExistingExtensions() { // Create mock $mock = $this->getMockBuilder('FuzeWorks\TemplateEngine\TemplateEngine')->getMock(); $mock->method('get')->willReturn('output'); // Register initial $this->assertTrue($this->layout->registerEngine($mock, 'Custom', ['test'])); // Register failing $this->layout->registerEngine($mock, 'other', ['test']); } /** * @depends testCustomEngine * @covers \FuzeWorks\Layout::setEngine * @covers \FuzeWorks\Layout::getEngine */ public function testSetEngine() { // Create mocks $mock = $this->getMockBuilder('FuzeWorks\TemplateEngine\TemplateEngine')->getMock(); $mock2 = $this->getMockBuilder('FuzeWorks\TemplateEngine\TemplateEngine')->getMock(); $mock->method('get')->willReturn('output'); $mock2->method('get')->willReturn('output2'); // Register custom engine $this->assertTrue($this->layout->registerEngine($mock, 'custom', ['test'])); $this->assertTrue($this->layout->registerEngine($mock2, 'custom2', ['test2'])); // Test getEngine $this->assertInstanceOf(get_class($mock), $this->layout->getEngine('custom')); $this->assertInstanceOf(get_class($mock2), $this->layout->getEngine('custom2')); // Test setEngine1 $this->assertTrue($this->layout->setEngine('custom')); $this->assertTrue($this->layout->setEngine('custom2')); } /** * @depends testSetEngine * @expectedException \FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::setEngine */ public function testSetInvalidEngine() { $this->layout->setEngine('invalid'); } /** * @depends testSetEngine * @expectedException \FuzeWorks\Exception\LayoutException * @covers \FuzeWorks\Layout::getEngine */ public function testGetInvalidEngine() { $this->layout->getEngine('invalid'); } /** * @covers \FuzeWorks\Layout::registerEngine */ public function testEnginesLoadLayout() { // Directory of these tests $directory = 'test'.DS.'templates'.DS.'testEngines'.DS; // First the PHP Engine $this->assertEquals('PHP Template Check', $this->layout->get('php', $directory)); $this->layout->reset(); // Then the JSON Engine $this->assertEquals('JSON Template Check', json_decode($this->layout->get('json', $directory), true)[0]); $this->layout->reset(); // And the Smarty Engine $this->assertEquals('Smarty Template Check', $this->layout->get('smarty', $directory)); } /** * @covers \FuzeWorks\Layout::assign */ public function testEngineVariables() { // Directory of these tests $directory = 'test'.DS.'templates'.DS.'testEngineVariables'.DS; // First the PHP Engine $this->layout->assign('key', 'value'); $this->assertEquals('value', $this->layout->get('php', $directory)); $this->layout->reset(); // Then the JSON Engine $this->layout->assign('key', 'value'); $this->assertEquals('value', json_decode($this->layout->get('json', $directory), true)['data']['key']); $this->layout->reset(); // And the Smarty Engine $this->layout->assign('key', 'value'); $this->assertEquals('value', $this->layout->get('smarty', $directory)); } } class MockEngine { }