Skip to content

Commit

Permalink
Re-adding session capability to RequestAction
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 18, 2014
1 parent b08bd00 commit 497e367
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Routing/RequestActionTrait.php
Expand Up @@ -16,6 +16,7 @@
use Cake\Core\Configure;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Network\Session;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;

Expand Down Expand Up @@ -66,6 +67,17 @@ trait RequestActionTrait {
* ]);
* }}}
*
* ### Trasmitting the session
*
* By default action dispatched by this method will use a vanialla session object. If you want the
* a particular session instance to be used, you need to specify it.
*
* {{{
* $vars = $this->requestAction('/articles/popular', [
* 'session' => new Session($someSessionConfig)
* ]);
* }}}
*
* @param string|array $url String or array-based url. Unlike other url arrays in CakePHP, this
* url will not automatically handle passed arguments in the $url parameter.
* @param array $extra if array includes the key "return" it sets the autoRender to true. Can
Expand Down Expand Up @@ -113,12 +125,17 @@ public function requestAction($url, array $extra = array()) {
$params['params']['pass'] = [];
}
}

if (!empty($post)) {
$params['post'] = $post;
}

if (!empty($query)) {
$params['query'] = $query;
}

$params['session'] = isset($extra['session']) ? $extra['session'] : new Session();

$request = new Request($params);
$request->addParams($extra);
$dispatcher = DispatcherFactory::create();
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Routing/RequestActionTraitTest.php
Expand Up @@ -306,4 +306,25 @@ public function testRequestActionGetParameters() {
$this->assertEquals('value', $result['query']['get']);
}

/**
* Tests that it is possible to transmit the session for the request
*
* @return void
*/
public function testRequestActionSession() {
$result = $this->object->requestAction('/request_action/session_test');
$this->assertNull($result);

$session = $this->getMock('Cake\Network\Session');
$session->expects($this->once())
->method('read')
->with('foo')
->will($this->returnValue('bar'));
$result = $this->object->requestAction(
'/request_action/session_test',
['session' => $session]
);
$this->assertEquals('bar', $result);
}

}
10 changes: 10 additions & 0 deletions tests/test_app/TestApp/Controller/RequestActionController.php
Expand Up @@ -122,4 +122,14 @@ public function param_check() {
$this->response->body($content);
}

/**
* Tests session transmission
*
* @return void
*/
public function session_test() {
$this->response->body($this->request->session()->read('foo'));
return $this->response;
}

}

0 comments on commit 497e367

Please sign in to comment.