Skip to content

Commit

Permalink
Add template/layout/view variable capturing to integrationtestcase
Browse files Browse the repository at this point in the history
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 f2969a2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/TestSuite/IntegrationTestCase.php
Expand Up @@ -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.
*
Expand All @@ -73,6 +94,9 @@ public function tearDown() {
$this->_session = [];
$this->_cookie = [];
$this->_response = null;
$this->_controller = null;
$this->_viewName = null;
$this->_layoutName = null;
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -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);
}

/**
*
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/TestApp/Controller/PostsController.php
Expand Up @@ -29,7 +29,6 @@ class PostsController extends AppController {
*/
public $components = array(
'RequestHandler',
'Auth'
);

/**
Expand All @@ -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.