fixing all appframework unit tests

This commit is contained in:
Thomas Müller 2013-10-07 11:25:50 +02:00
parent aefea2a408
commit 1e5012fc1d
7 changed files with 89 additions and 142 deletions

View File

@ -40,7 +40,7 @@ class AppTest extends \PHPUnit_Framework_TestCase {
protected function setUp() { protected function setUp() {
$this->container = new \OC\AppFramework\DependencyInjection\DIContainer('test'); $this->container = new \OC\AppFramework\DependencyInjection\DIContainer('test');
$this->controller = $this->getMockBuilder( $this->controller = $this->getMockBuilder(
'OC\AppFramework\Controller\Controller') 'OCP\AppFramework\Controller\Controller')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->dispatcher = $this->getMockBuilder( $this->dispatcher = $this->getMockBuilder(

View File

@ -25,13 +25,10 @@
namespace Test\AppFramework\Controller; namespace Test\AppFramework\Controller;
use OC\AppFramework\Http\Request; use OC\AppFramework\Http\Request;
use OC\AppFramework\Controller\Controller; use OCP\AppFramework\Controller\Controller;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
//require_once __DIR__ . "/../classloader.php";
class ChildController extends Controller {}; class ChildController extends Controller {};
class ControllerTest extends \PHPUnit_Framework_TestCase { class ControllerTest extends \PHPUnit_Framework_TestCase {
@ -40,7 +37,7 @@ class ControllerTest extends \PHPUnit_Framework_TestCase {
* @var Controller * @var Controller
*/ */
private $controller; private $controller;
private $api; private $app;
protected function setUp(){ protected function setUp(){
$request = new Request( $request = new Request(
@ -55,13 +52,13 @@ class ControllerTest extends \PHPUnit_Framework_TestCase {
) )
); );
$this->api = $this->getMock('OC\AppFramework\Core\API', $this->app = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
array('getAppName'), array('test')); array('getAppName'), array('test'));
$this->api->expects($this->any()) $this->app->expects($this->any())
->method('getAppName') ->method('getAppName')
->will($this->returnValue('apptemplate_advanced')); ->will($this->returnValue('apptemplate_advanced'));
$this->controller = new ChildController($this->api, $request); $this->controller = new ChildController($this->app, $request);
} }
@ -114,26 +111,6 @@ class ControllerTest extends \PHPUnit_Framework_TestCase {
} }
public function testRenderRenderAs(){
$ocTpl = $this->getMock('Template', array('fetchPage'));
$ocTpl->expects($this->once())
->method('fetchPage');
$api = $this->getMock('OC\AppFramework\Core\API',
array('getAppName', 'getTemplate'), array('app'));
$api->expects($this->any())
->method('getAppName')
->will($this->returnValue('app'));
$api->expects($this->once())
->method('getTemplate')
->with($this->equalTo('home'), $this->equalTo('admin'), $this->equalTo('app'))
->will($this->returnValue($ocTpl));
$this->controller = new ChildController($api, new Request());
$this->controller->render('home', array(), 'admin')->render();
}
public function testRenderHeaders(){ public function testRenderHeaders(){
$headers = array('one', 'two'); $headers = array('one', 'two');
$response = $this->controller->render('', array(), '', $headers); $response = $this->controller->render('', array(), '', $headers);

View File

@ -44,8 +44,8 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase {
protected function setUp() { protected function setUp() {
$this->controllerMethod = 'test'; $this->controllerMethod = 'test';
$api = $this->getMockBuilder( $app = $this->getMockBuilder(
'\OC\AppFramework\Core\API') 'OC\AppFramework\DependencyInjection\DIContainer')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$request = $this->getMockBuilder( $request = $this->getMockBuilder(
@ -62,8 +62,8 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->controller = $this->getMock( $this->controller = $this->getMock(
'\OC\AppFramework\Controller\Controller', '\OCP\AppFramework\Controller\Controller',
array($this->controllerMethod), array($api, $request)); array($this->controllerMethod), array($app, $request));
$this->dispatcher = new Dispatcher( $this->dispatcher = new Dispatcher(
$this->http, $this->middlewareDispatcher); $this->http, $this->middlewareDispatcher);

View File

@ -63,93 +63,33 @@ class TemplateResponseTest extends \PHPUnit_Framework_TestCase {
} }
public function testRender(){ // public function testRender(){
$ocTpl = $this->getMock('Template', array('fetchPage')); // $ocTpl = $this->getMock('Template', array('fetchPage'));
$ocTpl->expects($this->once()) // $ocTpl->expects($this->once())
->method('fetchPage'); // ->method('fetchPage');
//
$api = $this->getMock('OC\AppFramework\Core\API', // $tpl = new TemplateResponse('core', 'error');
array('getAppName', 'getTemplate'), array('app')); //
$api->expects($this->any()) // $tpl->render();
->method('getAppName') // }
->will($this->returnValue('app')); //
$api->expects($this->once()) //
->method('getTemplate') // public function testRenderAssignsParams(){
->with($this->equalTo('home'), $this->equalTo('user'), $this->equalTo('app')) // $params = array('john' => 'doe');
->will($this->returnValue($ocTpl)); //
// $tpl = new TemplateResponse('app', 'home');
$tpl = new TemplateResponse($api, 'home'); // $tpl->setParams($params);
//
$tpl->render(); // $tpl->render();
} // }
//
//
public function testRenderAssignsParams(){ // public function testRenderDifferentApp(){
$params = array('john' => 'doe'); //
// $tpl = new TemplateResponse('app', 'home', 'app2');
$ocTpl = $this->getMock('Template', array('assign', 'fetchPage')); //
$ocTpl->expects($this->once()) // $tpl->render();
->method('assign') // }
->with($this->equalTo('john'), $this->equalTo('doe'));
$api = $this->getMock('OC\AppFramework\Core\API',
array('getAppName', 'getTemplate'), array('app'));
$api->expects($this->any())
->method('getAppName')
->will($this->returnValue('app'));
$api->expects($this->once())
->method('getTemplate')
->with($this->equalTo('home'), $this->equalTo('user'), $this->equalTo('app'))
->will($this->returnValue($ocTpl));
$tpl = new TemplateResponse($api, 'home');
$tpl->setParams($params);
$tpl->render();
}
public function testRenderDifferentApp(){
$ocTpl = $this->getMock('Template', array('fetchPage'));
$ocTpl->expects($this->once())
->method('fetchPage');
$api = $this->getMock('OC\AppFramework\Core\API',
array('getAppName', 'getTemplate'), array('app'));
$api->expects($this->any())
->method('getAppName')
->will($this->returnValue('app'));
$api->expects($this->once())
->method('getTemplate')
->with($this->equalTo('home'), $this->equalTo('user'), $this->equalTo('app2'))
->will($this->returnValue($ocTpl));
$tpl = new TemplateResponse($api, 'home', 'app2');
$tpl->render();
}
public function testRenderDifferentRenderAs(){
$ocTpl = $this->getMock('Template', array('fetchPage'));
$ocTpl->expects($this->once())
->method('fetchPage');
$api = $this->getMock('OC\AppFramework\Core\API',
array('getAppName', 'getTemplate'), array('app'));
$api->expects($this->any())
->method('getAppName')
->will($this->returnValue('app'));
$api->expects($this->once())
->method('getTemplate')
->with($this->equalTo('home'), $this->equalTo('admin'), $this->equalTo('app'))
->will($this->returnValue($ocTpl));
$tpl = new TemplateResponse($api, 'home');
$tpl->renderAs('admin');
$tpl->render();
}
public function testGetRenderAs(){ public function testGetRenderAs(){

View File

@ -122,13 +122,13 @@ class MiddlewareDispatcherTest extends \PHPUnit_Framework_TestCase {
private function getAPIMock(){ private function getAPIMock(){
return $this->getMock('OC\AppFramework\Core\API', return $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
array('getAppName'), array('app')); array('getAppName'), array('app'));
} }
private function getControllerMock(){ private function getControllerMock(){
return $this->getMock('OC\AppFramework\Controller\Controller', array('method'), return $this->getMock('OCP\AppFramework\Controller\Controller', array('method'),
array($this->getAPIMock(), new Request())); array($this->getAPIMock(), new Request()));
} }

View File

@ -44,10 +44,10 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase {
protected function setUp(){ protected function setUp(){
$this->middleware = new ChildMiddleware(); $this->middleware = new ChildMiddleware();
$this->api = $this->getMock('OC\AppFramework\Core\API', $this->api = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
array(), array('test')); array(), array('test'));
$this->controller = $this->getMock('OC\AppFramework\Controller\Controller', $this->controller = $this->getMock('OCP\AppFramework\Controller\Controller',
array(), array($this->api, new Request())); array(), array($this->api, new Request()));
$this->exception = new \Exception(); $this->exception = new \Exception();
$this->response = $this->getMock('OCP\AppFramework\Http\Response'); $this->response = $this->getMock('OCP\AppFramework\Http\Response');

View File

@ -39,8 +39,8 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
private $request; private $request;
public function setUp() { public function setUp() {
$api = $this->getMock('OC\AppFramework\Core\API', array(), array('test')); $api = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer', array(), array('test'));
$this->controller = $this->getMock('OC\AppFramework\Controller\Controller', $this->controller = $this->getMock('OCP\AppFramework\Controller\Controller',
array(), array($api, new Request())); array(), array($api, new Request()));
$this->request = new Request(); $this->request = new Request();
@ -51,24 +51,19 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
private function getAPI(){ private function getAPI(){
return $this->getMock('OC\AppFramework\Core\API', return $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
array('isLoggedIn', 'passesCSRFCheck', 'isAdminUser', array('isLoggedIn', 'passesCSRFCheck', 'isAdminUser',
'isSubAdminUser', 'activateNavigationEntry', 'isSubAdminUser', 'getUserId'),
'getUserId'),
array('app')); array('app'));
} }
private function checkNavEntry($method, $shouldBeActivated=false){ private function checkNavEntry($method){
$api = $this->getAPI(); $api = $this->getAPI();
if($shouldBeActivated){ $serverMock = $this->getMock('\OC\Server', array());
$api->expects($this->once()) $api->expects($this->any())->method('getServer')
->method('activateNavigationEntry'); ->will($this->returnValue($serverMock));
} else {
$api->expects($this->never())
->method('activateNavigationEntry');
}
$sec = new SecurityMiddleware($api, $this->request); $sec = new SecurityMiddleware($api, $this->request);
$sec->beforeController('\OC\AppFramework\Middleware\Security\SecurityMiddlewareTest', $method); $sec->beforeController('\OC\AppFramework\Middleware\Security\SecurityMiddlewareTest', $method);
@ -80,7 +75,7 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function testSetNavigationEntry(){ public function testSetNavigationEntry(){
$this->checkNavEntry('testSetNavigationEntry', true); $this->checkNavEntry('testSetNavigationEntry');
} }
@ -215,9 +210,33 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
/** /**
* @PublicPage * @PublicPage
* @expectedException \OC\AppFramework\Middleware\Security\SecurityException
*/ */
public function testCsrfCheck(){ public function testCsrfCheck(){
$this->securityCheck('testCsrfCheck', 'passesCSRFCheck'); $api = $this->getAPI();
$request = $this->getMock('OC\AppFramework\Http\Request', array('passesCSRFCheck'));
$request->expects($this->once())
->method('passesCSRFCheck')
->will($this->returnValue(false));
$sec = new SecurityMiddleware($api, $request);
$sec->beforeController('\OC\AppFramework\Middleware\Security\SecurityMiddlewareTest', 'testCsrfCheck');
}
/**
* @PublicPage
* @NoCSRFRequired
*/
public function testNoCsrfCheck(){
$api = $this->getAPI();
$request = $this->getMock('OC\AppFramework\Http\Request', array('passesCSRFCheck'));
$request->expects($this->never())
->method('passesCSRFCheck')
->will($this->returnValue(false));
$sec = new SecurityMiddleware($api, $request);
$sec->beforeController('\OC\AppFramework\Middleware\Security\SecurityMiddlewareTest', 'testNoCsrfCheck');
} }
@ -225,7 +244,14 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
* @PublicPage * @PublicPage
*/ */
public function testFailCsrfCheck(){ public function testFailCsrfCheck(){
$this->securityCheck('testFailCsrfCheck', 'passesCSRFCheck', true); $api = $this->getAPI();
$request = $this->getMock('OC\AppFramework\Http\Request', array('passesCSRFCheck'));
$request->expects($this->once())
->method('passesCSRFCheck')
->will($this->returnValue(true));
$sec = new SecurityMiddleware($api, $request);
$sec->beforeController('\OC\AppFramework\Middleware\Security\SecurityMiddlewareTest', 'testFailCsrfCheck');
} }
@ -271,8 +297,12 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
public function testAfterExceptionReturnsRedirect(){ public function testAfterExceptionReturnsRedirect(){
$api = $this->getMock('OC\AppFramework\Core\API', array(), array('test')); $api = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer', array(), array('test'));
$this->controller = $this->getMock('OC\AppFramework\Controller\Controller', $serverMock = $this->getMock('\OC\Server', array('getNavigationManager'));
$api->expects($this->once())->method('getServer')
->will($this->returnValue($serverMock));
$this->controller = $this->getMock('OCP\AppFramework\Controller\Controller',
array(), array($api, new Request())); array(), array($api, new Request()));
$this->request = new Request( $this->request = new Request(