Skip to content

Commit

Permalink
Update to CakePHP 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Feb 9, 2024
1 parent 8948d0e commit 5a26db5
Show file tree
Hide file tree
Showing 25 changed files with 185 additions and 390 deletions.
48 changes: 16 additions & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ name: CakePHP Plugin CI
on:
push:
branches:
- cake4
- cake3
- cake5
pull_request:
branches:
- '*'
Expand All @@ -13,40 +12,29 @@ on:

jobs:
testsuite:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
cakephp-version: ['4.5.*']
php-version: ['7.4', '8.0', '8.1', '8.2']
cakephp-version: ['5.0.*']
php-version: ['8.2', '8.3']
db-type: ['mysql']
prefer-lowest: ['']
coverage: ['no']
include:
- cakephp-version: '4.3.*'
php-version: '7.2'
db-type: 'mysql:5.7'
prefer-lowest: 'prefer-lowest'
coverage: 'no'
- cakephp-version: '4.3.*'
- cakephp-version: '5.0.*'
php-version: '8.1'
db-type: 'mysql'
prefer-lowest: ''
coverage: 'no'

- cakephp-version: '4.4.*'
php-version: '7.4'
db-type: 'mysql:5.7'
db-type: 'mysql:8.0'
prefer-lowest: 'prefer-lowest'
coverage: 'no'
- cakephp-version: '4.4.*'
- cakephp-version: '5.0.*'
php-version: '8.2'
db-type: 'mysql'
prefer-lowest: ''
coverage: 'no'

- php-version: '8.2'
cakephp-version: '4.5.*'
- php-version: '8.3'
cakephp-version: '5.*'
db-type: 'mysql'
prefer-lowest: ''
coverage: 'coverage'
Expand All @@ -56,9 +44,9 @@ jobs:
if: matrix.db-type == 'mysql'
run: docker run --rm --name=mysqld -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=cakephp_test -p 3306:3306 -d mysql --default-authentication-plugin=mysql_native_password --disable-log-bin

- name: Setup MySQL 5.7
if: matrix.db-type == 'mysql:5.7'
run: docker run --rm --name=mysqld -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=cakephp_test -p 3306:3306 -d mysql:5.7 --character-set-server=utf8 --default-authentication-plugin=mysql_native_password --disable-log-bin
- name: Setup MySQL 8.0
if: matrix.db-type == 'mysql:8.0'
run: docker run --rm --name=mysqld -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=cakephp_test -p 3306:3306 -d mysql:8.0 --default-authentication-plugin=mysql_native_password --disable-log-bin

- uses: actions/checkout@v3
with:
Expand Down Expand Up @@ -105,18 +93,14 @@ jobs:
composer update
fi
- name: Setup problem matchers for PHPUnit
if: matrix.php-version == '7.4' && matrix.db-type == 'mysql'
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Run PHPUnit
env:
REDIS_PORT: ${{ job.services.redis.ports['6379'] }}
MEMCACHED_PORT: ${{ job.services.memcached.ports['11211'] }}
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_test?encoding=utf8mb4&init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"'; fi
if [[ ${{ matrix.db-type }} == 'mysql:5.7' ]]; then export DB_URL='mysql://root:[email protected]/cakephp_test?encoding=utf8mb4&init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"'; fi
if [[ ${{ matrix.db-type }} == 'mysql:8.0' ]]; then export DB_URL='mysql://root:[email protected]/cakephp_test?encoding=utf8mb4&init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"'; fi
if [[ ${{ matrix.coverage }} == 'coverage' ]]; then
export CODECOVERAGE=1 && vendor/bin/phpunit --stderr --verbose --coverage-clover=coverage.xml
Expand All @@ -130,7 +114,7 @@ jobs:

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

steps:
- uses: actions/checkout@v3
Expand All @@ -140,7 +124,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: '8.3'
extensions: mbstring, intl, apcu
coverage: none

Expand All @@ -159,7 +143,7 @@ jobs:
key: ${{ runner.os }}-composer-${{ steps.key-date.outputs.date }}-${{ hashFiles('composer.json') }}-${{ matrix.prefer-lowest }}

- name: Get CodeSniffer
run: composer require cakephp/cakephp-codesniffer:^4.0
run: composer require cakephp/cakephp-codesniffer:^5.0
#
# - name: composer install
# run: composer stan-setup
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ActivityLogger plugin for CakePHP 4.x
# ActivityLogger plugin for CakePHP 5.x

