Skip to content

Commit

Permalink
Merge pull request #670 from FriendsOfCake/cake-4.3
Browse files Browse the repository at this point in the history
Fix deprecation warnings on CakePHP 4.3.
  • Loading branch information
ADmad committed Feb 1, 2022
2 parents 9791e28 + c9f600a commit ace4262
Show file tree
Hide file tree
Showing 28 changed files with 137 additions and 79 deletions.
33 changes: 20 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['7.2', '7.4', '8.0']
php-version: ['7.4', '8.0']
db-type: [sqlite, mysql, pgsql]
name: PHP ${{ matrix.php-version }} & ${{ matrix.db-type }}
prefer-lowest: ['']
include:
- php-version: '7.2'
db-type: 'mysql'
prefer-lowest: 'prefer-lowest'
- php-version: '8.1'
db-type: 'mysql'

services:
postgres:
Expand All @@ -21,9 +27,7 @@ jobs:
POSTGRES_PASSWORD: postgres

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- uses: actions/checkout@v2

- name: Setup Service
if: matrix.db-type == 'mysql'
Expand All @@ -39,32 +43,35 @@ jobs:
coverage: pcov

- name: Composer install
run: composer install
run: |
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
composer update --prefer-lowest --prefer-stable
else
composer update
fi
- name: Run PHPUnit
run: |
if [[ ${{ matrix.db-type }} == 'sqlite' ]]; then export DB_URL='sqlite:///:memory:'; fi
if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_URL='mysql://root:root@127.0.0.1/cakephp'; fi
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:postgres@127.0.0.1/postgres'; fi
if [[ ${{ matrix.php-version }} == '7.2' && ${{ matrix.db-type }} == 'sqlite' ]]; then
if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.db-type }} == 'sqlite' ]]; then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Code Coverage Report
if: success() && matrix.php-version == '7.2' && matrix.db-type == 'sqlite'
uses: codecov/codecov-action@v1
if: success() && matrix.php-version == '7.4' && matrix.db-type == 'sqlite'
uses: codecov/codecov-action@v2

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -74,7 +81,7 @@ jobs:
coverage: none

- name: Composer Install
run: composer require --dev cakephp/cakephp-codesniffer:^4.1 psalm/phar:^3.18 phpstan/phpstan:^0.12
run: composer require --dev cakephp/cakephp-codesniffer:^4.1 psalm/phar:~4.19 phpstan/phpstan:~1.4

