wrote this file. * As long as you retain this notice you can do whatever you want with * this stuff. If we meet some day, and you think this stuff is worth it, * you can buy me a beer in return. * * @author Sven Strittmatter * @copyright Copyright (c) 2010, Sven Strittmatter. * @version 0.2.2 * @license http://www.weltraumschaf.de/the-beer-ware-license.txt */ /** * Test helper */ require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'TestHelper.php'; /** * @see Api_Controller */ require_once 'Api/Controller.php'; /** * Fake the magic globael GET parameters array. * * @var array */ $_GET; /** * Fake the magic globael POST parameters array. * * @var array */ $_POST; /** * Testcase for the class Api_Controller. */ class Api_ControllerTest extends PHPUnit_Extensions_OutputTestCase { /** * Sets up clear GET and POST parameter arrays. */ protected function setUp() { $_GET = array(); $_POST = array(); Api_Log::clear(); } /** * Returtns a default SUT with mocked config and request. * * @return Api_Controller */ protected function getDefaultController() { $config = $this->getMock('Api_Kwick_Config', array(), array('config' => array())); $request = $this->getMock('Api_Kwick_Request', array(), array('config' => $config)); return new Api_Controller($request); } /** * Test geting request parameters. */ public function testGetRequestParameter() { $_GET['foo'] = 'bar'; $_POST['baz'] = 3; $controller = $this->getDefaultController(); $this->assertEquals('bar', $controller->getRequestParameter('foo')); $this->assertEquals(3, $controller->getRequestParameter('baz')); $this->assertEquals('default', $controller->getRequestParameter('test', 'default')); $this->assertEquals('', $controller->getRequestParameter('test')); $this->assertNull($controller->getRequestParameter('test', null)); } /** * Test getting the method. */ public function testGetMethod() { $controller = $this->getDefaultController(); $this->assertEquals(Api_Kwick_Request::DEFAULT_METHOD, $controller->getMethod()); $_GET['method'] = 'aMethod'; $this->assertEquals('aMethod', $controller->getMethod()); unset($_GET['method']); $_POST['method'] = 'bMethod'; $this->assertEquals('bMethod', $controller->getMethod()); unset($_POST['method']); } /** * Test getting the service. */ public function testGetService() { $controller = $this->getDefaultController(); $this->assertEquals(Api_Kwick_Request::DEFAULT_SERVICE, $controller->getService()); $_GET['service'] = 'aService'; $this->assertEquals('aService', $controller->getService()); unset($_GET['service']); $_POST['service'] = 'bService'; $this->assertEquals('bService', $controller->getService()); unset($_POST['service']); } /** * Test dispatching. */ public function testDispatch() { $_GET['service'] = 'aService'; $_GET['method'] = 'aMethod'; $config = $this->getMock('Api_Kwick_Config', array(), array('config' => array())); $request = $this->getMock('Api_Kwick_Request', array('send', 'setMethod', 'setService'), array('config' => $config)); $request->expects($this->once()) ->method('setMethod') ->with($this->equalTo($_GET['method'])); $request->expects($this->once()) ->method('setService') ->with($this->equalTo($_GET['service'])); $request->expects($this->once()) ->method('send') ->will($this->returnValue('result')); $this->expectOutputString(json_encode(array('response' => 'result', 'log' => array()))); Api_Log::disable(); $controller = new Api_Controller($request); $controller->dispatch(); Api_Log::enable(); } public function testThrowExceptionOnDispatching() { $config = $this->getMock('Api_Kwick_Config', array(), array('config' => array())); $request = $this->getMock('Api_Kwick_Request', array('send', 'setMethod', 'setService'), array('config' => $config)); $request->expects($this->once()) ->method('setMethod'); $request->expects($this->once()) ->method('setService'); $exception = new Exception('A testexception', 5); $request->expects($this->once()) ->method('send') ->will($this->throwException($exception)); $this->expectOutputString(json_encode(array('exception' => $exception->getMessage(), 'log' => array()))); Api_Log::disable(); $controller = new Api_Controller($request); $controller->dispatch(); Api_Log::enable(); } public function testPrepareParametersOnRequest() { $this->markTestIncomplete(); } public function testExternalSecret() { $this->markTestIncomplete(); } }