Skip to content

Commit 6f34077

Browse files
authored
Merge pull request #48 from ADmad/cake-5
Cake 5
2 parents 95ad44c + 1fcbf42 commit 6f34077

16 files changed

Lines changed: 120 additions & 225 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
phpunit.xml.dist export-ignore
77
tests export-ignore
88
psalm.xml export-ignore
9+
phpstan.neon export-ignore

.github/workflows/ci.yml

Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -10,94 +10,9 @@ on:
1010

1111
jobs:
1212
testsuite:
13-
runs-on: ubuntu-18.04
14-
strategy:
15-
fail-fast: false
16-
matrix:
17-
php-version: ['7.4', '8.0', '8.1', '8.2']
18-
db-type: [mysql, pgsql]
19-
prefer-lowest: ['']
20-
include:
21-
- php-version: '7.2'
22-
db-type: 'sqlite'
23-
prefer-lowest: 'prefer-lowest'
24-
25-
services:
26-
postgres:
27-
image: postgres
28-
ports:
29-
- 5432:5432
30-
env:
31-
POSTGRES_PASSWORD: postgres
32-
33-
steps:
34-
- uses: actions/checkout@v2
35-
36-
- name: Setup Service
37-
if: matrix.db-type == 'mysql'
38-
run: |
39-
sudo service mysql start
40-
mysql -h 127.0.0.1 -u root -proot -e 'CREATE DATABASE cakephp;'
41-
42-
- name: Setup PHP
43-
uses: shivammathur/setup-php@v2
44-
with:
45-
php-version: ${{ matrix.php-version }}
46-
extensions: mbstring, intl, pdo_${{ matrix.db-type }}
47-
coverage: pcov
48-
49-
- name: Composer install
50-
run: |
51-
composer --version
52-
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
53-
composer update --prefer-lowest --prefer-stable
54-
else
55-
composer install
56-
fi
57-
58-
- name: Run PHPUnit
59-
run: |
60-
if [[ ${{ matrix.db-type }} == 'sqlite' ]]; then export DB_URL='sqlite:///:memory:'; fi
61-
if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_URL='mysql://root:root@127.0.0.1/cakephp?init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'; fi
62-
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:postgres@127.0.0.1/postgres'; fi
63-
64-
if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.db-type }} == 'mysql' ]]; then
65-
vendor/bin/phpunit --coverage-clover=coverage.xml
66-
else
67-
vendor/bin/phpunit
68-
fi
69-
70-
- name: Code Coverage Report
71-
if: success() && matrix.php-version == '7.4' && matrix.db-type == 'mysql'
72-
uses: codecov/codecov-action@v2
13+
uses: ADmad/.github/.github/workflows/testsuite-with-db.yml@master
14+
secrets: inherit
7315

