Skip to content

Commit

Permalink
Compatibility with Sonata 4.x (#30)
Browse files Browse the repository at this point in the history
* Allow sonata ^4.0

* Introduced WorkflowExtension::configureTabMenu as an alias of WorkflowExtension::configureSideMenu

* Added missing sonata.admin.pool.do-not-use service to controller container

* Add Github action tests with sonata 4.x-dev@dev

* Bump packages versions

* Adapt tests & fix sources for Sonata 4.x

* Updated PHPUnit configuration structure

* Use PHP 7.4 features

* Fixed missing session service for Symfony 4.4 tests

* Force PHPUnit 9.x
  • Loading branch information
yann-eugone authored Oct 18, 2021
1 parent 5f0f6bd commit ea912e9
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 177 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request: null
push:
branches:
- "main"
- "0.*.x"

jobs:
Expand All @@ -14,14 +15,12 @@ jobs:
strategy:
matrix:
include:
- php-version: 7.3
symfony-version: 4.4.*
- php-version: 7.3
symfony-version: 5.2.*
- php-version: 7.4
symfony-version: 5.2.*
symfony-version: 4.4.*
sonata-version: ^4.0
- php-version: 8.0
symfony-version: 5.2.*
symfony-version: 5.3.*
sonata-version: ^4.0

steps:
- name: "Checkout"
Expand All @@ -36,6 +35,7 @@ jobs:
- name: "Install dependencies with composer"
run: |
composer require --no-update symfony/workflow:${{ matrix.symfony-version }}
composer require --no-update sonata-project/admin-bundle:${{ matrix.sonata-version }}
composer update --no-interaction --no-progress --no-suggest
- name: "Run tests with phpunit/phpunit"
Expand All @@ -49,7 +49,7 @@ jobs:
matrix:
include:
- php-version: 8.0
symfony-version: 5.2.*
symfony-version: 5.3.*

steps:
- name: "Checkout"
Expand All @@ -76,7 +76,7 @@ jobs:
matrix:
include:
- php-version: 8.0
symfony-version: 5.2.*
symfony-version: 5.3.*

steps:
- name: "Checkout"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/build/
/vendor/
/.phpcs-cache
/.phpunit.result.cache
/composer.lock
/docker-compose.yml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class PullRequestController extends CRUDController
{
use WorkflowControllerTrait;
protected function preApplyTransition($object, string $transition): ?Response
protected function preApplyTransition(object $object, string $transition): ?Response
{
switch ($transition) {
case 'start_review':
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
}
},
"require": {
"php": "^7.3|^8.0",
"sonata-project/admin-bundle": "^3.0",
"php": "^7.4|^8.0",
"sonata-project/admin-bundle": "^4.0",
"symfony/workflow": "^4.4|^5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.5",
"squizlabs/php_codesniffer": "^3.5"
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5",
"phpspec/prophecy-phpunit": "^2.0"
}
}
18 changes: 9 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
colors="true"
bootstrap="vendor/autoload.php"
>
bootstrap="vendor/autoload.php">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="default">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
90 changes: 20 additions & 70 deletions src/Admin/Extension/WorkflowExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Knp\Menu\ItemInterface as MenuItemInterface;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
Expand All @@ -20,20 +20,9 @@
*/
class WorkflowExtension extends AbstractAdminExtension
{
/**
* @var Registry
*/
private $registry;
private Registry $registry;
private array $options;

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

/**
* @param Registry $registry
* @param array $options
*/
public function __construct(Registry $registry, array $options = [])
{
$this->registry = $registry;
Expand All @@ -44,7 +33,7 @@ public function __construct(Registry $registry, array $options = [])
/**
* @inheritdoc
*/
public function configureRoutes(AdminInterface $admin, RouteCollection $collection): void
public function configureRoutes(AdminInterface $admin, RouteCollectionInterface $collection): void
{
$collection->add(
'workflow_apply_transition',
Expand All @@ -69,18 +58,23 @@ public function alterNewInstance(AdminInterface $admin, $object): void
/**
* @inheritdoc
*/
public function configureSideMenu(
public function configureTabMenu(
AdminInterface $admin,
MenuItemInterface $menu,
$action,
AdminInterface $childAdmin = null
?AdminInterface $childAdmin = null
): void {
if (null !== $childAdmin || !in_array($action, $this->options['render_actions'], true)) {
return;
}

$subject = $admin->getSubject();
if (null === $subject || !$this->isGrantedView($admin, $subject)) {
try {
$subject = $admin->getSubject();
} catch (\LogicException $exception) {
return;
}

if (!$this->isGrantedView($admin, $subject)) {
return;
}

Expand Down Expand Up @@ -111,20 +105,13 @@ public function getAccessMapping(AdminInterface $admin): array
}

/**
* @param object $subject
* @param string|null $workflowName
*
* @return Workflow
* @throws InvalidArgumentException
*/
protected function getWorkflow($subject, string $workflowName = null): Workflow
protected function getWorkflow(object $subject, string $workflowName = null): Workflow
{
return $this->registry->get($subject, $workflowName);
}

/**
* @param OptionsResolver $resolver
*/
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver
Expand Down Expand Up @@ -155,10 +142,6 @@ protected function configureOptions(OptionsResolver $resolver): void
;
}

/**
* @param MenuItemInterface $menu
* @param AdminInterface $admin
*/
protected function noTransitions(MenuItemInterface $menu, AdminInterface $admin): void
{
if ($this->options['no_transition_display']) {
Expand All @@ -175,16 +158,13 @@ protected function noTransitions(MenuItemInterface $menu, AdminInterface $admin)
}

/**
* @param MenuItemInterface $menu
* @param AdminInterface $admin
* @param iterable|Transition[] $transitions
* @param object $subject
* @param iterable&Transition[] $transitions
*/
protected function transitionsDropdown(
MenuItemInterface $menu,
AdminInterface $admin,
iterable $transitions,
$subject
object $subject
): void {
$workflowMenu = $menu->addChild($this->options['dropdown_transitions_label'], [
'attributes' => [
Expand All @@ -201,17 +181,11 @@ protected function transitionsDropdown(
}
}

/**
* @param MenuItemInterface $menu
* @param AdminInterface $admin
* @param Transition $transition
* @param object $subject
*/
protected function transitionsItem(
MenuItemInterface $menu,
AdminInterface $admin,
Transition $transition,
$subject
object $subject
): void {
$options = [
'attributes' => [],
Expand All @@ -234,11 +208,6 @@ protected function transitionsItem(
);
}

/**
* @param Transition $transition
*
* @return string|null
*/
protected function getTransitionIcon(Transition $transition): ?string
{
if (isset($this->options['transitions_icons'][$transition->getName()])) {
Expand All @@ -248,14 +217,7 @@ protected function getTransitionIcon(Transition $transition): ?string
return $this->options['transitions_default_icon'];
}

/**
* @param AdminInterface $admin
* @param Transition $transition
* @param object $subject
*
* @return string
*/
protected function generateTransitionUri(AdminInterface $admin, Transition $transition, $subject): string
protected function generateTransitionUri(AdminInterface $admin, Transition $transition, object $subject): string
{
return $admin->generateObjectUrl(
'workflow_apply_transition',
Expand All @@ -264,13 +226,7 @@ protected function generateTransitionUri(AdminInterface $admin, Transition $tran
);
}

/**
* @param AdminInterface $admin
* @param object $subject
*
* @return bool
*/
protected function isGrantedView(AdminInterface $admin, $subject): bool
protected function isGrantedView(AdminInterface $admin, object $subject): bool
{
try {
$admin->checkAccess('viewTransitions', $subject);
Expand All @@ -281,13 +237,7 @@ protected function isGrantedView(AdminInterface $admin, $subject): bool
return true;
}

/**
* @param AdminInterface $admin
* @param object $subject
*
* @return bool
*/
protected function isGrantedApply(AdminInterface $admin, $subject): bool
protected function isGrantedApply(AdminInterface $admin, object $subject): bool
{
try {
$admin->checkAccess('applyTransitions', $subject);
Expand Down
Loading

0 comments on commit ea912e9

Please sign in to comment.