Skip to content

Commit

Permalink
Merge 7b22dd5 into 6c0d6b8
Browse files Browse the repository at this point in the history
  • Loading branch information
cnizzardini committed Jul 23, 2022
2 parents 6c0d6b8 + 7b22dd5 commit 1908873
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 357 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
version: ['~4.2.0', '~4.3.0']
version: ['~4.2.0', '~4.3.0', '^4.4']
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
17 changes: 9 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
# MIXERAPI #
##########################
/mixerapi_dev_test
/src/Controller/*
/coverage-reports/
/plugins/cakephp-swagger-bake
/plugins/cakephp-swagger-bake/*

# MKDOCS #
##########################
site/*
Expand Down Expand Up @@ -54,5 +46,14 @@ nbproject/*
.vscode
# Sass preprocessor
.sass-cache/

# MIXERAPI #
##########################
/mixerapi_dev_test
/src/Controller/*
/coverage-reports/
/plugins/cakephp-swagger-bake
/plugins/cakephp-swagger-bake/*
/plugins/jwt-auth/tests/mixerapi_dev_test
/plugins/jwt-auth/mixerapi_dev_test
/plugins/exception-render/tests/TestCase/mixerapi_dev_test
605 changes: 275 additions & 330 deletions composer.lock

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Datasource\ConnectionManager;
use Cake\Error\ConsoleErrorHandler;
use Cake\Error\ErrorHandler;
use Cake\Error\ErrorTrap;
use Cake\Error\ExceptionTrap;
use Cake\Http\ServerRequest;
use Cake\Log\Log;
use Cake\Mailer\Mailer;
Expand Down Expand Up @@ -120,17 +120,13 @@
/*
* Register application error and exception handlers.
*/
$isCli = PHP_SAPI === 'cli';
if ($isCli) {
(new ConsoleErrorHandler(Configure::read('Error')))->register();
} else {
(new ErrorHandler(Configure::read('Error')))->register();
}
(new ErrorTrap(Configure::read('Error')))->register();
(new ExceptionTrap(Configure::read('Error')))->register();

/*
* Include the CLI bootstrap overrides.
*/
if ($isCli) {
if (PHP_SAPI === 'cli') {
require __DIR__ . '/bootstrap_cli.php';
}

Expand Down
14 changes: 14 additions & 0 deletions plugins/exception-render/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ If for instance you have a custom exception that is thrown, such as `InventoryEx
Providing an Exception name, in conjunction with the status code already provided by CakePHP, enables API clients
to tailor their exception handling.

### Disabling ValidationExceptions

There may be times when you don't want ValidationExceptions to run. You can easily disable the event:

```php
Configure::write('MixerApi.ExceptionRender.entity_validation', false);
```

Another example is you may only want the event to run for non-CLI portions of your application:

```php
Configure::write('MixerApi.ExceptionRender.entity_validation', PHP_SAPI !== 'cli');
```

### Changing Error Messages

ExceptionRender dispatches a `MixerApi.ExceptionRender.beforeRender` event that you can listen for to alter `viewVars`
Expand Down
15 changes: 9 additions & 6 deletions plugins/exception-render/src/EntityValidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace MixerApi\ExceptionRender;

use ArrayObject;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\Event\EventManager;
Expand All @@ -24,12 +25,14 @@ class EntityValidationListener
*/
public function __construct()
{
EventManager::instance()->on(
'Model.afterMarshal',
function ($event, $entity, $options) {
$this->handler($event, $entity, $options);
}
);
if (Configure::read('MixerApi.ExceptionRender.entity_validation') !== false) {
EventManager::instance()->on(
'Model.afterMarshal',
function ($event, $entity, $options) {
$this->handler($event, $entity, $options);
}
);
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions plugins/exception-render/tests/TestCase/EntityValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MixerApi\ExceptionRender\Test\TestCase;

use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\TestSuite\TestCase;
use MixerApi\ExceptionRender\EntityValidationListener;
use MixerApi\ExceptionRender\ValidationException;
Expand Down Expand Up @@ -48,4 +50,18 @@ public function test_validation_exception(): void
'last_name' => ''
]);
}

public function test_validation_exception_does_not_run_when_cli(): void
{
Configure::write('MixerApi.ExceptionRender.entity_validation', false);
new EntityValidationListener();

$actorsTable = new ActorsTable();
$entity = $actorsTable->patchEntity($actorsTable->newEmptyEntity(), [
'first_name' => '',
'last_name' => ''
]);

$this->assertInstanceOf(EntityInterface::class, $entity);
}
}
4 changes: 2 additions & 2 deletions plugins/jwt-auth/tests/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public function test_login_fails(string $alg): void
public function test_auth_required(string $alg): void
{
$alg === 'RS' ? TestHelper::createRs256Config() : TestHelper::createHs256Config();
TestHelper::createHs256Config();
$this->get('/test/index.json');
$this->assertResponseCode(401);
$this->assertResponseContains('Authentication is required to continue');

}

public function dataProviderForAlg(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class TestController extends Controller
public function beforeFilter(EventInterface $event)
{
parent::beforeFilter($event);
$this->loadComponent('Authentication.Authentication');
$this->loadComponent('RequestHandler');
$this->loadComponent('Authentication.Authentication');
$this->Authentication->allowUnauthenticated(['login','jwks']);
}

Expand Down

0 comments on commit 1908873

Please sign in to comment.