Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ['8.0', '8.1', '8.2', '8.3']

name: PHP ${{ matrix.php-version }}

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl, pdo_sqlite
coverage: none

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer update --prefer-dist --no-interaction --no-progress

- name: Run test suite
run: vendor/bin/phpunit

code-quality:
runs-on: ubuntu-latest
name: Code Quality

steps:
- uses: actions/checkout@v3

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

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer update --prefer-dist --no-interaction --no-progress

- name: Run PHPStan
run: vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr
continue-on-error: true

- name: Run PHP CS Fixer
run: vendor/bin/php-cs-fixer fix --dry-run --diff --verbose
continue-on-error: true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
composer.phar
composer.lock
.php_cs.cache
.php-cs-fixer.cache
.phpunit.result.cache
.DS_Store
Thumbs.db
15 changes: 15 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
$header = 'This file is part of riesenia/cakephp-fetchable package.

Licensed under the MIT License
(c) RIESENIA.com';

$config = new Rshop\CS\Config\Rshop($header);

$config->setStrict()
->setRule('general_phpdoc_annotation_remove', ['annotations' => ['author']])
->getFinder()
->in(__DIR__)
->exclude('vendor');

return $config;
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Total Downloads](https://img.shields.io/packagist/dt/riesenia/cakephp-fetchable.svg?style=flat-square)](https://packagist.org/packages/riesenia/cakephp-fetchable)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

This plugin is for CakePHP 3.x and contains behavior that handles fetching entities
This plugin is for CakePHP 4.x and contains behavior that handles fetching entities
from cache / memory storage. Relevant for tables that contain moderate number of rows
and are used commonly in many parts of application.

Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
}
],
"require": {
"php": ">=7.1",
"cakephp/orm": "^3.5"
"php": ">=8.0",
"cakephp/orm": "^4.0"
},
"require-dev": {
"cakephp/cakephp": "^3.5",
"friendsofphp/php-cs-fixer": "~2.0",
"phpstan/phpstan": "~0.11",
"phpunit/phpunit": "^5.7.14|^6.0",
"rshop/php-cs-fixer-config": "~2.0"
"cakephp/cakephp": "^4.0",
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^8.5|^9.3",
"rshop/php-cs-fixer-config": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 6
paths:
- src
bootstrapFiles:
- tests/bootstrap.php
ignoreErrors:
- '#Access to an undefined property Cake\\ORM\\Table::\$[a-zA-Z0-9_]+\.#'
25 changes: 9 additions & 16 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>
<php>
Expand All @@ -13,26 +12,20 @@

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Duplicatable Test Suite">
<testsuite name="Fetchable Test Suite">
<directory>./tests/TestCase</directory>
</testsuite>
</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>
<extensions>
<extension class="\Cake\TestSuite\Fixture\PHPUnitExtension"/>
</extensions>

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

</phpunit>
</phpunit>
14 changes: 2 additions & 12 deletions src/Model/Behavior/FetchableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Licensed under the MIT License
* (c) RIESENIA.com
*/

declare(strict_types=1);

namespace Fetchable\Model\Behavior;
Expand All @@ -18,7 +17,7 @@
class FetchableBehavior extends Behavior
{
/** @var array */
protected $_defaultConfig = [

Check failure on line 20 in src/Model/Behavior/FetchableBehavior.php

View workflow job for this annotation

GitHub Actions / Code Quality

Property Fetchable\Model\Behavior\FetchableBehavior::$_defaultConfig type has no value type specified in iterable type array.

Check failure on line 20 in src/Model/Behavior/FetchableBehavior.php

View workflow job for this annotation

GitHub Actions / Code Quality

PHPDoc type array of property Fetchable\Model\Behavior\FetchableBehavior::$_defaultConfig is not covariant with PHPDoc type array<string, mixed> of overridden property Cake\ORM\Behavior::$_defaultConfig.
'finder' => 'all',
'cache' => 'default',
'contain' => [],
Expand All @@ -26,12 +25,9 @@
];

/** @var array */
private $_cache;

Check failure on line 28 in src/Model/Behavior/FetchableBehavior.php

View workflow job for this annotation

GitHub Actions / Code Quality

Property Fetchable\Model\Behavior\FetchableBehavior::$_cache type has no value type specified in iterable type array.

/**
* {@inheritdoc}
*/
public function initialize(array $config)
public function initialize(array $config): void
{
// key is set automatically
if (!isset($config['key'])) {
Expand All @@ -43,10 +39,8 @@

/**
* Fetch data.
*
* @return array
*/
public function fetch(): array

Check failure on line 43 in src/Model/Behavior/FetchableBehavior.php

View workflow job for this annotation

GitHub Actions / Code Quality

Method Fetchable\Model\Behavior\FetchableBehavior::fetch() return type has no value type specified in iterable type array.
{
$key = $this->getConfig('key');

Expand All @@ -58,7 +52,7 @@
return $this->_cache[$key];
}

if (($data = Cache::read($key, $this->getConfig('cache'))) === false) {
if (!($data = Cache::read($key, $this->getConfig('cache')))) {
$data = [];

$find = $this->_table->find($this->getConfig('finder'));
Expand All @@ -83,8 +77,6 @@
* Fetch entity.
*
* @param int|string $id
*
* @return EntityInterface
*/
public function fetchEntity($id): EntityInterface
{
Expand All @@ -100,8 +92,6 @@
/**
* Get primary key value for entity.
*
* @param EntityInterface $entity
*
* @return int|string
*/
protected function _getPrimaryValue(EntityInterface $entity)
Expand Down
55 changes: 13 additions & 42 deletions tests/Fixture/I18nFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Licensed under the MIT License
* (c) RIESENIA.com
*/

declare(strict_types=1);

namespace Fetchable\Test\Fixture;
Expand All @@ -23,53 +22,25 @@ class I18nFixture extends TestFixture
* @var string
*/
public $table = 'i18n';
/**
* Fields.
*
* @var array
*/
// @codingStandardsIgnoreStart

public $fields = [
'id' => ['type' => 'integer', 'length' => 11, 'unsigned' => false, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null],
'locale' => ['type' => 'string', 'length' => 6, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null],
'model' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null],
'foreign_key' => ['type' => 'integer', 'length' => 10, 'unsigned' => false, 'null' => false, 'default' => null, 'comment' => '', 'precision' => null, 'autoIncrement' => null],
'field' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null],
'content' => ['type' => 'text', 'length' => null, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null],
'id' => ['type' => 'integer', 'null' => false, 'autoIncrement' => true],
'locale' => ['type' => 'string', 'length' => 6, 'null' => false],
'model' => ['type' => 'string', 'length' => 255, 'null' => false],
'foreign_key' => ['type' => 'integer', 'null' => false],
'field' => ['type' => 'string', 'length' => 255, 'null' => false],
'content' => ['type' => 'text', 'null' => true],
'_indexes' => [
'model' => ['type' => 'index', 'columns' => ['model', 'foreign_key', 'field'], 'length' => []],
'model' => ['type' => 'index', 'columns' => ['model', 'foreign_key', 'field']]
],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []],
'locale' => ['type' => 'unique', 'columns' => ['locale', 'model', 'foreign_key', 'field'], 'length' => []],
],
'_options' => [
'engine' => 'InnoDB',
'collation' => 'utf8_general_ci'
],
'primary' => ['type' => 'primary', 'columns' => ['id']],
'locale' => ['type' => 'unique', 'columns' => ['locale', 'model', 'foreign_key', 'field']]
]
];

/**
* Records.
*
* @var array
*/
public $records = [
[
'id' => 1,
'locale' => 'sk_SK',
'model' => 'Statuses',
'foreign_key' => 1,
'field' => 'name',
'content' => 'First status - sk'
],
[
'id' => 2,
'locale' => 'sk_SK',
'model' => 'StatusProperties',
'foreign_key' => 1,
'field' => 'name',
'content' => 'Property 1 - sk'
],
['id' => 1, 'locale' => 'sk_SK', 'model' => 'Statuses', 'foreign_key' => 1, 'field' => 'name', 'content' => 'First status - sk'],
['id' => 2, 'locale' => 'sk_SK', 'model' => 'StatusProperties', 'foreign_key' => 1, 'field' => 'name', 'content' => 'Property 1 - sk']
];
}
18 changes: 5 additions & 13 deletions tests/Fixture/StatusPropertiesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Licensed under the MIT License
* (c) RIESENIA.com
*/

declare(strict_types=1);

namespace Fetchable\Test\Fixture;
Expand All @@ -16,22 +15,15 @@ class StatusPropertiesFixture extends TestFixture
{
public $fields = [
'id' => ['type' => 'integer'],
'status_id' => ['type' => 'integer', 'default' => null, 'null' => true],
'name' => ['type' => 'string', 'default' => null, 'null' => true],
'status_id' => ['type' => 'integer', 'null' => true],
'name' => ['type' => 'string', 'length' => 255, 'null' => true],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
];

public $records = [
[
'id' => 1,
'status_id' => 1,
'name' => 'First property'
],
[
'id' => 2,
'status_id' => 1,
'name' => 'Second property'
]
['id' => 1, 'status_id' => 1, 'name' => 'First property'],
['id' => 2, 'status_id' => 1, 'name' => 'Second property']
];
}
14 changes: 4 additions & 10 deletions tests/Fixture/StatusesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Licensed under the MIT License
* (c) RIESENIA.com
*/

declare(strict_types=1);

namespace Fetchable\Test\Fixture;
Expand All @@ -16,19 +15,14 @@ class StatusesFixture extends TestFixture
{
public $fields = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'default' => null, 'null' => true],
'name' => ['type' => 'string', 'length' => 255, 'null' => true],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
];

public $records = [
[
'id' => 1,
'name' => 'First status'
],
[
'id' => 2,
'name' => 'Second status'
]
['id' => 1, 'name' => 'First status'],
['id' => 2, 'name' => 'Second status']
];
}
Loading
Loading