From 44e0c53ea4eb659252dd9d755cc816ec8cdf9f65 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 17 Jul 2023 18:09:38 +0530 Subject: [PATCH 1/2] Updates for phpunit 10 --- .github/workflows/ci.yml | 2 +- .gitignore | 1 + composer.json | 2 +- phpunit.xml.dist | 22 ++++++------------- src/Action/DashboardAction.php | 1 + src/Dashboard/Dashboard.php | 1 + src/Listener/Traits/FormTypeTrait.php | 1 + src/Listener/ViewListener.php | 1 + src/View/Helper/CrudViewHelper.php | 1 + tests/TestCase/Dashboard/DashboardTest.php | 1 + .../View/Widget/DateTimeWidgetTest.php | 2 +- 11 files changed, 17 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3a9f666f..5301470b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: php-version: '8.1' extensions: mbstring, intl coverage: none - tools: vimeo/psalm:5, phpstan:1.9, cs2pr + tools: vimeo/psalm:5, phpstan:1.10, cs2pr - name: Composer Install run: composer require --dev cakephp/cakephp-codesniffer:^5.0 diff --git a/.gitignore b/.gitignore index 5209c916..ad935574 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ vendor/ composer.lock tmp .phpunit.result.cache +.phpunit.cache diff --git a/composer.json b/composer.json index 4f9bed6d..786e3c73 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "require-dev": { "friendsofcake/cakephp-test-utilities": "dev-cake-5.x", "markstory/asset_compress": "5.x-dev", - "phpunit/phpunit": "^9.5.19" + "phpunit/phpunit": "^10.1" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 10712c99..f6a7ea06 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,27 +1,19 @@ - + ./tests/TestCase - - + - - - - src/ - - - + + + src/ + + diff --git a/src/Action/DashboardAction.php b/src/Action/DashboardAction.php index b8e89fdc..9001fe8f 100644 --- a/src/Action/DashboardAction.php +++ b/src/Action/DashboardAction.php @@ -6,6 +6,7 @@ use Crud\Action\BaseAction; use Crud\Traits\ViewTrait; use CrudView\Dashboard\Dashboard; +use function Cake\I18n\__d; class DashboardAction extends BaseAction { diff --git a/src/Dashboard/Dashboard.php b/src/Dashboard/Dashboard.php index 9afd4872..68c10dcf 100644 --- a/src/Dashboard/Dashboard.php +++ b/src/Dashboard/Dashboard.php @@ -6,6 +6,7 @@ use Cake\Datasource\EntityTrait; use Cake\View\Cell; use InvalidArgumentException; +use function Cake\I18n\__d; class Dashboard { diff --git a/src/Listener/Traits/FormTypeTrait.php b/src/Listener/Traits/FormTypeTrait.php index d2e84a73..e00d40a0 100644 --- a/src/Listener/Traits/FormTypeTrait.php +++ b/src/Listener/Traits/FormTypeTrait.php @@ -6,6 +6,7 @@ use Cake\Controller\Controller; use Crud\Action\BaseAction; use Crud\Action\EditAction; +use function Cake\I18n\__d; trait FormTypeTrait { diff --git a/src/Listener/ViewListener.php b/src/Listener/ViewListener.php index e2add620..60de2716 100644 --- a/src/Listener/ViewListener.php +++ b/src/Listener/ViewListener.php @@ -15,6 +15,7 @@ use CrudView\Listener\Traits\SiteTitleTrait; use CrudView\Listener\Traits\UtilityNavigationTrait; use CrudView\Traits\CrudViewConfigTrait; +use function Cake\I18n\__d; class ViewListener extends BaseListener { diff --git a/src/View/Helper/CrudViewHelper.php b/src/View/Helper/CrudViewHelper.php index f6a9bd5a..6595e1d7 100644 --- a/src/View/Helper/CrudViewHelper.php +++ b/src/View/Helper/CrudViewHelper.php @@ -9,6 +9,7 @@ use Cake\Utility\Text; use Cake\View\Helper; use Cake\View\Helper\FormHelper; +use function Cake\I18n\__d; /** * @property \BootstrapUI\View\Helper\FormHelper $Form diff --git a/tests/TestCase/Dashboard/DashboardTest.php b/tests/TestCase/Dashboard/DashboardTest.php index b885b60d..06dd42a1 100644 --- a/tests/TestCase/Dashboard/DashboardTest.php +++ b/tests/TestCase/Dashboard/DashboardTest.php @@ -9,6 +9,7 @@ use CrudView\Dashboard\Dashboard; use CrudView\View\Cell\DashboardTableCell; use InvalidArgumentException; +use function Cake\I18n\__d; /** * DashboardTest class diff --git a/tests/TestCase/View/Widget/DateTimeWidgetTest.php b/tests/TestCase/View/Widget/DateTimeWidgetTest.php index 55c440c6..8abd06d4 100644 --- a/tests/TestCase/View/Widget/DateTimeWidgetTest.php +++ b/tests/TestCase/View/Widget/DateTimeWidgetTest.php @@ -55,7 +55,7 @@ public function testRenderSimple($compareFileName, $data) * * @return array */ - public function renderProvider() + public static function renderProvider() { return [ [ From d98efdee3e6a7f07b80506c1e1b24b8b1583112d Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 17 Jul 2023 18:20:38 +0530 Subject: [PATCH 2/2] Fix errors reported by static analysis --- phpstan.neon | 2 -- psalm-baseline.xml | 5 ----- psalm.xml | 3 +++ src/Listener/Traits/IndexTypeTrait.php | 4 ++-- src/Listener/ViewListener.php | 23 ++++++++++++++--------- src/Listener/ViewSearchListener.php | 9 ++++++--- src/View/Helper/CrudViewHelper.php | 1 + 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 3b09bdc5..ec34e635 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,5 +9,3 @@ parameters: - src universalObjectCratesClasses: - Crud\Event\Subject - bootstrapFiles: - - vendor/cakephp/cakephp/src/Database/Exception/DatabaseException.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 2e98c4d3..53d6cf0d 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,10 +1,5 @@ - - - $event->getSubject()->element - - $input['type'] diff --git a/psalm.xml b/psalm.xml index c423ba40..e3b7c384 100644 --- a/psalm.xml +++ b/psalm.xml @@ -6,6 +6,9 @@ xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" errorBaseline="psalm-baseline.xml" + usePhpDocMethodsWithoutMagicCall="true" + findUnusedBaselineEntry="true" + findUnusedCode="false" > diff --git a/src/Listener/Traits/IndexTypeTrait.php b/src/Listener/Traits/IndexTypeTrait.php index d19dee76..d2d4f6f8 100644 --- a/src/Listener/Traits/IndexTypeTrait.php +++ b/src/Listener/Traits/IndexTypeTrait.php @@ -84,7 +84,7 @@ protected function _getIndexTitleField(): string $field = $action->getConfig('scaffold.index_title_field'); if ($field === null) { - $field = $this->_table()->getDisplayField(); + $field = $this->_model()->getDisplayField(); } return $field; @@ -154,5 +154,5 @@ abstract protected function _action(?string $name = null): BaseAction; /** * @inheritDoc */ - abstract protected function _table(); + abstract protected function _model(); } diff --git a/src/Listener/ViewListener.php b/src/Listener/ViewListener.php index 60de2716..33e4a204 100644 --- a/src/Listener/ViewListener.php +++ b/src/Listener/ViewListener.php @@ -15,8 +15,12 @@ use CrudView\Listener\Traits\SiteTitleTrait; use CrudView\Listener\Traits\UtilityNavigationTrait; use CrudView\Traits\CrudViewConfigTrait; +use function Cake\Core\pluginSplit; use function Cake\I18n\__d; +/** + * @method \Cake\ORM\Table _model() + */ class ViewListener extends BaseListener { use CrudViewConfigTrait; @@ -134,6 +138,7 @@ public function beforeRender(EventInterface $event): void public function setFlash(EventInterface $event): void { unset($event->getSubject()->params['class']); + /** @psalm-suppress UndefinedPropertyAssignment */ $event->getSubject()->element = ltrim($event->getSubject()->type); } @@ -173,7 +178,7 @@ protected function _getPageTitle(): string $displayFieldValue = $this->_displayFieldValue(); if ( $displayFieldValue === null - || $this->_table()->getDisplayField() === $this->_table()->getPrimaryKey() + || $this->_model()->getDisplayField() === $this->_model()->getPrimaryKey() ) { /** @psalm-var string $primaryKeyValue */ return sprintf('%s %s #%s', $actionName, $controllerName, $primaryKeyValue); @@ -218,12 +223,12 @@ protected function _getRelatedModels(array $associationTypes = []): array if (empty($models)) { $associations = []; if (empty($associationTypes)) { - $associations = $this->_table()->associations(); + $associations = $this->_model()->associations(); } else { foreach ($associationTypes as $assocType) { $associations = array_merge( $associations, - $this->_table()->associations()->getByType($assocType) + $this->_model()->associations()->getByType($assocType) ); } } @@ -268,7 +273,7 @@ protected function _blacklist(): array */ protected function _getPageVariables(): array { - $table = $this->_table(); + $table = $this->_model(); $modelClass = $table->getAlias(); $controller = $this->_controller(); $scope = $this->_action()->getConfig('scope'); @@ -317,7 +322,7 @@ protected function _scaffoldFields(array $associations = []): array } if (empty($scaffoldFields) || $action->getConfig('scaffold.autoFields')) { - $cols = $this->_table()->getSchema()->columns(); + $cols = $this->_model()->getSchema()->columns(); $cols = Hash::normalize($cols); $scope = $action->getConfig('scope'); @@ -554,7 +559,7 @@ protected function _normalizeActions(array $actions): array */ protected function _associations(array $whitelist = []): array { - $table = $this->_table(); + $table = $this->_model(); $associationConfiguration = []; @@ -605,7 +610,7 @@ protected function _associations(array $whitelist = []): array */ protected function _primaryKeyValue(): array|string { - $fields = (array)$this->_table()->getPrimaryKey(); + $fields = (array)$this->_model()->getPrimaryKey(); $values = []; foreach ($fields as $field) { $values[] = $this->_deriveFieldFromContext($field); @@ -628,7 +633,7 @@ protected function _primaryKeyValue(): array|string protected function _displayFieldValue(): string|int|null { /** @psalm-suppress PossiblyInvalidArgument */ - return $this->_deriveFieldFromContext($this->_table()->getDisplayField()); + return $this->_deriveFieldFromContext($this->_model()->getDisplayField()); } /** @@ -641,7 +646,7 @@ protected function _displayFieldValue(): string|int|null protected function _deriveFieldFromContext(string $field): mixed { $controller = $this->_controller(); - $modelClass = $this->_table()->getAlias(); + $modelClass = $this->_model()->getAlias(); $entity = $this->_entity(); $request = $this->_request(); $value = $entity->get($field); diff --git a/src/Listener/ViewSearchListener.php b/src/Listener/ViewSearchListener.php index 13ce81a4..c29b3292 100644 --- a/src/Listener/ViewSearchListener.php +++ b/src/Listener/ViewSearchListener.php @@ -8,6 +8,9 @@ use Cake\Utility\Hash; use Crud\Listener\BaseListener; +/** + * @method \Cake\ORM\Table _model() + */ class ViewSearchListener extends BaseListener { /** @@ -56,7 +59,7 @@ public function implementedEvents(): array */ public function afterPaginate(EventInterface $event): void { - if (!$this->_table()->behaviors()->has('Search')) { + if (!$this->_model()->behaviors()->has('Search')) { return; } @@ -82,13 +85,13 @@ public function fields(): array $fields = $this->getConfig('fields') ?: []; $config = $this->getConfig(); - $schema = $this->_table()->getSchema(); + $schema = $this->_model()->getSchema(); $request = $this->_request(); if ($fields) { $fields = Hash::normalize($fields); } else { - $filters = $this->_table()->searchManager()->getFilters($config['collection']); + $filters = $this->_model()->searchManager()->getFilters($config['collection']); foreach ($filters as $filter) { $opts = $filter->getConfig('form'); diff --git a/src/View/Helper/CrudViewHelper.php b/src/View/Helper/CrudViewHelper.php index 6595e1d7..261302c1 100644 --- a/src/View/Helper/CrudViewHelper.php +++ b/src/View/Helper/CrudViewHelper.php @@ -9,6 +9,7 @@ use Cake\Utility\Text; use Cake\View\Helper; use Cake\View\Helper\FormHelper; +use function Cake\Core\h; use function Cake\I18n\__d; /**