<p style="text-align: center">
<a href="LICENSE.txt" target="_blank">
Expand All @@ -22,7 +22,7 @@ You can install this plugin into your CakePHP application using [composer](http:
The recommended way to install composer packages is:

```
composer require elstc/cakephp-activity-logger:^2.0
composer require elstc/cakephp-activity-logger:^3.0
```

### Load plugin
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
"type": "cakephp-plugin",
"license": ["MIT"],
"require": {
"php": ">=7.2",
"php": ">=8.1",
"ext-json": "*",
"ext-pdo": "*",
"cakephp/cakephp": "^4.3",
"psr/log": "^1.0 || ^2.0"
"cakephp/cakephp": "^5.0",
"psr/log": "^3.0"
},
"require-dev": {
"cakephp/authentication": "^2.0",
"cakephp/cakephp-codesniffer": "^4.6",
"cakephp/migrations": "^3.4",
"phpunit/phpunit": "^8.5 || ^9.3"
"cakephp/authentication": "^3.0",
"cakephp/cakephp-codesniffer": "^5.0",
"cakephp/migrations": "^4.0",
"phpunit/phpunit": "^10.1"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 1 addition & 5 deletions config/Migrations/20220209074122_AlterCharsetToUtf8mb4.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ class AlterCharsetToUtf8mb4 extends AbstractMigration//@codingStandardsIgnoreEnd
*/
public function up()
{
if (class_exists('\Migrations\CakeAdapter') && $this->getAdapter() instanceof \Migrations\CakeAdapter) {
$schemaCollection = new \Cake\Database\Schema\Collection($this->getAdapter()->getCakeConnection());
} else {
$schemaCollection = $this->getQueryBuilder()->getConnection()->getSchemaCollection();
}
$schemaCollection = $this->getQueryBuilder('select')->getConnection()->getSchemaCollection();

if ($schemaCollection === null) {
throw new Exception('$schemaCollection not exists.');
Expand Down
54 changes: 28 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
<?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"/>
<ini name="apc.enable_cli" value="1"/>
</php>
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.1/phpunit.xsd"
cacheDirectory=".phpunit.cache"
>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Elastic/ActivityLogger Test Suite">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>
<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Elastic/ActivityLogger Test Suite">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>

<!-- Load extension for fixtures -->
<extensions>
<extension class="Cake\TestSuite\Fixture\PHPUnitExtension"/>
</extensions>

<!-- Prevent coverage reports from looking in tests and vendors -->
<coverage>
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
<!-- Load extension for fixtures -->
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>

<!-- Prevent coverage reports from looking in tests and vendors -->
<source>
<include>
<directory suffix=".php">./src/</directory>
</include>
</source>
</phpunit>
58 changes: 12 additions & 46 deletions src/Controller/Component/AutoIssuerComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

namespace Elastic\ActivityLogger\Controller\Component;

use ArrayAccess;
use Cake\Controller\Component;
use Cake\Controller\Component\AuthComponent;
use Cake\Controller\ComponentRegistry;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\Event\EventManager;
use Cake\ORM\Entity;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\Table;
use Cake\Utility\Hash;

/**
Expand All @@ -32,7 +34,7 @@ class AutoIssuerComponent extends Component
*
* @var array
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'userModel' => 'Users',
'identityAttribute' => 'identity',
'initializedTables' => [],
Expand All @@ -43,12 +45,12 @@ class AutoIssuerComponent extends Component
*
* @var \Cake\ORM\Entity|null
*/
protected $issuer;
protected ?Entity $issuer = null;

/**
* @var \Cake\ORM\Table[]
* @var array<\Cake\ORM\Table>
*/
protected $tables = [];
protected array $tables = [];

/**
* AutoIssuerComponent constructor.
Expand All @@ -71,7 +73,6 @@ public function implementedEvents(): array
EventManager::instance()->on('Model.initialize', [$this, 'onInitializeModel']);

return parent::implementedEvents() + [
'Auth.afterIdentify' => 'onAfterIdentifyAtAuth',
'Authentication.afterIdentify' => 'onAfterIdentifyAtAuthentication',
];
}
Expand All @@ -83,16 +84,6 @@ public function implementedEvents(): array
*/
public function startup(): void
{
$loadedComponents = $this->_registry->loaded();

// Get a logged-in user from AuthComponent
if (in_array('Auth', $loadedComponents)) {
$auth = $this->_registry->get('Auth');
if (is_a($auth, AuthComponent::class)) {
$this->issuer = $this->getIssuerFromUserArray($auth->user());
}
}

// Get a logged-in user from the request identity attribute
if (!$this->issuer) {
$identity = $this->getController()->getRequest()
Expand All @@ -111,31 +102,6 @@ public function startup(): void
$this->setIssuerToAllModel($this->issuer);
}

/**
* on Auth.afterIdentify
*
* - get issuer from event data
* - register issuer to the model
*
* @param \Cake\Event\Event $event the Event
* @return void
* @noinspection PhpUnused
*/
public function onAfterIdentifyAtAuth(Event $event): void
{
/** @var array $user */
[$user] = $event->getData();
$this->issuer = $this->getIssuerFromUserArray($user);

if (!$this->issuer) {
// not logged in
return;
}

// register issuer to the model
$this->setIssuerToAllModel($this->issuer);
}

/**
* on Authentication.afterIdentify
*
Expand Down Expand Up @@ -198,7 +164,7 @@ private function setInitializedTables(array $tables): void
{
foreach ($tables as $tableName) {
if ($this->getTableLocator()->exists($tableName)) {
$this->tables[$tableName] = $this->getTableLocator()->get($tableName);
$this->tables[$tableName] = $this->fetchTable($tableName);
}
}
}
Expand All @@ -221,10 +187,10 @@ private function setIssuerToAllModel(EntityInterface $issuer): void
/**
* Get issuer from logged in user data
*
* @param array|\ArrayAccess|null $user a User entity
* @param \ArrayAccess|array|null $user a User entity
* @return \Cake\Datasource\EntityInterface|null
*/
private function getIssuerFromUserArray($user): ?EntityInterface
private function getIssuerFromUserArray(array|ArrayAccess|null $user): ?EntityInterface
{
if ($user === null) {
return null;
Expand All @@ -244,8 +210,8 @@ private function getIssuerFromUserArray($user): ?EntityInterface
*
* @return \Cake\ORM\Table
*/
private function getUserModel(): \Cake\ORM\Table
private function getUserModel(): Table
{
return $this->getTableLocator()->get($this->getConfig('userModel'));
return $this->fetchTable($this->getConfig('userModel'));
}
}
Loading

0 comments on commit 5a26db5

Please sign in to comment.