浏览代码

Add template/layout/view variable capturing to integrationtestcase

Abuse the event hooks to capture interesting data about the request and
store it as testcase properties so assert's can be built later on.
mark_story 11 年之前
父节点
当前提交
f2969a2859

+ 49 - 0
src/TestSuite/IntegrationTestCase.php

@@ -63,6 +63,27 @@ 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;
+
+/**
  * Clear the state used for requests.
  *
  * @return void
@@ -73,6 +94,9 @@ class IntegrationTestCase extends TestCase {
 		$this->_session = [];
 		$this->_cookie = [];
 		$this->_response = null;
+		$this->_controller = null;
+		$this->_viewName = null;
+		$this->_layoutName = null;
 	}
 
 /**
@@ -212,6 +236,11 @@ 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->_response = $response;
@@ -223,6 +252,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.

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

@@ -76,6 +76,18 @@ 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);
+	}
+
+/**
  *
  */
 	public function testPostAndErrorHandling() {

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

@@ -29,7 +29,6 @@ class PostsController extends AppController {
  */
 	public $components = array(
 		'RequestHandler',
-		'Auth'
 	);
 
 /**
@@ -38,5 +37,6 @@ class PostsController extends AppController {
  * @return void
  */
 	public function index() {
+		$this->set('test', 'value');
 	}
 }