Skip to content

Commit bc19424

Browse files
authored
[ValidationException] - Add config option to turn off ValidationException (#108)
* Adds config to turn off ValidationException, fixes failing tests
1 parent 6c0d6b8 commit bc19424

8 files changed

Lines changed: 327 additions & 348 deletions

File tree

.github/workflows/pull-request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
runs-on: ubuntu-latest
6565
strategy:
6666
matrix:
67-
version: ['~4.2.0', '~4.3.0']
67+
version: ['~4.2.0', '~4.3.0', '^4.4']
6868
steps:
6969
- name: Checkout
7070
uses: actions/checkout@v2

.gitignore

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
# MIXERAPI #
2-
##########################
3-
/mixerapi_dev_test
4-
/src/Controller/*
5-
/coverage-reports/
6-
/plugins/cakephp-swagger-bake
7-
/plugins/cakephp-swagger-bake/*
8-
91
# MKDOCS #
102
##########################
113
site/*
@@ -54,5 +46,14 @@ nbproject/*
5446
.vscode
5547
# Sass preprocessor
5648
.sass-cache/
49+
50+
# MIXERAPI #
51+
##########################
52+
/mixerapi_dev_test
53+
/src/Controller/*
54+
/coverage-reports/
55+
/plugins/cakephp-swagger-bake
56+
/plugins/cakephp-swagger-bake/*
5757
/plugins/jwt-auth/tests/mixerapi_dev_test
5858
/plugins/jwt-auth/mixerapi_dev_test
59+
/plugins/exception-render/tests/TestCase/mixerapi_dev_test

composer.lock

Lines changed: 275 additions & 330 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/exception-render/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ If for instance you have a custom exception that is thrown, such as `InventoryEx
146146
Providing an Exception name, in conjunction with the status code already provided by CakePHP, enables API clients
147147
to tailor their exception handling.
148148

149+
### Disabling ValidationExceptions
150+
151+
There may be times when you don't want ValidationExceptions to run. You can easily disable the event:
152+
153+
```php
154+
Configure::write('MixerApi.ExceptionRender.entity_validation', false);
155+
```
156+
157+
Another example is you may only want the event to run for non-CLI portions of your application:
158+
159+
```php
160+
Configure::write('MixerApi.ExceptionRender.entity_validation', PHP_SAPI !== 'cli');
161+
```
162+
149163
### Changing Error Messages
150164

151165
ExceptionRender dispatches a `MixerApi.ExceptionRender.beforeRender` event that you can listen for to alter `viewVars`

plugins/exception-render/src/EntityValidationListener.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace MixerApi\ExceptionRender;
55

66
use ArrayObject;
7+
use Cake\Core\Configure;
78
use Cake\Datasource\EntityInterface;
89
use Cake\Event\EventInterface;
910
use Cake\Event\EventManager;
@@ -24,12 +25,14 @@ class EntityValidationListener
2425
*/
2526
public function __construct()
2627
{
27-
EventManager::instance()->on(
28-
'Model.afterMarshal',
29-
function ($event, $entity, $options) {
30-
$this->handler($event, $entity, $options);
31-
}
32-
);
28+
if (Configure::read('MixerApi.ExceptionRender.entity_validation') !== false) {
29+
EventManager::instance()->on(
30+
'Model.afterMarshal',
31+
function ($event, $entity, $options) {
32+
$this->handler($event, $entity, $options);
33+
}
34+
);
35+
}
3336
}
3437

3538
/**

plugins/exception-render/tests/TestCase/EntityValidationTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MixerApi\ExceptionRender\Test\TestCase;
44

5+
use Cake\Core\Configure;
6+
use Cake\Datasource\EntityInterface;
57
use Cake\TestSuite\TestCase;
68
use MixerApi\ExceptionRender\EntityValidationListener;
79
use MixerApi\ExceptionRender\ValidationException;
@@ -48,4 +50,18 @@ public function test_validation_exception(): void
4850
'last_name' => ''
4951
]);
5052
}
53+
54+
public function test_validation_exception_does_not_run_when_cli(): void
55+
{
56+
Configure::write('MixerApi.ExceptionRender.entity_validation', false);
57+
new EntityValidationListener();
58+
59+
$actorsTable = new ActorsTable();
60+
$entity = $actorsTable->patchEntity($actorsTable->newEmptyEntity(), [
61+
'first_name' => '',
62+
'last_name' => ''
63+
]);
64+
65+
$this->assertInstanceOf(EntityInterface::class, $entity);
66+
}
5167
}

plugins/jwt-auth/tests/ControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public function test_login_fails(string $alg): void
7979
public function test_auth_required(string $alg): void
8080
{
8181
$alg === 'RS' ? TestHelper::createRs256Config() : TestHelper::createHs256Config();
82-
TestHelper::createHs256Config();
8382
$this->get('/test/index.json');
84-
$this->assertResponseCode(401);
83+
$this->assertResponseContains('Authentication is required to continue');
84+
8585
}
8686

8787
public function dataProviderForAlg(): array

plugins/jwt-auth/tests/test_app/src/Controller/TestController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class TestController extends Controller
1717
public function beforeFilter(EventInterface $event)
1818
{
1919
parent::beforeFilter($event);
20-
$this->loadComponent('Authentication.Authentication');
2120
$this->loadComponent('RequestHandler');
21+
$this->loadComponent('Authentication.Authentication');
2222
$this->Authentication->allowUnauthenticated(['login','jwks']);
2323
}
2424

0 commit comments

Comments
 (0)