Skip to content

Commit

Permalink
Merge pull request #335 from FriendsOfCake/cake-5
Browse files Browse the repository at this point in the history
Cake 5
  • Loading branch information
ADmad authored Sep 13, 2023
2 parents d99b5c7 + 4bcf285 commit acf6cde
Show file tree
Hide file tree
Showing 48 changed files with 333 additions and 459 deletions.
94 changes: 7 additions & 87 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,94 +8,14 @@ on:
branches:
- '*'

permissions:
contents: read

jobs:
testsuite:
runs-on: ubuntu-18.04
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.1']
db-type: [mysql, pgsql]
prefer-lowest: ['']
include:
- php-version: '7.2'
db-type: 'sqlite'
prefer-lowest: 'prefer-lowest'

services:
postgres:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres

steps:
- uses: actions/checkout@v3

- name: Setup Service
if: matrix.db-type == 'mysql'
run: |
sudo service mysql start
mysql -h 127.0.0.1 -u root -proot -e 'CREATE DATABASE cakephp;'
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl, pdo_${{ matrix.db-type }}
coverage: pcov

- name: Composer install
run: |
composer --version
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
composer update --prefer-lowest --prefer-stable
else
composer install
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:[email protected]/cakephp'; fi
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:[email protected]/postgres'; fi
if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.db-type }} == 'mysql' ]]; then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Code Coverage Report
if: success() && matrix.php-version == '7.4' && matrix.db-type == 'mysql'
uses: codecov/codecov-action@v2
uses: cakephp/.github/.github/workflows/[email protected]
secrets: inherit

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

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl
coverage: none
tools: cs2pr

- name: Composer Install
run: composer stan-setup

- name: Run phpcs
run: vendor/bin/phpcs --report=checkstyle src/ tests/ | cs2pr

- name: Run psalm
if: always()
run: vendor/bin/psalm.phar --output-format=github

