Skip to content

Commit 5bea6d7

Browse files
authored
Merge pull request #114 from FriendsOfCake/dakota-patch-1
2 parents 0e7b21c + f2b64f3 commit 5bea6d7

File tree

7 files changed

+19
-52
lines changed

7 files changed

+19
-52
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"require":{
3232
"php": ">=7.2.0",
3333
"cakephp/cakephp": "^4.0.2",
34-
"friendsofcake/crud": "^6.0-beta",
34+
"friendsofcake/crud": "^6.0-beta3",
3535
"neomerx/json-api": "^4.0"
3636
},
3737
"require-dev": {
@@ -66,7 +66,7 @@
6666
"cs-check": "phpcs -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
6767
"cs-fix": "phpcbf --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
6868
"phpstan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:\"^0.12\" && mv composer.backup composer.json",
69-
"phpstan": "phpstan analyse src/",
69+
"phpstan": "phpstan analyse --memory-limit=3G src/",
7070
"test": "phpunit"
7171
}
7272
}

phpstan.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ parameters:
99
ignoreErrors:
1010
# ORM
1111
- '#Call to an undefined method Cake\\Datasource\\EntityInterface::visibleProperties\(\)\.#'
12-
- '#Call to an undefined method Cake\\ORM\\Association::getAlias\(\)\.#'
13-
- '#Method CrudJsonApi\\Listener\\JsonApiListener::_getSingleEntity\(\) should return Cake\\Datasource\\EntityInterface but returns array\|Cake\\Datasource\\EntityInterface\|null\.#'
14-
- '#Method CrudJsonApi\\Listener\\JsonApiListener::_getSingleEntity\(\) should return Cake\\Datasource\\EntityInterface but returns array\|object\|null\.#'
1512

1613
# Crud properties
1714
- '#Access to an undefined property object::\$created#'

src/Listener/JsonApi/DocumentValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ protected function _primaryDataMayHaveRelationships(): bool
259259
if (empty($relationships)) {
260260
$this->_errorCollection->addRelationshipsError(
261261
$title = '_required',
262-
$detail = "Relationships object does not contain any members",
262+
$detail = 'Relationships object does not contain any members',
263263
$status = null,
264264
$idx = null,
265265
$aboutLink = $this->_getAboutLink('http://jsonapi.org/format/#crud-creating')

src/Listener/JsonApiListener.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
namespace CrudJsonApi\Listener;
55

6-
use Cake\Core\Configure;
76
use Cake\Datasource\EntityInterface;
87
use Cake\Datasource\RepositoryInterface;
98
use Cake\Datasource\ResultSetDecorator;
109
use Cake\Datasource\ResultSetInterface;
1110
use Cake\Event\EventInterface;
1211
use Cake\Http\Exception\BadRequestException;
12+
use Cake\Http\Exception\MethodNotAllowedException;
1313
use Cake\Http\Response;
1414
use Cake\ORM\Association;
1515
use Cake\ORM\Query;
@@ -110,28 +110,6 @@ public function implementedEvents(): array
110110
];
111111
}
112112

113-
/**
114-
* setup
115-
*
116-
* Called when the listener is created
117-
*
118-
* @return void
119-
*/
120-
public function setup(): void
121-
{
122-
if (!$this->_checkRequestType('jsonapi')) {
123-
return;
124-
}
125-
126-
$appClass = Configure::read('App.namespace') . '\Application';
127-
128-
// If `App\Application` class exists it means Cake 3.3's PSR7 middleware
129-
// implementation is used and it's too late to register new error handler.
130-
if (!class_exists($appClass, false)) {
131-
$this->registerExceptionHandler();
132-
}
133-
}
134-
135113
/**
136114
* beforeHandle
137115
*
@@ -435,7 +413,7 @@ protected function _includeParameter($includes, Subject $subject, $options): voi
435413
}
436414

437415
if ($options['blacklist'] === true || $options['whitelist'] === false) {
438-
throw new BadRequestException("The include parameter is not supported");
416+
throw new BadRequestException('The include parameter is not supported');
439417
}
440418

441419
$this->setConfig('include', []);
@@ -790,25 +768,23 @@ protected function _validateConfigOptions(): void
790768
* Override ApiListener method to enforce required JSON API request methods.
791769
*
792770
* @throws \Cake\Http\Exception\BadRequestException
793-
* @return bool
771+
* @return void
794772
*/
795-
protected function _checkRequestMethods(): bool
773+
protected function _checkRequestMethods(): void
796774
{
797775
if ($this->_request()->is('put')) {
798-
throw new BadRequestException('JSON API does not support the PUT method, use PATCH instead');
776+
throw new MethodNotAllowedException('JSON API does not support the PUT method, use PATCH instead');
799777
}
800778

801779
if (!$this->_request()->contentType()) {
802-
return true;
780+
return;
803781
}
804782

805783
if ($this->_request()->contentType() !== self::MIME_TYPE) {
806784
throw new BadRequestException(
807785
'JSON API requests with data require the "' . self::MIME_TYPE . '" Content-Type header'
808786
);
809787
}
810-
811-
return true;
812788
}
813789

