Skip to content

Commit

Permalink
Very crude dispatch cycle is working now.
Browse files Browse the repository at this point in the history
The code is pretty simple right now as it doesn't attempt any fancy
footwork around mocks or giving you ways to only get the just the view
without the layout. It is also not possible to get the view variables
just yet.

I've also added a stub response to smooth over any issues where someone
might try and send a response by accident or on purpose :)
  • Loading branch information
markstory committed Sep 2, 2014
1 parent eec011e commit 07d559a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/TestSuite/IntegrationTestCase.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Network\Session;
use Cake\Routing\DispatcherFactory;
use Cake\TestSuite\TestCase;
use Cake\TestSuite\Stub\Response;

/**
* A test case class intended to make integration tests of
Expand All @@ -32,7 +33,20 @@
*/
class IntegrationTestCase extends TestCase {

/**
* The data used to build the next request.
*
* @var array
*/
protected $_request = [];

/**
* The response for the most recent request.
*
* @var \Cake\Network\Response
*/
protected $_response;

protected $_session = [];
protected $_cookie = [];

Expand All @@ -46,6 +60,7 @@ public function tearDown() {
$this->_request = [];
$this->_session = [];
$this->_cookie = [];
$this->_response = null;
}

/**
Expand Down Expand Up @@ -183,6 +198,10 @@ public function delete($url) {
*/
protected function _sendRequest($url, $method, $data = null) {
$request = $this->_buildRequest($url, $method, $data);
$response = new Response();
$dispatcher = DispatcherFactory::create();
$dispatcher->dispatch($request, $response);
$this->_response = $response;
}

/**
Expand Down Expand Up @@ -211,7 +230,6 @@ protected function _buildRequest($url, $method, $data) {
unset($this->_request['headers']);
}
$props += $this->_request;

return new Request($props);
}

Expand Down
32 changes: 32 additions & 0 deletions src/TestSuite/Stub/Response.php
@@ -0,0 +1,32 @@
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite\Stub;

use Cake\Network\Response as Base;

/**
* A response class intended for test cases.
*/
class Response extends Base {

/**
* Stub the send() method so headers and output are not sent.
*
* @return void
*/
public function send() {
}

}
27 changes: 26 additions & 1 deletion tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -14,6 +14,8 @@
*/
namespace Cake\Test\TestCase\TestSuite;

use Cake\Core\Configure;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Cake\TestSuite\IntegrationTestCase;

Expand All @@ -22,9 +24,19 @@
*/
class IntegrationTestCaseTest extends IntegrationTestCase {

/**
* Setup method
*
* @return void
*/
public function setUp() {
parent::setUp();
Router::connect('/:controller/:action/*');
Configure::write('App.namespace', 'TestApp');

Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
DispatcherFactory::clear();
DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');
}

/**
Expand All @@ -47,4 +59,17 @@ public function testRequestBuilding() {
$this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
}

/**
* Test sending get requests.
*
* @return void
*/
public function testSendingGet() {
$this->assertNull($this->_response);

$this->get('/request_action/test_request_action');
$this->assertNotEmpty($this->_response);
$this->assertInstanceOf('Cake\Network\Response', $this->_response);
}

}

0 comments on commit 07d559a

Please sign in to comment.