2015-10-12 15:45:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* FuzeWorks.
|
2015-10-12 15:45:56 +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:45:56 +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:45:56 +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:45:56 +00:00
|
|
|
*/
|
2016-05-25 15:01:59 +00:00
|
|
|
use FuzeWorks\Events;
|
2017-07-14 14:14:47 +00:00
|
|
|
use FuzeWorks\Factory;
|
2016-05-25 15:01:59 +00:00
|
|
|
use FuzeWorks\EventPriority;
|
2015-10-12 15:45:56 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-14 12:45:02 +00:00
|
|
|
* Class LayoutLoadEventTest.
|
2015-10-12 15:45:56 +00:00
|
|
|
*/
|
2017-09-14 12:45:02 +00:00
|
|
|
class layoutLoadEventTest extends CoreTestAbstract
|
2016-05-07 17:22:09 +00:00
|
|
|
{
|
2017-07-14 14:14:47 +00:00
|
|
|
|
|
|
|
protected $factory;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
// Load the factory first
|
|
|
|
$this->factory = Factory::getInstance();
|
|
|
|
}
|
|
|
|
|
2015-10-12 15:45:56 +00:00
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* Check if the event is fired when it should be.
|
2015-10-12 15:45:56 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function test_basic()
|
|
|
|
{
|
2017-09-14 12:45:02 +00:00
|
|
|
$mock = $this->getMockBuilder(MockLayoutEventTest::class)->setMethods(['mockMethod'])->getMock();
|
2015-10-12 15:45:56 +00:00
|
|
|
$mock->expects($this->once())->method('mockMethod');
|
|
|
|
|
2017-09-14 12:45:02 +00:00
|
|
|
Events::addListener(array($mock, 'mockMethod'), 'layoutLoadEvent', EventPriority::NORMAL);
|
2015-10-12 15:45:56 +00:00
|
|
|
|
|
|
|
// And run the test
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->get('home');
|
2015-10-12 15:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* Intercept and change the event.
|
|
|
|
*
|
2016-07-11 10:44:13 +00:00
|
|
|
* @expectedException FuzeWorks\Exception\LayoutException
|
2015-10-12 15:45:56 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function test_change()
|
|
|
|
{
|
2017-09-14 12:45:02 +00:00
|
|
|
Events::addListener(array($this, 'listener_change'), 'layoutLoadEvent', EventPriority::NORMAL);
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->factory->layout->get('home');
|
2015-10-12 15:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change title from new to other
|
2016-05-07 17:22:09 +00:00
|
|
|
public function listener_change($event)
|
|
|
|
{
|
2015-10-12 15:45:56 +00:00
|
|
|
|
|
|
|
// This controller should not exist
|
2017-09-14 12:45:02 +00:00
|
|
|
$this->assertTrue(strpos($event->file, 'application/Layout/layout.home.php') !== false);
|
|
|
|
$this->assertTrue(strpos($event->directory, 'application/Layout/') !== false);
|
2015-10-12 15:45:56 +00:00
|
|
|
|
|
|
|
// It should exist now
|
2017-09-14 12:45:02 +00:00
|
|
|
$event->file = $event->directory . 'layout.test.not_found';
|
2016-05-07 17:22:09 +00:00
|
|
|
|
2015-10-12 15:45:56 +00:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* Cancel events.
|
2015-10-12 15:45:56 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function test_cancel()
|
|
|
|
{
|
2015-10-12 15:45:56 +00:00
|
|
|
|
|
|
|
// Listen for the event and cancel it
|
2017-09-14 12:45:02 +00:00
|
|
|
Events::addListener(array($this, 'listener_cancel'), 'layoutLoadEvent', EventPriority::NORMAL);
|
2017-07-14 14:14:47 +00:00
|
|
|
$this->assertFalse($this->factory->layout->get('home'));
|
2015-10-12 15:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel all calls
|
2016-05-07 17:22:09 +00:00
|
|
|
public function listener_cancel($event)
|
|
|
|
{
|
2015-10-12 15:45:56 +00:00
|
|
|
$event->setCancelled(true);
|
|
|
|
}
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
2017-07-14 14:14:47 +00:00
|
|
|
|
2017-09-14 12:45:02 +00:00
|
|
|
class MockLayoutEventTest {
|
2017-07-14 14:14:47 +00:00
|
|
|
public function MockMethod() {}
|
|
|
|
}
|