- name: Run phpcs
run: vendor/bin/phpcs --report=checkstyle --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"cakephp/cakephp": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "~8.5.0",
"friendsofcake/cakephp-test-utilities": "^2.0",
"phpunit/phpunit": "^8.5.23 || ^9.3",
"friendsofcake/cakephp-test-utilities": "^2.0.1",
"friendsofcake/search": "^6.0"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parameters:
paths:
- src
checkMissingIterableValueType: false
excludes_analyse:
excludePaths:
- */src/TestSuite/*
universalObjectCratesClasses:
- Crud\Event\Subject
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
</ignoreFiles>
</projectFiles>

<stubs>
<file name="src/Error/Exception/CrudException.php" preloadClasses="true" />
</stubs>

<universalObjectCrates>
<class name="Crud\Event\Subject" />
</universalObjectCrates>
Expand Down
2 changes: 1 addition & 1 deletion src/Action/BaseAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ protected function _deriveResourceName(): string
/**
* Additional auxiliary events emitted if certain traits are loaded
*
* @return array
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
Expand Down
17 changes: 12 additions & 5 deletions src/Controller/Component/CrudComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class CrudComponent extends Component
*
* `eventLogging` boolean to determine whether the class should log triggered events.
*
* @var array
* @var array<string, mixed>
*/
protected $_defaultConfig = [
'actions' => [],
Expand Down Expand Up @@ -611,13 +611,12 @@ public function eventLog(): array
/**
* Sets the model class to be used during the action execution.
*
* @param string $modelName The name of the model to load.
* @param string $modelName The name of the model to use.
* @return void
*/
public function useModel(string $modelName): void
{
$this->_controller->loadModel($modelName);
[, $this->_modelName] = pluginSplit($modelName);
$this->_modelName = $modelName;
}

/**
Expand All @@ -627,7 +626,15 @@ public function useModel(string $modelName): void
*/
public function table(): Table
{
return $this->_controller->{$this->_modelName};
if (method_exists($this->_controller, 'fetchTable')) {
return $this->_controller->fetchTable($this->_modelName);
}

/**
* @var \Cake\ORM\Table
* @psalm-suppress DeprecatedMethod
*/
return $this->_controller->loadModel($this->_modelName);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Core/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(Controller $Controller, array $config = [])
/**
* List of implemented events
*
* @return array
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
Expand Down
9 changes: 8 additions & 1 deletion src/Core/ProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,17 @@ protected function _response(): Response
*/
protected function _table()
{
$modelType = $this->getConfig('modelFactory');

if (!$modelType && method_exists($this->_controller(), 'fetchTable')) {
return $this->_controller()->fetchTable();
}

/** @psalm-suppress DeprecatedMethod */
return $this->_controller()
->loadModel(
null,
$this->getConfig('modelFactory') ?: $this->_controller()->getModelType()
$modelType ?: $this->_controller()->getModelType()
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Error/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ValidationException extends BadRequestException
*/
public function __construct(EntityInterface $entity, int $code = 422, ?Throwable $previous = null)
{
$this->_validationErrors = array_filter((array)$entity->getErrors());
$this->_validationErrors = array_filter($entity->getErrors());
$flat = Hash::flatten($this->_validationErrors);

$errorCount = $this->_validationErrorCount = count($flat);
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/ApiListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ApiListener extends BaseListener
*
* We attach at priority 10 so normal bound events can run before us
*
* @return array
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/ApiPaginationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ApiPaginationListener extends BaseListener
*
* We attach at priority 10 so normal bound events can run before us
*
* @return array
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
Expand Down
3 changes: 2 additions & 1 deletion src/Listener/ApiQueryLogListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ApiQueryLogListener extends BaseListener
*
* We attach at priority 10 so normal bound events can run before us
*
* @return array
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
Expand Down Expand Up @@ -65,6 +65,7 @@ public function setupLogging(EventInterface $event): void
try {
$connection = $this->_getSource($connectionName);
$connection->enableQueryLogging(true);
/** @psalm-suppress InternalMethod */
$connection->setLogger(new QueryLogger());
} catch (MissingDatasourceConfigException $e) {
//Safe to ignore this :-)
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/BaseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class BaseListener extends BaseObject implements EventListenerInterface
* - invalidId : Called if the ID format validation failed
* - setFlash : Called before any FlashComponent::set()
*
* @return array
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/RedirectListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RedirectListener extends BaseListener
* Returns a list of all events that will fire in the controller during its lifecycle.
* You can override this function to add your own listener callbacks
*
* @return array
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/RelatedModelsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RelatedModelsListener extends BaseListener
/**
* Returns a list of events this listener is interested in.
*
* @return array
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/SearchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SearchListener extends BaseListener
* Returns a list of all events that will fire in the controller during its lifecycle.
* You can override this function to add your own listener callbacks
*
* @return array
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
Expand Down
6 changes: 4 additions & 2 deletions src/TestSuite/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public function setUp(): void

Router::extensions('json');

Router::connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);
Router::connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);
$routeBuilder = Router::createRouteBuilder('/');

$routeBuilder->connect('/{controller}', ['action' => 'index'], ['routeClass' => 'DashedRoute']);
$routeBuilder->connect('/{controller}/{action}/*', [], ['routeClass' => 'DashedRoute']);
}
}
5 changes: 4 additions & 1 deletion src/Traits/FindMethodTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ protected function _findRecord(?string $id, Subject $subject): EntityInterface

[$finder, $options] = $this->_extractFinder();
$query = $repository->find($finder, $options);
/** @psalm-suppress PossiblyInvalidArgument */
/**
* @psalm-suppress PossiblyInvalidArgument
* @psalm-suppress InvalidArrayOffset
*/
$query->where([current($query->aliasField($repository->getPrimaryKey())) => $id]);

$subject->set([
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/SerializeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function serialize(?array $keys = null)
return (array)$this->getConfig('serialize');
}

return $this->setConfig('serialize', (array)$keys);
return $this->setConfig('serialize', $keys);
}
}
21 changes: 13 additions & 8 deletions tests/TestCase/Action/AddActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Crud\Test\TestCase\Action;

use Cake\Controller\Component\FlashComponent;
use Cake\Core\Configure;
use Cake\Routing\Router;
use Crud\TestSuite\IntegrationTestCase;

Expand Down Expand Up @@ -239,16 +240,17 @@ function ($event) {

$this->_subscribeToEvents($this->_controller);

$this->_controller->Blogs = $this->getMockForModel(
$blogs = $this->getMockForModel(
$this->tableClass,
['save'],
['alias' => 'Blogs', 'table' => 'blogs']
);

$this->_controller->Blogs
$blogs
->expects($this->once())
->method('save')
->will($this->returnValue(false));

$this->getTableLocator()->set('Blogs', $blogs);
}
);

Expand Down Expand Up @@ -314,7 +316,11 @@ function ($event) {
$this->assertFalse($this->_subject->success);
$this->assertFalse($this->_subject->created);

$expected = '<div class="error-message">Name need to be at least 10 characters long</div>';
if (version_compare(Configure::version(), '4.3.0', '>=')) {
$expected = '<div class="error-message" id="name-error">Name need to be at least 10 characters long</div>';
} else {
$expected = '<div class="error-message">Name need to be at least 10 characters long</div>';
}
$this->assertStringContainsString(
$expected,
(string)$this->_response->getBody(),
Expand Down Expand Up @@ -344,10 +350,9 @@ public function apiGetHttpMethodProvider()
*/
public function testApiGet($method)
{
Router::scope('/', function ($routes) {
$routes->setExtensions(['json']);
$routes->fallbacks();
});
Router::createRouteBuilder('/')
->setExtensions(['json'])
->fallbacks();

$this->{$method}('/Blogs/add.json');

Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Action/BaseActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function setUp(): void
->addMethods(['foobar'])
->getMock();
$this->Controller->Crud = $this->Crud;
$this->Controller->modelClass = 'CrudExamples';
$this->Controller->CrudExamples = $this->getTableLocator()->get('Crud.CrudExamples');
$this->Controller->CrudExamples->setAlias('MyModel');
$this->Controller->defaultTable = 'CrudExamples';

$this->getTableLocator()->get('CrudExamples')->setAlias('MyModel');

$this->actionClassName = $this->getMockBuilder(BaseAction::class)
->addMethods(['_handle'])
Expand Down

0 comments on commit ace4262

Please sign in to comment.