Skip to content

Commit

Permalink
Merge pull request #48 from ADmad/cake-5
Browse files Browse the repository at this point in the history
Cake 5
  • Loading branch information
ADmad authored Sep 29, 2023
2 parents 95ad44c + 1fcbf42 commit 6f34077
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 225 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
phpunit.xml.dist export-ignore
tests export-ignore
psalm.xml export-ignore
phpstan.neon export-ignore
93 changes: 4 additions & 89 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,94 +10,9 @@ on:

jobs:
testsuite:
runs-on: ubuntu-18.04
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.1', '8.2']
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@v2

- 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?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
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: ADmad/.github/.github/workflows/testsuite-with-db.yml@master
secrets: inherit

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

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl
coverage: none
tools: vimeo/psalm:4.4, phpstan:1.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Composer Install
run: composer require cakephp/cakephp-codesniffer:^4.2

- name: Run phpcs
run: vendor/bin/phpcs --standard=CakePHP src/ tests/

- name: Run psalm
if: success() || failure()
run: psalm --output-format=github

- name: Run phpstan
if: success() || failure()
run: phpstan
uses: ADmad/.github/.github/workflows/cs-stan.yml@master
secrets: inherit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
vendor
.phpunit.cache
.phpunit.result.cache
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"type": "cakephp-plugin",
"license": "MIT",
"require": {
"cakephp/orm": "^4.0"
"cakephp/orm": "^5.0"
},
"require-dev": {
"cakephp/cakephp": "^4.0",
"phpunit/phpunit": "~8.5.0 || ^9.5"
"cakephp/cakephp": "^5.0",
"phpunit/phpunit": "^10.1"
},
"autoload": {
"psr-4": {
Expand Down
41 changes: 12 additions & 29 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="./tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<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">
<testsuites>
<testsuite name="ADmad/Sequence TestSuite">
<directory>./tests/TestCase</directory>
</testsuite>
<!-- Add plugin test suites here. -->
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
<php>
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
28 changes: 14 additions & 14 deletions src/Model/Behavior/SequenceBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\ORM\Query\SelectQuery;

/**
* SequenceBehavior maintains a contiguous sequence of integers (starting at 1
Expand Down Expand Up @@ -67,7 +67,7 @@ class SequenceBehavior extends Behavior
*
* @var array<string, mixed>
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'sequenceField' => 'position',
'scope' => [],
'startAt' => 1,
Expand All @@ -78,7 +78,7 @@ class SequenceBehavior extends Behavior
*
* @var array|null
*/
protected $_oldValues;
protected ?array $_oldValues = null;

/**
* Normalize config options.
Expand Down Expand Up @@ -108,14 +108,14 @@ public function initialize(array $config): void
* Adds order value if not already set in query.
*
* @param \Cake\Event\EventInterface $event The beforeFind event that was fired.
* @param \Cake\ORM\Query $query The query object.
* @param \Cake\ORM\Query\SelectQuery $query The query object.
* @param \ArrayObject<string, mixed> $options The options passed to the find method.
* @return void
*/
public function beforeFind(EventInterface $event, Query $query, ArrayObject $options): void
public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options): void
{
if (!$query->clause('order')) {
$query->order([$this->_table->aliasField($this->_config['sequenceField']) => 'ASC']);
$query->orderBy([$this->_table->aliasField($this->_config['sequenceField']) => 'ASC']);
}
}

Expand Down Expand Up @@ -250,7 +250,7 @@ public function beforeDelete(EventInterface $event, EntityInterface $entity): vo
* @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved.
* @return void
*/
public function afterDelete(EventInterface $event, EntityInterface $entity)
public function afterDelete(EventInterface $event, EntityInterface $entity): void
{
if (!$this->_oldValues) {
return;
Expand All @@ -276,7 +276,7 @@ public function afterDelete(EventInterface $event, EntityInterface $entity)
* @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved.
* @return bool
*/
public function moveUp(EntityInterface $entity)
public function moveUp(EntityInterface $entity): bool
{
return $this->_movePosition($entity, '-');
}
Expand All @@ -289,7 +289,7 @@ public function moveUp(EntityInterface $entity)
* @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved.
* @return bool
*/
public function moveDown(EntityInterface $entity)
public function moveDown(EntityInterface $entity): bool
{
return $this->_movePosition($entity, '+');
}
Expand Down Expand Up @@ -448,7 +448,7 @@ protected function _getOldValues(EntityInterface $entity): array
* @phpstan-ignore-next-line
*/
$primaryKey = $entity->get($this->_table->getPrimaryKey());
$entity = $this->_table->get($primaryKey, ['fields' => $fields]);
$entity = $this->_table->get($primaryKey, fields: $fields);
$values = $entity->extract($fields);
}

Expand All @@ -471,7 +471,7 @@ protected function _getOldValues(EntityInterface $entity): array
* @param \Cake\Datasource\EntityInterface $entity Entity.
* @return array|false
*/
protected function _getScope(EntityInterface $entity)
protected function _getScope(EntityInterface $entity): array|false
{
$scope = [];
$config = $this->getConfig();
Expand Down Expand Up @@ -504,15 +504,15 @@ protected function _getScope(EntityInterface $entity)
* @param array $scope Array with scope field => scope values, used for conditions.
* @return int Value of order field of last record in set
*/
protected function _getHighestOrder(array $scope = []): int
protected function _getHighestOrder(array $scope): int
{
$orderField = $this->_config['sequenceField'];

// Find the last record in the set
$last = $this->_table->find()
->select([$orderField])
->where($scope)
->order([$orderField => 'DESC'])
->orderBy([$orderField => 'DESC'])
->limit(1)
->enableHydration(false)
->first();
Expand Down Expand Up @@ -554,7 +554,7 @@ protected function _getUpdateExpression(string $direction = '+'): QueryExpressio
{
$field = $this->_config['sequenceField'];

return $this->_table->query()->newExpr()
return $this->_table->selectQuery()->newExpr()
->add(new IdentifierExpression($field))
->add('1')
->setConjunction($direction);
Expand Down
30 changes: 30 additions & 0 deletions src/SequencePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace ADmad\Sequence;

use Cake\Core\BasePlugin;

class SequencePlugin extends BasePlugin
{
/**
* Do bootstrapping or not
*
* @var bool
*/
protected bool $bootstrapEnabled = false;

/**
* Console middleware
*
* @var bool
*/
protected bool $consoleEnabled = false;

/**
* Load routes or not
*
* @var bool
*/
protected bool $routesEnabled = false;
}
20 changes: 1 addition & 19 deletions tests/Fixture/GroupedItemsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,7 @@

class GroupedItemsFixture extends TestFixture
{
/**
* fields property.
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'null' => true],
'group_field' => ['type' => 'integer', 'null' => true],
'position' => ['type' => 'integer', 'null' => true],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
];

/**
* records property.
*
* @var array
*/
public $records = [
public array $records = [
['name' => 'Group 1 Item A', 'group_field' => 1, 'position' => 0],
['name' => 'Group 1 Item B', 'group_field' => 1, 'position' => 1],
['name' => 'Group 1 Item C', 'group_field' => 1, 'position' => 2],
Expand Down
19 changes: 1 addition & 18 deletions tests/Fixture/ItemsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,7 @@

class ItemsFixture extends TestFixture
{
/**
* fields property.
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'null' => true],
'position' => ['type' => 'integer', 'null' => true],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
];

/**
* records property.
*
* @var array
*/
public $records = [
public array $records = [
['name' => 'Item A', 'position' => 0],
['name' => 'Item B', 'position' => 1],
['name' => 'Item C', 'position' => 2],
Expand Down
19 changes: 1 addition & 18 deletions tests/Fixture/KeywordItemsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,7 @@

class KeywordItemsFixture extends TestFixture
{
/**
* fields property.
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'null' => true],
'order' => ['type' => 'integer', 'null' => true],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
];

/**
* records property.
*
* @var array
*/
public $records = [
public array $records = [
['name' => 'Item A', 'order' => 0],
['name' => 'Item B', 'order' => 1],
['name' => 'Item C', 'order' => 2],
Expand Down
Loading

0 comments on commit 6f34077

Please sign in to comment.