/** * This function tests, that bodies are correctly stored and rendered */ public function testBodyStoringAndRendering() { Config::getInstance()->clear(); $this->response->setRenderedBody('test body 1'); $this->assertEquals('test body 1', $this->response->getRenderedBody(), 'HttpResponse does not correctly store or render a plain text body.'); $view = new ViewMock(); $view->set('test body 2'); $this->response->setBody($view); $this->assertEquals('test body 2', $this->response->getRenderedBody(), 'HttpResponse does not correctly store or render an ViewAbstract body.'); }
public function testRun() { $response = null; $controller = $this->getController($response); $controller->setAction(function () { }); $this->assertFalse($controller->ran); $controller->run('Test'); $this->assertTrue($controller->ran); try { $controller->run('nonExistent'); $this->fail('Running a non-existent action should result in a ControllerException'); } catch (\YapepBase\Exception\ControllerException $e) { $this->assertEquals(\YapepBase\Exception\ControllerException::ERR_ACTION_NOT_FOUND, $e->getCode()); } $controller->setAction(function () { return 3.14; }); try { $controller->run('Test'); $this->fail('Running an action with an invalid result in a ControllerException'); } catch (\YapepBase\Exception\ControllerException $e) { $this->assertEquals(\YapepBase\Exception\ControllerException::ERR_INVALID_ACTION_RESULT, $e->getCode()); } $controller->setAction(function () { return 'test string'; }); $controller->run('Test'); $this->assertEquals('test string', $response->getRenderedBody()); $controller->setAction(function () { $view = new ViewMock(); $view->set('view test string'); return $view; }); $controller->run('Test'); $this->assertEquals('view test string', $response->getRenderedBody()); $controller->setAction(function (MockController $controller) { $controller->internalRedirect('Mock', 'RedirectTarget'); }); try { $controller->run('Test'); $this->fail('No redirectException is thrown'); } catch (RedirectException $exception) { } $this->assertEquals('redirect test', $response->getRenderedBody()); // Test if the action is run with an invalid case with no strict checking $controller->setAction(function (MockController $controller) { return 'test'; }); $controller->run('test'); $this->assertTrue($controller->ran, 'The action should have been run with an invalid case in the action and no strict checking enabled'); // Test if the action is run with an invalid case with strict checking enabled Config::getInstance()->set('system.performStrictControllerActionNameValidation', true); $controller->setAction(function (MockController $controller) { return 'test'; }); try { $controller->run('test'); $this->fail('There should be a ControllerException thrown when running an action with an invalid case' . ' and strict checking is enabled'); } catch (ControllerException $e) { $this->assertEquals(ControllerException::ERR_ACTION_NOT_FOUND, $e->getCode(), 'The exception should have a code for action not found'); $this->assertContains('Invalid case', $e->getMessage(), 'The error message should contain invalid case'); } $this->assertFalse($controller->ran, 'The action should not have been run with an invalid case in the action and strict checking enabled'); // Test that strict checking doesn't affect running a correctly named action $controller->setAction(function (MockController $controller) { return 'test'; }); $controller->run('Test'); $this->assertTrue($controller->ran, 'The action should have been run with a valid case in the action and strict checking enabled'); }