Browse Source

Merge pull request #4564 from cakephp/integration-test-case-2

Integration test case part 2
José Lorenzo Rodríguez 11 years ago
parent
commit
abb93bf1f1

+ 136 - 0
src/TestSuite/IntegrationTestCase.php

@@ -63,6 +63,34 @@ class IntegrationTestCase extends TestCase {
 	protected $_cookie = [];
 
 /**
+ * The controller used in the last request.
+ *
+ * @var \Cake\Controller\Controller
+ */
+	protected $_controller;
+
+/**
+ * The last rendered view
+ *
+ * @var string
+ */
+	protected $_viewName;
+
+/**
+ * The last rendered layout
+ *
+ * @var string
+ */
+	protected $_layoutName;
+
+/**
+ * The session instance from the last request
+ *
+ * @var \Cake\Network\Session
+ */
+	protected $_requestSession;
+
+/**
  * Clear the state used for requests.
  *
  * @return void
@@ -73,6 +101,10 @@ class IntegrationTestCase extends TestCase {
 		$this->_session = [];
 		$this->_cookie = [];
 		$this->_response = null;
+		$this->_controller = null;
+		$this->_viewName = null;
+		$this->_layoutName = null;
+		$this->_requestSession = null;
 	}
 
 /**
@@ -212,8 +244,14 @@ class IntegrationTestCase extends TestCase {
 		$request = $this->_buildRequest($url, $method, $data);
 		$response = new Response();
 		$dispatcher = DispatcherFactory::create();
+		$dispatcher->eventManager()->attach(
+			[$this, 'controllerSpy'],
+			'Dispatcher.beforeDispatch',
+			['priority' => 999]
+		);
 		try {
 			$dispatcher->dispatch($request, $response);
+			$this->_requestSession = $request->session();
 			$this->_response = $response;
 		} catch (\PHPUnit_Exception $e) {
 			throw $e;
@@ -223,6 +261,26 @@ class IntegrationTestCase extends TestCase {
 	}
 
 /**
+ * Add additional event spies to the controller/view event manager.
+ *
+ * @param \Cake\Event\Event $event A dispatcher event.
+ * @return void
+ */
+	public function controllerSpy($event) {
+		if (empty($event->data['controller'])) {
+			return;
+		}
+		$this->_controller = $event->data['controller'];
+		$events = $this->_controller->eventManager();
+		$events->attach(function($event, $viewFile) {
+			$this->_viewName = $viewFile;
+		}, 'View.beforeRender');
+		$events->attach(function($event, $viewFile) {
+			$this->_layoutName = $viewFile;
+		}, 'View.beforeLayout');
+	}
+
+/**
  * Attempt to render an error response for a given exception.
  *
  * This method will attempt to use the configured exception renderer.
@@ -389,4 +447,82 @@ class IntegrationTestCase extends TestCase {
 		$this->assertContains($content, $this->_response->body(), $message);
 	}
 
+/**
+ * Assert that the search string was in the template name.
+ *
+ * @param string $content The content to check for.
+ * @param string $message The failure message that will be appended to the generated message.
+ * @return void
+ */
+	public function assertTemplate($content, $message = '') {
+		if (!$this->_viewName) {
+			$this->fail('No view name stored. ' . $message);
+		}
+		$this->assertContains($content, $this->_viewName, $message);
+	}
+
+/**
+ * Assert that the search string was in the layout name.
+ *
+ * @param string $content The content to check for.
+ * @param string $message The failure message that will be appended to the generated message.
+ * @return void
+ */
+	public function assertLayout($content, $message = '') {
+		if (!$this->_layoutName) {
+			$this->fail('No layout name stored. ' . $message);
+		}
+		$this->assertContains($content, $this->_layoutName, $message);
+	}
+
+/**
+ * Fetch a view variable by name.
+ *
+ * If the view variable does not exist null will be returned.
+ *
+ * @param string $name The view variable to get.
+ * @return mixed The view variable if set.
+ */
+	public function viewVariable($name) {
+		if (empty($this->_controller->viewVars)) {
+			$this->fail('There are no view variables, perhaps you need to run a request?');
+		}
+		if (isset($this->_controller->viewVars[$name])) {
+			return $this->_controller->viewVars[$name];
+		}
+		return null;
+	}
+
+/**
+ * Assert session contents
+ *
+ * @param string $expected The expected contents.
+ * @param string $path The session data path. Uses Hash::get() compatible notation
+ * @param string $message The failure message that will be appended to the generated message.
+ * @return void
+ */
+	public function assertSession($expected, $path, $message = '') {
+		if (empty($this->_requestSession)) {
+			$this->fail('There is no stored session data. Perhaps you need to run a request?');
+		}
+		$result = $this->_requestSession->read($path);
+		$this->assertEquals($expected, $result, 'Session content differs. ' . $message);
+	}
+
+/**
+ * Assert cookie values
+ *
+ * @param string $expected The expected contents.
+ * @param string $name The cookie name.
+ * @param string $message The failure message that will be appended to the generated message.
+ * @return void
+ */
+	public function assertCookie($expected, $name, $message = '') {
+		if (empty($this->_response)) {
+			$this->fail('Not response set, cannot assert cookies.');
+		}
+		$result = $this->_response->cookie($name);
+		$this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
+	}
+
 }

+ 30 - 0
tests/TestCase/TestSuite/IntegrationTestCaseTest.php

@@ -76,7 +76,37 @@ class IntegrationTestCaseTest extends IntegrationTestCase {
 	}
 
 /**
+ * Test sending requests stores references to controller/view/layout.
  *
+ * @return void
+ */
+	public function testRequestSetsProperties() {
+		$this->post('/posts/index');
+		$this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
+		$this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
+		$this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
+
+		$this->assertTemplate('index');
+		$this->assertLayout('default');
+		$this->assertEquals('value', $this->viewVariable('test'));
+	}
+
+/**
+ * Test flash and cookie assertions
+ *
+ * @return void
+ */
+	public function testFlashSessionAndCookieAsserts() {
+		$this->post('/posts/index');
+
+		$this->assertSession('An error message', 'Flash.flash.message');
+		$this->assertCookie(1, 'remember_me');
+	}
+
+/**
+ * Test error handling and error page rendering.
+ *
+ * @return void
  */
 	public function testPostAndErrorHandling() {
 		$this->post('/request_action/error_method');

+ 7 - 1
tests/test_app/TestApp/Controller/PostsController.php

@@ -28,8 +28,8 @@ class PostsController extends AppController {
  * @var array
  */
 	public $components = array(
+		'Flash',
 		'RequestHandler',
-		'Auth'
 	);
 
 /**
@@ -38,5 +38,11 @@ class PostsController extends AppController {
  * @return void
  */
 	public function index() {
+		$this->Flash->error('An error message');
+		$this->response->cookie([
+			'name' => 'remember_me',
+			'value' => 1
+		]);
+		$this->set('test', 'value');
 	}
 }