From a4b3e7170d9475b09fcaaaa4bdb5bbfe04c118ac Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Mon, 31 Oct 2011 17:07:09 +0400 Subject: [PATCH] Layout class tested --- tests/layout/LayoutTest.php | 135 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 tests/layout/LayoutTest.php diff --git a/tests/layout/LayoutTest.php b/tests/layout/LayoutTest.php new file mode 100644 index 0000000..227796b --- /dev/null +++ b/tests/layout/LayoutTest.php @@ -0,0 +1,135 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-10-31 + * + * Unit tests for Layout class + */ + +require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php'; +require_once dirname(__FILE__) . '/../../app/FrontController.php'; +require_once dirname(__FILE__) . '/../../layout/Layout.php'; + +/** + * @runTestsInSeparateProcesses + */ +class LayoutTest extends PHPUnit_Framework_TestCase +{ + + public function run(PHPUnit_Framework_TestResult $result = NULL) + { + $this->setPreserveGlobalState(false); + return parent::run($result); + } + + public function setUp() + { + if (!class_exists('RouterMock')) { + $this->getMock('Router', array(), array(), 'RouterMock', false); + } + if (!class_exists('PHPView')) { + $this->getMock('PHPView', array('fetch', 'append', 'prepend', 'assign', 'getTemplate'), array(), 'PHPViewMock', false); + } + + set_new_overload(array($this, 'newCallback')); + } + + public function testConstruct() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + $layout = $this->getMockForAbstractClass('Layout'); + $this->assertAttributeInstanceOf('PHPView', 'view', $layout); + } + + public function testFetch() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + $layout = $this->getMockForAbstractClass('Layout'); + + $action = $this->getMock('Action', array('fetch')); + $action->expects($this->once()) + ->method('fetch') + ->will($this->returnValue('this is the result of Action::fetch()')); + + $this->assertNull($layout->fetch($action)); + } + + public function testFetchWithTemplate() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + $layout = $this->getMockForAbstractClass('Layout'); + + $class = new ReflectionClass('Layout'); + $template = $class->getProperty('template'); + $template->setAccessible(TRUE); + + $this->assertAttributeEquals(null, 'template', $layout); + + $template->setValue($layout, 'template'); + + $this->assertAttributeEquals('template', 'template', $layout); + + $action = $this->getMock('Action', array('fetch')); + $action->expects($this->once()) + ->method('fetch') + ->will($this->returnValue('this is the result of Action::fetch()')); + + $this->assertNull($layout->fetch($action)); + } + + public function testAppend() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + $layout = $this->getMockForAbstractClass('Layout'); + $action = $this->getMock('Action', array('fetch')); + + $class = new ReflectionClass('Layout'); + + $method = $class->getMethod('append'); + $method->setAccessible(true); + $method->invoke($layout, 'var', $action); + + $method = $class->getMethod('prepend'); + $method->setAccessible(true); + $method->invoke($layout, 'var', $action); + + $method = $class->getMethod('assign'); + $method->setAccessible(true); + $method->invoke($layout, 'var', $action); + } + + protected function newCallback($className) + { + switch ($className) { + case 'Router': + return 'RouterMock'; + case 'PHPView': + return 'PHPViewMock'; + default: + return $className; + } + } + + public function tearDown() + { +// if (defined('DEBUG')) { +// $debug = DEBUG ? 'TRUE' : 'FALSE'; +// echo PHP_EOL . __CLASS__ . ' DEBUG = ' . $debug . PHP_EOL; +// } else { +// echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL; +// } + unset_new_overload(); + } +} \ No newline at end of file