<?php
/**
* @author keeper
*/
if ( !defined("APP_INDEX_ROUTE") ) {
define("APP_INDEX_ROUTE", "Test/Route/Index");
}
class Base_Controllers_DispatchTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException Base_Route_Exception
*/
public function testUnknownRouteDispatch()
{
new Base_Route("Path/To/UnknownRoute");
}
/**
* @expectedException Base_Exception_404
*/
public function testUnknownRouteDispatch404()
{
new Base_Route("Path/To/UnknownRoute");
}
/**
* @expectedException Base_Exception
*/
public function testUnknownRouteDispatchBaseException()
{
new Base_Route("Path/To/UnknownRoute");
}
/**
* @covers App_Test_Controllers_Route::ProtectedMethod
* @expectedException Base_Controllers_Dispatch_Exception
*/
public function testDispatchCallProtectedMethod()
{
$dispatch = new Base_Controllers_Dispatch( new Base_Route("Test/Route/ProtectedMethod") );
$this->assertEquals("Test", $dispatch->getRoute()->getModule());
$this->assertEquals("App_Test_Controllers_Route", $dispatch->getRoute()->getController());
$this->assertEquals("ProtectedMethod", $dispatch->getRoute()->getMethod());
$dispatch->call();
}
/**
* @covers App_Test_Controllers_Route::ProtectedMethod
* @expectedException Base_Exception_404
*/
public function testDispatchCallProtectedMethod404()
{
(new Base_Controllers_Dispatch( new Base_Route("Test/Route/ProtectedMethod") ))->call();
}
/**
* @covers App_Test_Controllers_Route::ProtectedMethod
* @expectedException Base_Exception
*/
public function testDispatchCallProtectedMethodBaseException()
{
(new Base_Controllers_Dispatch( new Base_Route("Test/Route/ProtectedMethod") ))->call();
}
public function testCreateExistsDispatchEntity()
{
$dispatch = new Base_Controllers_Dispatch( new Base_Route("Test/Route/Index") );
return $dispatch;
}
/**
* @depends testCreateExistsDispatchEntity
* @covers Base_Controller
*/
public function testDispatchAttributes(Base_Controllers_Dispatch $dispatch)
{
$this->assertInstanceOf("Base_Route", $dispatch->getRoute());
$this->assertInstanceOf("App_Test_Controllers_Route", $dispatch->getControllerEntity() );
$this->assertInstanceOf("Base_Controller", $dispatch->getControllerEntity() );
$this->assertInstanceOf("Base_Controllers_Dispatch", $dispatch->getControllerEntity()->getParentDispatcher() );
}
/**
* @depends testCreateExistsDispatchEntity
*/
public function testDispatchCallMethodMethod(Base_Controllers_Dispatch $dispatch)
{
$call_result = $dispatch->call();
$this->assertInstanceOf("Base_Controller", $call_result);
return $call_result;
}
/**
* @depends testDispatchCallMethodMethod
*/
public function testPreAndPostDispatch(Base_Controller $controller)
{
$this->assertTrue($controller->pre_dispatched);
$this->assertTrue($controller->post_dispatched);
}
public function testCallRewrited()
{
$route = new Base_Route( "/UnitTest/Rewrite/Engine/Testing/44" );
$this->assertEquals("Test/Route/Rewrited", $route->getPath());
$this->assertTrue($route->isRewrited());
$this->assertEquals(2, sizeof($route->getArgs()));
$this->assertTrue($route->getArgs()[0]);
$this->assertEquals(44, $route->getArgs()[1]);
$dispatch = new Base_Controllers_Dispatch( $route );
$dispatch->call();
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test404HtmlDispatch()
{
require_once 'App/Bootstrap.php'; // Bootstrap в Eclipse PHPUnit не подключается при @preserveGlobalState
$dispatch = new Base_Controllers_Dispatch( new Base_Route("Test/Route/Response404") );
$controller = $dispatch->call();
$controller->getLayout()
->setTemplatePath("Tests/App/Layouts/for-layout-test.tpl.php")
->output();
$this->expectOutputString("<html><head><title>Ошибка 404</title></head><body>Ошибка. Страница не найдена</body></html>");
$this->assertEquals(404, http_response_code());
$this->assertContains("Content-Type: application/html; charset=utf8", xdebug_get_headers());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test404XRequestedWithDispatch()
{
require_once 'App/Bootstrap.php'; // Bootstrap в Eclipse PHPUnit не подключается при @preserveGlobalState
$_SERVER['X-REQUESTED-WITH'] = "XMLHttpRequest";
$dispatch = new Base_Controllers_Dispatch( new Base_Route("Test/Route/Response404") );
$controller = $dispatch->call();
$controller->getLayout()
->setTemplatePath("Tests/App/Layouts/for-layout-test.tpl.php")
->output();
$this->expectOutputString(json_encode(array("response_code" => 404, "title" => "Ошибка 404", "content" => "Ошибка. Страница не найдена")));
$this->assertEquals(200, http_response_code());
$this->assertContains("Content-Type: application/json", xdebug_get_headers());
}
}