814790
/**
@@ -866,11 +842,11 @@ protected function _getFindResult(Subject $subject)
866842
*
867843
* @param \Crud\Event\Subject $subject Subject
868844
* @return \Cake\Datasource\EntityInterface|null
869-
* @phpstan-ignore
870845
*/
871846
protected function _getSingleEntity(Subject $subject): ?EntityInterface
872847
{
873848
if (!empty($subject->entities) && $subject->entities instanceof Query) {
849+
// @phpstan-ignore-next-line
874850
return (clone $subject->entities)->first();
875851
}
876852

@@ -987,7 +963,7 @@ protected function _getRepositoryList(RepositoryInterface $repository, array $as
987963
];
988964

989965
if ($association['association'] === null) {
990-
throw new InvalidArgumentException("Association does not have an association object set");
966+
throw new InvalidArgumentException('Association does not have an association object set');
991967
}
992968

993969
$associationRepository = $association['association']->getTarget();

src/Schema/JsonApi/DynamicEntitySchema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,13 @@ public function getRelationshipRelatedLink($entity, string $name): LinkInterface
356356
// generate the link for hasMany relationship
357357
$foreignKeys = (array)$association->getForeignKey();
358358
$primaryKeys = $entity->extract((array)$this->getRepository()->getPrimaryKey());
359-
$keys = array_combine($foreignKeys, $primaryKeys);
359+
$keys = (array)array_combine($foreignKeys, $primaryKeys);
360360

361361
$url = Router::url(
362362
$this->_getRepositoryRoutingParameters($relatedRepository) + $keys + [
363363
'_method' => 'GET',
364364
'action' => 'index',
365-
'?' => $keys
365+
'?' => $keys,
366366
],
367367
$this->view->getConfig('absoluteLinks', false)
368368
);

tests/TestCase/Listener/JsonApiListenerTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Cake\Event\Event;
1010
use Cake\Filesystem\File;
1111
use Cake\Http\Exception\BadRequestException;
12+
use Cake\Http\Exception\MethodNotAllowedException;
1213
use Cake\Http\Response;
1314
use Cake\Http\ServerRequest;
1415
use Cake\ORM\Entity;
@@ -192,10 +193,6 @@ public function testBeforeHandle()
192193
->method('_convertJsonApiDocumentArray')
193194
->willReturn([]);
194195

195-
$listener
196-
->method('_checkRequestMethods')
197-
->willReturn(true);
198-
199196
$listener->beforeHandle(new Event('Crud.beforeHandle'));
200197
$this->assertTrue(true);
201198
}
@@ -737,15 +734,16 @@ public function testCheckRequestMethodsSuccess()
737734
$listener->setupDetectors();
738735

739736
$this->setReflectionClassInstance($listener);
740-
$this->assertTrue($this->callProtectedMethod('_checkRequestMethods', [], $listener));
737+
$this->callProtectedMethod('_checkRequestMethods', [], $listener);
738+
$this->assertTrue(true); //No exception was thrown
741739
}
742740

743741
/**
744742
* Make sure the listener fails on non JSON API request Content-Type header
745743
*/
746744
public function testCheckRequestMethodsFailContentHeader()
747745
{
748-
$this->expectException('Cake\Http\Exception\BadRequestException');
746+
$this->expectException(BadRequestException::class);
749747
$this->expectExceptionMessage('JSON API requests with data require the "application/vnd.api+json" Content-Type header');
750748
$request = new ServerRequest();
751749
$request = $request->withEnv('HTTP_ACCEPT', 'application/vnd.api+json')
@@ -765,7 +763,7 @@ public function testCheckRequestMethodsFailContentHeader()
765763
*/
766764
public function testCheckRequestMethodsFailOnPutMethod()
767765
{
768-
$this->expectException('Cake\Http\Exception\BadRequestException');
766+
$this->expectException(MethodNotAllowedException::class);
769767
$this->expectExceptionMessage('JSON API does not support the PUT method, use PATCH instead');
770768
$request = new ServerRequest();
771769
$request = $request->withEnv('HTTP_ACCEPT', 'application/vnd.api+json')
@@ -1148,10 +1146,6 @@ public function testCheckRequestData()
11481146
->method('_convertJsonApiDocumentArray')
11491147
->willReturn([]);
11501148

1151-
$listener
1152-
->method('_checkRequestMethods')
1153-
->willReturn(true);
1154-
11551149
$this->setReflectionClassInstance($listener);
11561150

11571151
// assert null if there is no Content-Type

tests/test_app/src/Model/Table/CountriesTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function validationDefault(Validator $validator): Validator
4646
->add('code', [
4747
'UPPERCASE_ONLY' => [
4848
'rule' => ['custom', '/^([A-Z]+)+$/'],
49-
'message' => "Field must be uppercase only",
49+
'message' => 'Field must be uppercase only',
5050
],
5151
]);
5252

0 commit comments

Comments
 (0)