Skip to content

Commit

Permalink
Merge pull request #5 from didoda/feat/bedita-5
Browse files Browse the repository at this point in the history
BEdita 5
  • Loading branch information
batopa committed Dec 15, 2023
2 parents 338c360 + 4a69a0a commit 4323347
Show file tree
Hide file tree
Showing 25 changed files with 196 additions and 117 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:
cs:
uses: bedita/github-workflows/.github/workflows/php-cs.yml@v1
with:
php_versions: '["7.4"]'
php_versions: '["7.4", "8.1", "8.2"]'

stan:
uses: bedita/github-workflows/.github/workflows/php-stan.yml@v1
with:
php_versions: '["7.4"]'
php_versions: '["7.4", "8.1", "8.2"]'

unit:
name: 'Run unit tests'
Expand All @@ -30,7 +30,7 @@ jobs:

strategy:
matrix:
php-version: [7.4]
php-version: [7.4, 8.1, 8.2]

steps:
- name: 'Checkout current revision'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ jobs:
uses: bedita/github-workflows/.github/workflows/release.yml@v1
with:
main_branch: 'main'
dist_branches: '["main"]'
dist_branches: '["main","1.x"]'
version_bump: ${{ inputs.releaseType }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/phpunit.xml
/vendor
config/Migrations/schema-dump-default.lock
.phpunit.result.cache
4 changes: 2 additions & 2 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ filter:
- 'vendor/*'

build:
image: default-bionic
image: default-jammy
nodes:
analysis:
environment:
php:
version: 7.4
version: 8.2
pecl_extensions:
- zip
tests:
Expand Down
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
"license": "LGPL-3.0-or-later",
"require": {
"php": ">= 7.4",
"cakephp/cakephp": "^3.8",
"bedita/core": "^4.7",
"bedita/api": "^4.7"
"cakephp/cakephp": "^4.4",
"bedita/core": "^5.14",
"bedita/api": "^5.14"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^3.0",
"cakephp/cakephp-codesniffer": "^4.5",
"cakephp/plugin-installer": "^1.3",
"phpunit/phpunit": "^6.0",
"phpstan/phpstan": "^1.5"
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.7",
"league/flysystem-memory": "^2.0.0"
},
"autoload": {
"psr-4": {
Expand All @@ -35,7 +36,8 @@
},
"config": {
"allow-plugins": {
"cakephp/plugin-installer": true
"cakephp/plugin-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ parameters:
- tests
level: 8
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
34 changes: 13 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,25 @@
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
>
bootstrap="./tests/bootstrap.php"
forceCoversAnnotation="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<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="BEdita/Placeholders">
<directory>tests/TestCase/</directory>
<directory>./tests/TestCase</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>

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

<extensions>
<extension class="\Cake\TestSuite\Fixture\PHPUnitExtension" />
</extensions>
<coverage>
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
</phpunit>
17 changes: 14 additions & 3 deletions src/Controller/Component/PlaceholdersComponent.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2022 Atlas Srl, Chialab Srl
Expand All @@ -24,7 +26,7 @@ class PlaceholdersComponent extends Component
/**
* Default configuration.
*
* @var array
* @var array<string, mixed>
*/
protected $_defaultConfig = [
'relations' => ['placeholder', 'placeholded'],
Expand All @@ -38,11 +40,20 @@ class PlaceholdersComponent extends Component
public function beforeFilter(): void
{
$request = $this->getController()->getRequest();
if ($request->getParam('action') !== 'relationships' || !in_array($request->getParam('relationship'), (array)$this->getConfig('relations'))) {
if (
$request->getParam('action') !== 'relationships' ||
!in_array($request->getParam('relationship'), (array)$this->getConfig('relations'))
) {
return;
}
if (!in_array(mb_strtoupper($request->getMethod()), ['GET', 'HEAD', 'OPTIONS'])) {
throw new ForbiddenException(__d('placeholders', 'Relationships of type {0} can only be managed saving an object', $request->getParam('relationship')));
throw new ForbiddenException(
__d(
'placeholders',
'Relationships of type {0} can only be managed saving an object',
$request->getParam('relationship')
)
);
}
}
}
4 changes: 3 additions & 1 deletion src/Event/BootstrapEventHandler.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2022 Atlas Srl, Chialab Srl
Expand All @@ -25,7 +27,7 @@
class BootstrapEventHandler implements EventListenerInterface
{
/**
* @inheritDoc
* {@inheritDoc}
*
* @codeCoverageIgnore
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Event/JsonSchemaEventHandler.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2022 Atlas Srl, Chialab Srl
Expand Down Expand Up @@ -28,7 +30,7 @@ class JsonSchemaEventHandler implements EventListenerInterface
/**
* @inheritDoc
*/
public function implementedEvents()
public function implementedEvents(): array
{
return [
'ObjectType.getSchema' => 'onGetSchema',
Expand Down
6 changes: 4 additions & 2 deletions src/Model/Behavior/GetAssociationTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2022 Atlas Srl, Chialab Srl
Expand Down Expand Up @@ -26,7 +28,7 @@ trait GetAssociationTrait
*
* @return \Cake\ORM\Table
*/
abstract public function getTable();
abstract public function table();

/**
* Get association for a relation.
Expand All @@ -37,7 +39,7 @@ abstract public function getTable();
protected function getAssociation(string $relation): ?Association
{
$name = Inflector::camelize($relation);
$table = $this->getTable();
$table = $this->table();
if (!$table->hasAssociation($name)) {
return null;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Model/Behavior/PlaceholdedBehavior.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2022 Atlas Srl, Chialab Srl
Expand All @@ -17,6 +19,7 @@
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\Utility\Hash;

/**
* Placeholded behavior
Expand All @@ -30,7 +33,7 @@ class PlaceholdedBehavior extends Behavior
*
* - `relations`: names of the BEdita relations to check.
*
* @var array
* @var array<string, mixed>
*/
protected $_defaultConfig = [
'relations' => ['placeholded'],
Expand All @@ -57,12 +60,11 @@ public function beforeSave(Event $event, EntityInterface $entity): void
*
* @param \Cake\Datasource\EntityInterface $entity Entity being checked.
* @return void
*
* @throws \BEdita\Core\Exception\LockedResourceException
*/
protected function ensureNotPlaceholded(EntityInterface $entity): void
{
$Table = $this->getTable();
$Table = $this->table();

$relations = $this->getConfig('relations', []);
foreach ($relations as $relation) {
Expand All @@ -83,7 +85,7 @@ protected function ensureNotPlaceholded(EntityInterface $entity): void
throw new LockedResourceException(__d(
'placeholders',
'Cannot delete object {0} because it is still {1} in {2,plural,=1{one object} other{# objects}}',
$entity->id,
(string)Hash::get($entity, 'id'),
$relation,
$refCount
));
Expand Down
13 changes: 9 additions & 4 deletions src/Model/Behavior/PlaceholdersBehavior.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2022 Atlas Srl, Chialab Srl
Expand All @@ -13,7 +15,6 @@

namespace BEdita\Placeholders\Model\Behavior;

use BEdita\Core\Exception\LockedResourceException;
use BEdita\Core\Model\Action\SetRelatedObjectsAction;
use Cake\Database\Expression\QueryExpression;
use Cake\Datasource\EntityInterface;
Expand Down Expand Up @@ -48,7 +49,7 @@ class PlaceholdersBehavior extends Behavior
* a list of associative arrays with `id` and `params` fields.
* If `null`, uses {@see \BEdita\Core\Model\Behavior\PlaceholdersBehavior::extractPlaceholders()}.
*
* @var array
* @var array<string, mixed>
*/
protected $_defaultConfig = [
'relation' => 'placeholder',
Expand All @@ -59,7 +60,7 @@ class PlaceholdersBehavior extends Behavior
/**
* Extract placeholders from an entity.
*
* @param EntityInterface $entity The entity from which to extract placeholder references.
* @param \Cake\Datasource\EntityInterface $entity The entity from which to extract placeholder references.
* @param string[] $fields Field names.
* @return array[] A list of arrays, each with `id` and `params` set.
*/
Expand All @@ -72,7 +73,10 @@ public static function extractPlaceholders(EntityInterface $entity, array $field
continue;
}

if (!is_string($datum) || preg_match_all(static::REGEX, $datum, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE) === false) {
if (
!is_string($datum) ||
preg_match_all(static::REGEX, $datum, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE) === false
) {
throw new RuntimeException(__d('bedita', 'Error extracting placeholders'));
}

Expand Down Expand Up @@ -161,6 +165,7 @@ protected function prepareEntities(Table $table, array $placeholders): array
->where(function (QueryExpression $exp) use ($table, $pk, $ids): QueryExpression {
return $exp->in($table->aliasField($pk), $ids);
})
->all()
->map(function (EntityInterface $entity) use ($pk, $placeholders): EntityInterface {
$id = $entity->get($pk);
foreach ($placeholders as $datum) {
Expand Down
4 changes: 3 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2022 Atlas Srl, Chialab Srl
Expand Down Expand Up @@ -29,7 +31,7 @@ class Plugin extends BasePlugin
*
* @codeCoverageIgnore
*/
public function bootstrap(PluginApplicationInterface $app)
public function bootstrap(PluginApplicationInterface $app): void
{
parent::bootstrap($app);

Expand Down
3 changes: 2 additions & 1 deletion tests/Fixture/ObjectRelationsFixture.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
declare(strict_types=1);

namespace BEdita\Placeholders\Test\Fixture;

use BEdita\Core\TestSuite\Fixture\TestFixture;
use Cake\TestSuite\Fixture\TestFixture;

/**
* Fixture for `object_relations` table.
Expand Down
3 changes: 2 additions & 1 deletion tests/Fixture/RelationTypesFixture.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
declare(strict_types=1);

namespace BEdita\Placeholders\Test\Fixture;

use BEdita\Core\TestSuite\Fixture\TestFixture;
use Cake\TestSuite\Fixture\TestFixture;

/**
* Fixture for `relation_types` table.
Expand Down
3 changes: 2 additions & 1 deletion tests/Fixture/RelationsFixture.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
declare(strict_types=1);

namespace BEdita\Placeholders\Test\Fixture;

use BEdita\Core\TestSuite\Fixture\TestFixture;
use Cake\TestSuite\Fixture\TestFixture;

/**
* Fixture for `relations` table.
Expand Down
Loading

0 comments on commit 4323347

Please sign in to comment.