Browse Source

Fix tests.

Since Response::_sendHeader() throws an exception if headers are already sent
(which is the case when running tests), the method needs to be mocked in all
tests.
ADmad 11 years ago
parent
commit
f3b653b0a0

+ 1 - 1
tests/TestCase/Controller/Component/RequestHandlerComponentTest.php

@@ -66,7 +66,7 @@ class RequestHandlerComponentTest extends TestCase {
  */
 	protected function _init() {
 		$request = new Request('controller_posts/index');
-		$response = $this->getMock('Cake\Network\Response', array('stop'));
+		$response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
 		$this->Controller = new RequestHandlerTestController($request, $response);
 		$this->Controller->constructClasses();
 		$this->RequestHandler = new RequestHandlerComponent($this->Controller->components());

+ 40 - 6
tests/TestCase/Error/ErrorHandlerTest.php

@@ -22,12 +22,35 @@ use Cake\Core\Configure;
 use Cake\Core\Plugin;
 use Cake\Error;
 use Cake\Error\ErrorHandler;
+use Cake\Error\ExceptionRenderer;
 use Cake\Log\Log;
 use Cake\Network\Request;
+use Cake\Network\Response;
 use Cake\Routing\Router;
 use Cake\TestSuite\TestCase;
 
 /**
+ * Test exception renderer
+ */
+class TestExceptionRenderer extends ExceptionRenderer {
+
+/**
+ * Constructor for mocking Response::_sendHeader()
+ *
+ * @param \Exception $exception
+ */
+	public function __construct(\Exception $exception) {
+		parent::__construct($exception);
+		$testCase = new ErrorHandlerTest();
+		$this->controller->response = $testCase->getMock(
+			'Cake\Network\Response',
+			['_sendHeader']
+		);
+	}
+
+}
+
+/**
  * ErrorHandlerTest class
  */
 class ErrorHandlerTest extends TestCase {
@@ -190,7 +213,9 @@ class ErrorHandlerTest extends TestCase {
  */
 	public function testHandleException() {
 		$error = new Error\NotFoundException('Kaboom!');
-		$errorHandler = new ErrorHandler();
+		$errorHandler = new ErrorHandler([
+			'exceptionRenderer' => 'Cake\Test\TestCase\Error\TestExceptionRenderer'
+		]);
 
 		ob_start();
 		$errorHandler->handleException($error);
@@ -204,7 +229,10 @@ class ErrorHandlerTest extends TestCase {
  * @return void
  */
 	public function testHandleExceptionLog() {
-		$errorHandler = new ErrorHandler(['log' => true]);
+		$errorHandler = new ErrorHandler([
+			'log' => true,
+			'exceptionRenderer' => 'Cake\Test\TestCase\Error\TestExceptionRenderer'
+		]);
 
 		$error = new Error\NotFoundException('Kaboom!');
 
@@ -239,7 +267,8 @@ class ErrorHandlerTest extends TestCase {
 
 		$errorHandler = new ErrorHandler([
 			'log' => true,
-			'skipLog' => ['Cake\Error\NotFoundException']
+			'skipLog' => ['Cake\Error\NotFoundException'],
+			'exceptionRenderer' => 'Cake\Test\TestCase\Error\TestExceptionRenderer'
 		]);
 
 		ob_start();
@@ -281,7 +310,9 @@ class ErrorHandlerTest extends TestCase {
  */
 	public function testHandleFatalErrorPage() {
 		$line = __LINE__;
-		$errorHandler = new ErrorHandler();
+		$errorHandler = new ErrorHandler([
+			'exceptionRenderer' => 'Cake\Test\TestCase\Error\TestExceptionRenderer'
+		]);
 		Configure::write('debug', true);
 		ob_start();
 		ob_start();
@@ -310,7 +341,7 @@ class ErrorHandlerTest extends TestCase {
 		$this->_logger->expects($this->at(0))
 			->method('write')
 			->with('error', $this->logicalAnd(
-				$this->stringContains(__FILE__ . ', line ' . (__LINE__ + 10)),
+				$this->stringContains(__FILE__ . ', line ' . (__LINE__ + 13)),
 				$this->stringContains('Fatal Error (1)'),
 				$this->stringContains('Something wrong')
 			));
@@ -318,7 +349,10 @@ class ErrorHandlerTest extends TestCase {
 			->method('write')
 			->with('error', $this->stringContains('[Cake\Error\FatalErrorException] Something wrong'));
 
-		$errorHandler = new ErrorHandler(['log' => true]);
+		$errorHandler = new ErrorHandler([
+			'log' => true,
+			'exceptionRenderer' => 'Cake\Test\TestCase\Error\TestExceptionRenderer'
+		]);
 		ob_start();
 		$errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
 		ob_clean();

+ 2 - 2
tests/TestCase/Error/ExceptionRendererTest.php

@@ -251,7 +251,7 @@ class ExceptionRendererTest extends TestCase {
 	public function testExceptionMessageCoercion() {
 		Configure::write('debug', false);
 		$exception = new MissingActionException('Secret info not to be leaked');
-		$ExceptionRenderer = new ExceptionRenderer($exception);
+		$ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
 
 		$this->assertInstanceOf('Cake\Controller\ErrorController', $ExceptionRenderer->controller);
 		$this->assertEquals($exception, $ExceptionRenderer->error);
@@ -273,7 +273,7 @@ class ExceptionRendererTest extends TestCase {
 	public function testCakeErrorHelpersNotLost() {
 		Configure::write('App.namespace', 'TestApp');
 		$exception = new SocketException('socket exception');
-		$renderer = new \TestApp\Error\TestAppsExceptionRenderer($exception);
+		$renderer = $this->_mockResponse(new \TestApp\Error\TestAppsExceptionRenderer($exception));
 
 		ob_start();
 		$renderer->render();

+ 2 - 2
tests/TestCase/Network/ResponseTest.php

@@ -748,12 +748,12 @@ class ResponseTest extends TestCase {
 		$response->sharable(true);
 		$this->assertTrue($response->sharable());
 
-		$response = new Response;
+		$response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
 		$response->sharable(true, 3600);
 		$headers = $response->header();
 		$this->assertEquals('public, max-age=3600', $headers['Cache-Control']);
 
-		$response = new Response;
+		$response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
 		$response->sharable(false, 3600);
 		$headers = $response->header();
 		$this->assertEquals('private, max-age=3600', $headers['Cache-Control']);

+ 2 - 2
tests/TestCase/Routing/DispatcherTest.php

@@ -455,7 +455,7 @@ class DispatcherTest extends TestCase {
  * @return void
  */
 	public function testAfterDispatchReplaceResponse() {
-		$response = $this->getMock('Cake\Network\Response', ['send']);
+		$response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'send']);
 		$response->expects($this->once())
 			->method('send');
 
@@ -471,7 +471,7 @@ class DispatcherTest extends TestCase {
 			'url' => '/posts',
 			'params' => [
 				'plugin' => null,
-				'controller' => 'posts',
+				'controller' => 'Posts',
 				'action' => 'index',
 				'pass' => [],
 			],

+ 2 - 1
tests/test_app/TestApp/Controller/SomePagesController.php

@@ -46,7 +46,8 @@ class SomePagesController extends Controller {
  * @return \Cake\Network\Response
  */
 	public function responseGenerator() {
-		return new Response(array('body' => 'new response'));
+		$this->response->body('new response');
+		return $this->response;
 	}
 
 	protected function _fail() {