- name: Run phpstan
if: always()
run: vendor/bin/phpstan
uses: cakephp/.github/.github/workflows/[email protected]
secrets: inherit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ phpunit.xml
composer.lock
.idea/
.phpunit.result.cache
/tools
5 changes: 5 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="1.10.14" installed="1.10.14" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.10.0" installed="5.10.0" location="./tools/psalm" copy="false"/>
</phive>
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
}
],
"require": {
"cakephp/cakephp": "^4.0"
"cakephp/cakephp": "^5.0"
},
"require-dev": {
"cakephp/twig-view": "^1.1.1",
"cakephp/bake": "^2.3",
"phpunit/phpunit": "~8.5.0 || ^9.3",
"cakephp/cakephp-codesniffer": "^4.0",
"muffin/webservice": "^3.0"
"cakephp/twig-view": "^2.0",
"cakephp/bake": "^3.0",
"phpunit/phpunit": "^10.1",
"cakephp/cakephp-codesniffer": "^5.0"
},
"autoload": {
"psr-4": {
Expand All @@ -49,13 +48,15 @@
"scripts": {
"cs-check": "phpcs -p src/ tests/",
"cs-fix": "phpcbf src/ tests/",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:~1.7.0 psalm/phar:~4.23.0 && mv composer.backup composer.json",
"phpstan": "phpstan.phar analyse",
"psalm": "psalm.phar",
"stan-setup": "phive install",
"phpstan": "tools/phpstan analyse",
"psalm": "tools/psalm --show-info=false",
"stan": [
"@phpstan",
"@psalm"
],
"stan-tests": "tools/phpstan analyze -c tests/phpstan.neon",
"stan-baseline": "tools/phpstan --generate-baseline",
"test": "phpunit"
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion docs/basic-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function index()
$query = $this->Posts
// Use the plugins 'search' custom finder and pass in the
// processed query params
->find('search', ['search' => $this->request->getQueryParams()])
->find('search', search: $this->request->getQueryParams())
// You can add extra things to the query if you need to
->contain(['Comments'])
->where(['title IS NOT' => null]);
Expand Down
12 changes: 2 additions & 10 deletions docs/filter-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ Let's use the *backend*'s filters by doing:
```php
// PostsController::action()
$query = $this->Examples
->find('search', [
'search' => $this->request->getQueryParams(),
'collection' => 'backend',
]);
}
->find('search', search: $this->request->getQueryParams(), collection: 'backend');
```

## Filter collection classes
Expand Down Expand Up @@ -85,11 +81,7 @@ You can also specify alternate collection class to use when making find call:
```php
// PostsController::action()
$query = $this->Posts
->find('search', [
'search' => $this->request->getQueryParams(),
'collection' => 'posts_backend',
]);
}
->find('search', search: $this->request->getQueryParams(), collection: 'posts_backend');
```

The above will use `App\Model\Filter\PostsBackendCollection`.
Expand Down
12 changes: 6 additions & 6 deletions docs/filters-and-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ $searchManager->exists('nullable_field');

----------

`Finder` to produce results using a [(custom)](https://book.cakephp.org/4/en/orm/retrieving-data-and-resultsets.html#custom-find-methods) finder
`Finder` to produce results using a [(custom)](https://book.cakephp.org/5/en/orm/retrieving-data-and-resultsets.html#custom-find-methods) finder

```php
// executes the findMyFinder() method in your table class
Expand All @@ -69,7 +69,7 @@ should return bool to specify `isSearch()` (useful when using with `alwaysRun` e
```php
// Completely up to your code inside the callback
$searchManager->callback('category_id', [
'callback' => function (\Cake\ORM\Query $query, array $args, \Search\Model\Filter\Base $filter) {
'callback' => function (\Cake\ORM\Query\SelectQuery $query, array $args, \Search\Model\Filter\Base $filter) {
// $args contains the values given in the request
// $query->where([]);
return true;
Expand Down Expand Up @@ -117,7 +117,7 @@ The following options are supported by all filters.
// PostsTable::initialize()
$searchManager->like('q', [
'fields' => ['Posts.title', 'Authors.title'],
'beforeProcess' => function (\Cake\ORM\Query $query, array $args, \Search\Model\Filter\Base $filter) {
'beforeProcess' => function (\Cake\ORM\Query\SelectQuery $query, array $args, \Search\Model\Filter\Base $filter) {
$query->contain('Authors');
},
]);
Expand Down Expand Up @@ -246,9 +246,9 @@ your best option is to use a `callback` like so:
```php
$searchManager
->callback('category_id', [
'callback' => function (\Cake\ORM\Query $query, array $args, \Search\Model\Filter\Base $filter) {
'callback' => function (\Cake\ORM\Query\SelectQuery $query, array $args, \Search\Model\Filter\Base $filter) {
$query
->innerJoinWith('Categories', function (\Cake\ORM\Query $query) use ($args) {
->innerJoinWith('Categories', function (\Cake\ORM\Query\SelectQuery $query) use ($args) {
return $query->where(['Categories.id IN' => $args['category_id']]);
})
->group('Products.id');
Expand Down Expand Up @@ -309,7 +309,7 @@ class MyCustomFilter extends \Search\Model\Filter\Base
/**
* @return bool
*/
public function process()
public function process(): bool
{
// return false if you want to skip modifying the query based on some condition.

Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 6
level: 7
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
paths:
Expand All @@ -9,5 +9,4 @@ parameters:
- vendor/cakephp/cakephp/src/Core/Exception/CakeException.php
- vendor/cakephp/cakephp/src/Database/Exception/DatabaseException.php
ignoreErrors:
- '#Call to an undefined method Cake\\ORM\\Table::searchManager\(\)#'
- '#PHPDoc tag @var for variable \$model contains unresolvable type#'
33 changes: 16 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="tests/bootstrap.php"
colors="true"
>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/bootstrap.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
>
<testsuites>
<testsuite name="search">
<directory>tests/</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener class="Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="Cake\TestSuite\Fixture\FixtureManager"/>
</arguments>
</listener>
</listeners>
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>

<filter>
<whitelist>
<source>
<include>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</include>
</source>

<php>
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
</php>
</phpunit>
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
usePhpDocMethodsWithoutMagicCall="true"
findUnusedPsalmSuppress="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
17 changes: 8 additions & 9 deletions src/Command/Bake/FilterCollectionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Database\Exception;
use Cake\Database\Exception\DatabaseException;
use Cake\ORM\Table;

/**
Expand All @@ -26,17 +26,17 @@ class FilterCollectionCommand extends BakeCommand
*
* @var string
*/
public $pathFragment = 'Model/Filter/';
public string $pathFragment = 'Model/Filter/';

/**
* @var string
*/
protected $_name;
protected string $_name;

/**
* @var string[][]
* @var array<array<string>>
*/
protected $_map = [
protected array $_map = [
'value' => [
'integer',
'tinyinteger',
Expand All @@ -53,9 +53,9 @@ class FilterCollectionCommand extends BakeCommand
];

/**
* @var string[]
* @var array<string>
*/
protected $_ignoreFields = [
protected array $_ignoreFields = [
'lft',
'rght',
'password',
Expand Down Expand Up @@ -181,7 +181,7 @@ protected function getFields(string $name, ?string $modelName): array
try {
$table = $this->getTableLocator()->get($model);
$columns = $table->getSchema()->columns();
} catch (Exception $exception) {
} catch (DatabaseException) {
return [];
}

Expand All @@ -200,7 +200,6 @@ protected function getFields(string $name, ?string $modelName): array

if (in_array($type, $this->_map['like'], true)) {
$fields[$column] = 'like';
continue;
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/Controller/Component/SearchComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SearchComponent extends Component
*
* @var array<string, mixed>
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'actions' => ['index', 'lookup'],
'queryStringWhitelist' => ['sort', 'direction', 'limit'],
'queryStringBlacklist' => ['_csrfToken', '_Token'],
Expand Down Expand Up @@ -91,20 +91,18 @@ public function startup(): ?Response
*
* @return void
*/
public function beforeRender()
public function beforeRender(): void
{
if (!$this->_isSearchAction()) {
return;
}

$controller = $this->getController();
try {
$method = 'loadModel';
if (method_exists($controller, 'fetchTable')) {
$method = 'fetchTable';
}
/** @var \Cake\ORM\Table&\Search\Model\Behavior\SearchBehavior $model */
$model = $controller->{$method}($this->getConfig('modelClass'));
/**
* @var \Cake\ORM\Table&\Search\Model\Behavior\SearchBehavior $model
*/
$model = $controller->fetchTable($this->getConfig('modelClass'));
} catch (UnexpectedValueException $e) {
return;
}
Expand Down
Loading

0 comments on commit acf6cde

Please sign in to comment.