Skip to content

Commit

Permalink
Merge pull request #443 from FriendsOfCake/cake-3.3
Browse files Browse the repository at this point in the history
Account for new PSR7 middleware of Cake 3.3.
  • Loading branch information
ADmad authored Aug 27, 2016
2 parents c837b74 + 80d0bc0 commit 038cb86
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 276 deletions.
4 changes: 4 additions & 0 deletions docs/listeners/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class and enabling it with the `exceptionRenderer` configuration option.
}
}
However if you are using CakePHP 3.3's PSR7 middleware feature the ``exceptionRenderer``
config won't be used and instead you will have to set the ``Error.exceptionRenderer``
config in ``config/app.php`` to ``'Crud\Error\ExceptionRenderer'``.

Request type enforcing
----------------------

Expand Down
8 changes: 7 additions & 1 deletion src/Listener/ApiListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ public function setup()
return;
}

$this->registerExceptionHandler();
$appClass = Configure::read('App.namespace') . '\Application';

// If `App\Application` class exists it means Cake 3.3's PSR7 middleware
// implementation is used and it's too late to register new error handler.
if (!class_exists($appClass, false)) {
$this->registerExceptionHandler();
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/TestSuite/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Event\EventManager;
use Cake\Routing\Router;
use Crud\TestSuite\Traits\CrudTestTrait;
use FriendsOfCake\TestUtilities\AccessibilityHelperTrait;
use FriendsOfCake\TestUtilities\CounterHelperTrait;
Expand Down Expand Up @@ -35,5 +36,14 @@ public function setUp()
$existing = Configure::read('App.paths.templates');
$existing[] = Plugin::path('Crud') . 'tests/App/Template/';
Configure::write('App.paths.templates', $existing);


Configure::write('App.namespace', 'Crud\Test\App');

Router::extensions('json');

Router::connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);
Router::connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);
$this->useHttpServer(false);
}
}
87 changes: 40 additions & 47 deletions tests/TestCase/Action/AddActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public function testActionGetWithQueryArgs()
public function testActionPost()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
'Dispatcher.invokeController',
['priority' => 1000],
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();

$this->_controller->Flash
->expects($this->once())
Expand Down Expand Up @@ -127,13 +127,13 @@ function ($event) {
public function testActionPostWithAddRedirect()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
'Dispatcher.invokeController',
['priority' => 1000],
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();

$this->_controller->Flash
->expects($this->once())
Expand Down Expand Up @@ -172,13 +172,13 @@ function ($event) {
public function testActionPostWithEditRedirect()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
'Dispatcher.invokeController',
['priority' => 1000],
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();

$this->_controller->Flash
->expects($this->once())
Expand Down Expand Up @@ -216,13 +216,13 @@ function ($event) {
public function testActionPostErrorSave()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
'Dispatcher.invokeController',
['priority' => 1000],
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();

$this->_controller->Flash
->expects($this->once())
Expand Down Expand Up @@ -269,13 +269,13 @@ function ($event) {
public function testActionPostValidationErrors()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
'Dispatcher.invokeController',
['priority' => 1000],
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();

$this->_controller->Flash
->expects($this->once())
Expand Down Expand Up @@ -339,7 +339,6 @@ public function apiGetHttpMethodProvider()
*/
public function testApiGet($method)
{
Router::extensions(['json']);
Router::scope('/', function ($routes) {
$routes->extensions(['json']);
$routes->fallbacks();
Expand Down Expand Up @@ -374,13 +373,13 @@ public function apiUpdateHttpMethodProvider()
public function testApiCreate($method)
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
'Dispatcher.invokeController',
['priority' => 1000],
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();

$this->_controller->Flash
->expects($this->never())
Expand All @@ -393,8 +392,6 @@ function ($event) {
}
);

Router::extensions('json');

$this->{$method}('/blogs/add.json', [
'name' => '6th blog post',
'body' => 'Amazing blog post'
Expand All @@ -418,16 +415,14 @@ function ($event) {
*/
public function testApiCreateError($method)
{
Router::extensions('json');

$this->_eventManager->on(
'Dispatcher.beforeDispatch',
'Dispatcher.invokeController',
['priority' => 1000],
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();

$this->_controller->Flash
->expects($this->never())
Expand Down Expand Up @@ -468,16 +463,14 @@ function ($event) {
*/
public function testApiCreateErrors($method)
{
Router::extensions('json');

$this->_eventManager->on(
'Dispatcher.beforeDispatch',
'Dispatcher.invokeController',
['priority' => 1000],
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();

$this->_controller->Flash
->expects($this->never())
Expand Down
Loading

0 comments on commit 038cb86

Please sign in to comment.