7416
cs-stan:
75-
name: Coding Standard & Static Analysis
76-
runs-on: ubuntu-18.04
77-
78-
steps:
79-
- uses: actions/checkout@v2
80-
81-
- name: Setup PHP
82-
uses: shivammathur/setup-php@v2
83-
with:
84-
php-version: '7.4'
85-
extensions: mbstring, intl
86-
coverage: none
87-
tools: vimeo/psalm:4.4, phpstan:1.0
88-
env:
89-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
90-
91-
- name: Composer Install
92-
run: composer require cakephp/cakephp-codesniffer:^4.2
93-
94-
- name: Run phpcs
95-
run: vendor/bin/phpcs --standard=CakePHP src/ tests/
96-
97-
- name: Run psalm
98-
if: success() || failure()
99-
run: psalm --output-format=github
100-
101-
- name: Run phpstan
102-
if: success() || failure()
103-
run: phpstan
17+
uses: ADmad/.github/.github/workflows/cs-stan.yml@master
18+
secrets: inherit

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
composer.lock
22
vendor
3+
.phpunit.cache
34
.phpunit.result.cache

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"type": "cakephp-plugin",
66
"license": "MIT",
77
"require": {
8-
"cakephp/orm": "^4.0"
8+
"cakephp/orm": "^5.0"
99
},
1010
"require-dev": {
11-
"cakephp/cakephp": "^4.0",
12-
"phpunit/phpunit": "~8.5.0 || ^9.5"
11+
"cakephp/cakephp": "^5.0",
12+
"phpunit/phpunit": "^10.1"
1313
},
1414
"autoload": {
1515
"psr-4": {

phpunit.xml.dist

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit
3-
colors="true"
4-
processIsolation="false"
5-
stopOnFailure="false"
6-
bootstrap="./tests/bootstrap.php"
7-
>
8-
<php>
9-
<ini name="memory_limit" value="-1"/>
10-
</php>
11-
12-
<!-- Add any additional test suites you want to run here -->
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="./tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache">
133
<testsuites>
144
<testsuite name="ADmad/Sequence TestSuite">
155
<directory>./tests/TestCase</directory>
166
</testsuite>
17-
<!-- Add plugin test suites here. -->
187
</testsuites>
19-
20-
<!-- Setup a listener for fixtures -->
21-
<listeners>
22-
<listener
23-
class="\Cake\TestSuite\Fixture\FixtureInjector"
24-
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
25-
<arguments>
26-
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
27-
</arguments>
28-
</listener>
29-
</listeners>
30-
31-
<filter>
32-
<whitelist>
33-
<directory suffix=".php">./src/</directory>
34-
</whitelist>
35-
</filter>
8+
<extensions>
9+
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
10+
</extensions>
11+
<php>
12+
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
13+
</php>
14+
<source>
15+
<include>
16+
<directory suffix=".php">src/</directory>
17+
</include>
18+
</source>
3619
</phpunit>

src/Model/Behavior/SequenceBehavior.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Cake\Datasource\EntityInterface;
1010
use Cake\Event\EventInterface;
1111
use Cake\ORM\Behavior;
12-
use Cake\ORM\Query;
12+
use Cake\ORM\Query\SelectQuery;
1313

1414
/**
1515
* SequenceBehavior maintains a contiguous sequence of integers (starting at 1
@@ -67,7 +67,7 @@ class SequenceBehavior extends Behavior
6767
*
6868
* @var array<string, mixed>
6969
*/
70-
protected $_defaultConfig = [
70+
protected array $_defaultConfig = [
7171
'sequenceField' => 'position',
7272
'scope' => [],
7373
'startAt' => 1,
@@ -78,7 +78,7 @@ class SequenceBehavior extends Behavior
7878
*
7979
* @var array|null
8080
*/
81-
protected $_oldValues;
81+
protected ?array $_oldValues = null;
8282

8383
/**
8484
* Normalize config options.
@@ -108,14 +108,14 @@ public function initialize(array $config): void
108108
* Adds order value if not already set in query.
109109
*
110110
* @param \Cake\Event\EventInterface $event The beforeFind event that was fired.
111-
* @param \Cake\ORM\Query $query The query object.
111+
* @param \Cake\ORM\Query\SelectQuery $query The query object.
112112
* @param \ArrayObject<string, mixed> $options The options passed to the find method.
113113
* @return void
114114
*/
115-
public function beforeFind(EventInterface $event, Query $query, ArrayObject $options): void
115+
public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options): void
116116
{
117117
if (!$query->clause('order')) {
118-
$query->order([$this->_table->aliasField($this->_config['sequenceField']) => 'ASC']);
118+
$query->orderBy([$this->_table->aliasField($this->_config['sequenceField']) => 'ASC']);
119119
}
120120
}
121121

@@ -250,7 +250,7 @@ public function beforeDelete(EventInterface $event, EntityInterface $entity): vo
250250
* @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved.
251251
* @return void
252252
*/
253-
public function afterDelete(EventInterface $event, EntityInterface $entity)
253+
public function afterDelete(EventInterface $event, EntityInterface $entity): void
254254
{
255255
if (!$this->_oldValues) {
256256
return;
@@ -276,7 +276,7 @@ public function afterDelete(EventInterface $event, EntityInterface $entity)
276276
* @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved.
277277
* @return bool
278278
*/
279-
public function moveUp(EntityInterface $entity)
279+
public function moveUp(EntityInterface $entity): bool
280280
{
281281
return $this->_movePosition($entity, '-');
282282
}
@@ -289,7 +289,7 @@ public function moveUp(EntityInterface $entity)
289289
* @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved.
290290
* @return bool
291291
*/
292-
public function moveDown(EntityInterface $entity)
292+
public function moveDown(EntityInterface $entity): bool
293293
{
294294
return $this->_movePosition($entity, '+');
295295
}
@@ -448,7 +448,7 @@ protected function _getOldValues(EntityInterface $entity): array
448448
* @phpstan-ignore-next-line
449449
*/
450450
$primaryKey = $entity->get($this->_table->getPrimaryKey());
451-
$entity = $this->_table->get($primaryKey, ['fields' => $fields]);
451+
$entity = $this->_table->get($primaryKey, fields: $fields);
452452
$values = $entity->extract($fields);
453453
}
454454

@@ -471,7 +471,7 @@ protected function _getOldValues(EntityInterface $entity): array
471471
* @param \Cake\Datasource\EntityInterface $entity Entity.
472472
* @return array|false
473473
*/
474-
protected function _getScope(EntityInterface $entity)
474+
protected function _getScope(EntityInterface $entity): array|false
475475
{
476476
$scope = [];
477477
$config = $this->getConfig();
@@ -504,15 +504,15 @@ protected function _getScope(EntityInterface $entity)
504504
* @param array $scope Array with scope field => scope values, used for conditions.
505505
* @return int Value of order field of last record in set
506506
*/
507-
protected function _getHighestOrder(array $scope = []): int
507+
protected function _getHighestOrder(array $scope): int
508508
{
509509
$orderField = $this->_config['sequenceField'];
510510

511511
// Find the last record in the set
512512
$last = $this->_table->find()
513513
->select([$orderField])
514514
->where($scope)
515-
->order([$orderField => 'DESC'])
515+
->orderBy([$orderField => 'DESC'])
516516
->limit(1)
517517
->enableHydration(false)
518518
->first();
@@ -554,7 +554,7 @@ protected function _getUpdateExpression(string $direction = '+'): QueryExpressio
554554
{
555555
$field = $this->_config['sequenceField'];
556556

557-
return $this->_table->query()->newExpr()
557+
return $this->_table->selectQuery()->newExpr()
558558
->add(new IdentifierExpression($field))
559559
->add('1')
560560
->setConjunction($direction);

src/SequencePlugin.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ADmad\Sequence;
5+
6+
use Cake\Core\BasePlugin;
7+
8+
class SequencePlugin extends BasePlugin
9+
{
10+
/**
11+
* Do bootstrapping or not
12+
*
13+
* @var bool
14+
*/
15+
protected bool $bootstrapEnabled = false;
16+
17+
/**
18+
* Console middleware
19+
*
20+
* @var bool
21+
*/
22+
protected bool $consoleEnabled = false;
23+
24+
/**
25+
* Load routes or not
26+
*
27+
* @var bool
28+
*/
29+
protected bool $routesEnabled = false;
30+
}

tests/Fixture/GroupedItemsFixture.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,7 @@
55

66
class GroupedItemsFixture extends TestFixture
77
{
8-
/**
9-
* fields property.
10-
*
11-
* @var array
12-
*/
13-
public $fields = [
14-
'id' => ['type' => 'integer'],
15-
'name' => ['type' => 'string', 'null' => true],
16-
'group_field' => ['type' => 'integer', 'null' => true],
17-
'position' => ['type' => 'integer', 'null' => true],
18-
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
19-
];
20-
21-
/**
22-
* records property.
23-
*
24-
* @var array
25-
*/
26-
public $records = [
8+
public array $records = [
279
['name' => 'Group 1 Item A', 'group_field' => 1, 'position' => 0],
2810
['name' => 'Group 1 Item B', 'group_field' => 1, 'position' => 1],
2911
['name' => 'Group 1 Item C', 'group_field' => 1, 'position' => 2],

tests/Fixture/ItemsFixture.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,7 @@
55

66
class ItemsFixture extends TestFixture
77
{
8-
/**
9-
* fields property.
10-
*
11-
* @var array
12-
*/
13-
public $fields = [
14-
'id' => ['type' => 'integer'],
15-
'name' => ['type' => 'string', 'null' => true],
16-
'position' => ['type' => 'integer', 'null' => true],
17-
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
18-
];
19-
20-
/**
21-
* records property.
22-
*
23-
* @var array
24-
*/
25-
public $records = [
8+
public array $records = [
269
['name' => 'Item A', 'position' => 0],
2710
['name' => 'Item B', 'position' => 1],
2811
['name' => 'Item C', 'position' => 2],

tests/Fixture/KeywordItemsFixture.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,7 @@
55

66
class KeywordItemsFixture extends TestFixture
77
{
8-
/**
9-
* fields property.
10-
*
11-
* @var array
12-
*/
13-
public $fields = [
14-
'id' => ['type' => 'integer'],
15-
'name' => ['type' => 'string', 'null' => true],
16-
'order' => ['type' => 'integer', 'null' => true],
17-
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
18-
];
19-
20-
/**
21-
* records property.
22-
*
23-
* @var array
24-
*/
25-
public $records = [
8+
public array $records = [
269
['name' => 'Item A', 'order' => 0],
2710
['name' => 'Item B', 'order' => 1],
2811
['name' => 'Item C', 'order' => 2],

0 commit comments

Comments
 (0)