Skip to content
Permalink
Browse files

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.
  • Loading branch information...
markstory committed Sep 9, 2014
1 parent e307ed3 commit f2969a28594492c9e08d41d5f3eb02369a044079
@@ -62,6 +62,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.
*
@@ -73,6 +94,9 @@ public function tearDown() {
$this->_session = [];
$this->_cookie = [];
$this->_response = null;
$this->_controller = null;
$this->_viewName = null;
$this->_layoutName = null;
}
/**
@@ -212,6 +236,11 @@ protected function _sendRequest($url, $method, $data = null) {
$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;
@@ -222,6 +251,26 @@ protected function _sendRequest($url, $method, $data = null) {
}
}
/**
* 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.
*
@@ -75,6 +75,18 @@ public function testGet() {
$this->assertEquals('This is a test', $this->_response->body());
}
/**
* 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);
}
/**
*
*/
@@ -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');
}
}

0 comments on commit f2969a2

Please sign in to comment.
You can’t perform that action at this time.