Skip to content

Commit ea912e9

Browse files
authored
Compatibility with Sonata 4.x (#30)
* 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
1 parent 5f0f6bd commit ea912e9

11 files changed

+91
-177
lines changed

.github/workflows/tests.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
pull_request: null
55
push:
66
branches:
7+
- "main"
78
- "0.*.x"
89

910
jobs:
@@ -14,14 +15,12 @@ jobs:
1415
strategy:
1516
matrix:
1617
include:
17-
- php-version: 7.3
18-
symfony-version: 4.4.*
19-
- php-version: 7.3
20-
symfony-version: 5.2.*
2118
- php-version: 7.4
22-
symfony-version: 5.2.*
19+
symfony-version: 4.4.*
20+
sonata-version: ^4.0
2321
- php-version: 8.0
24-
symfony-version: 5.2.*
22+
symfony-version: 5.3.*
23+
sonata-version: ^4.0
2524

2625
steps:
2726
- name: "Checkout"
@@ -36,6 +35,7 @@ jobs:
3635
- name: "Install dependencies with composer"
3736
run: |
3837
composer require --no-update symfony/workflow:${{ matrix.symfony-version }}
38+
composer require --no-update sonata-project/admin-bundle:${{ matrix.sonata-version }}
3939
composer update --no-interaction --no-progress --no-suggest
4040
4141
- name: "Run tests with phpunit/phpunit"
@@ -49,7 +49,7 @@ jobs:
4949
matrix:
5050
include:
5151
- php-version: 8.0
52-
symfony-version: 5.2.*
52+
symfony-version: 5.3.*
5353

5454
steps:
5555
- name: "Checkout"
@@ -76,7 +76,7 @@ jobs:
7676
matrix:
7777
include:
7878
- php-version: 8.0
79-
symfony-version: 5.2.*
79+
symfony-version: 5.3.*
8080

8181
steps:
8282
- name: "Checkout"

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
/build/
44
/vendor/
55
/.phpcs-cache
6+
/.phpunit.result.cache
67
/composer.lock
78
/docker-compose.yml

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class PullRequestController extends CRUDController
207207
{
208208
use WorkflowControllerTrait;
209209
210-
protected function preApplyTransition($object, string $transition): ?Response
210+
protected function preApplyTransition(object $object, string $transition): ?Response
211211
{
212212
switch ($transition) {
213213
case 'start_review':

composer.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
}
2626
},
2727
"require": {
28-
"php": "^7.3|^8.0",
29-
"sonata-project/admin-bundle": "^3.0",
28+
"php": "^7.4|^8.0",
29+
"sonata-project/admin-bundle": "^4.0",
3030
"symfony/workflow": "^4.4|^5.0"
3131
},
3232
"require-dev": {
33-
"phpunit/phpunit": "^8.5|^9.5",
34-
"squizlabs/php_codesniffer": "^3.5"
33+
"phpunit/phpunit": "^9.5",
34+
"squizlabs/php_codesniffer": "^3.5",
35+
"phpspec/prophecy-phpunit": "^2.0"
3536
}
3637
}

phpunit.xml.dist

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit backupGlobals="false"
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
5+
backupGlobals="false"
46
backupStaticAttributes="false"
57
convertErrorsToExceptions="true"
68
convertNoticesToExceptions="true"
79
convertWarningsToExceptions="true"
810
processIsolation="false"
911
stopOnFailure="false"
1012
colors="true"
11-
bootstrap="vendor/autoload.php"
12-
>
13+
bootstrap="vendor/autoload.php">
14+
<coverage>
15+
<include>
16+
<directory>./src</directory>
17+
</include>
18+
</coverage>
1319
<testsuites>
1420
<testsuite name="default">
1521
<directory>./tests</directory>
1622
</testsuite>
1723
</testsuites>
18-
19-
<filter>
20-
<whitelist>
21-
<directory>./src</directory>
22-
</whitelist>
23-
</filter>
2424
</phpunit>

src/Admin/Extension/WorkflowExtension.php

+20-70
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Knp\Menu\ItemInterface as MenuItemInterface;
88
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
99
use Sonata\AdminBundle\Admin\AdminInterface;
10-
use Sonata\AdminBundle\Route\RouteCollection;
10+
use Sonata\AdminBundle\Route\RouteCollectionInterface;
1111
use Symfony\Component\OptionsResolver\OptionsResolver;
1212
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1313
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
@@ -20,20 +20,9 @@
2020
*/
2121
class WorkflowExtension extends AbstractAdminExtension
2222
{
23-
/**
24-
* @var Registry
25-
*/
26-
private $registry;
23+
private Registry $registry;
24+
private array $options;
2725

28-
/**
29-
* @var array
30-
*/
31-
private $options;
32-
33-
/**
34-
* @param Registry $registry
35-
* @param array $options
36-
*/
3726
public function __construct(Registry $registry, array $options = [])
3827
{
3928
$this->registry = $registry;
@@ -44,7 +33,7 @@ public function __construct(Registry $registry, array $options = [])
4433
/**
4534
* @inheritdoc
4635
*/
47-
public function configureRoutes(AdminInterface $admin, RouteCollection $collection): void
36+
public function configureRoutes(AdminInterface $admin, RouteCollectionInterface $collection): void
4837
{
4938
$collection->add(
5039
'workflow_apply_transition',
@@ -69,18 +58,23 @@ public function alterNewInstance(AdminInterface $admin, $object): void
6958
/**
7059
* @inheritdoc
7160
*/
72-
public function configureSideMenu(
61+
public function configureTabMenu(
7362
AdminInterface $admin,
7463
MenuItemInterface $menu,
7564
$action,
76-
AdminInterface $childAdmin = null
65+
?AdminInterface $childAdmin = null
7766
): void {
7867
if (null !== $childAdmin || !in_array($action, $this->options['render_actions'], true)) {
7968
return;
8069
}
8170

82-
$subject = $admin->getSubject();
83-
if (null === $subject || !$this->isGrantedView($admin, $subject)) {
71+
try {
72+
$subject = $admin->getSubject();
73+
} catch (\LogicException $exception) {
74+
return;
75+
}
76+
77+
if (!$this->isGrantedView($admin, $subject)) {
8478
return;
8579
}
8680

@@ -111,20 +105,13 @@ public function getAccessMapping(AdminInterface $admin): array
111105
}
112106

113107
/**
114-
* @param object $subject
115-
* @param string|null $workflowName
116-
*
117-
* @return Workflow
118108
* @throws InvalidArgumentException
119109
*/
120-
protected function getWorkflow($subject, string $workflowName = null): Workflow
110+
protected function getWorkflow(object $subject, string $workflowName = null): Workflow
121111
{
122112
return $this->registry->get($subject, $workflowName);
123113
}
124114

125-
/**
126-
* @param OptionsResolver $resolver
127-
*/
128115
protected function configureOptions(OptionsResolver $resolver): void
129116
{
130117
$resolver
@@ -155,10 +142,6 @@ protected function configureOptions(OptionsResolver $resolver): void
155142
;
156143
}
157144

158-
/**
159-
* @param MenuItemInterface $menu
160-
* @param AdminInterface $admin
161-
*/
162145
protected function noTransitions(MenuItemInterface $menu, AdminInterface $admin): void
163146
{
164147
if ($this->options['no_transition_display']) {
@@ -175,16 +158,13 @@ protected function noTransitions(MenuItemInterface $menu, AdminInterface $admin)
175158
}
176159

177160
/**
178-
* @param MenuItemInterface $menu
179-
* @param AdminInterface $admin
180-
* @param iterable|Transition[] $transitions
181-
* @param object $subject
161+
* @param iterable&Transition[] $transitions
182162
*/
183163
protected function transitionsDropdown(
184164
MenuItemInterface $menu,
185165
AdminInterface $admin,
186166
iterable $transitions,
187-
$subject
167+
object $subject
188168
): void {
189169
$workflowMenu = $menu->addChild($this->options['dropdown_transitions_label'], [
190170
'attributes' => [
@@ -201,17 +181,11 @@ protected function transitionsDropdown(
201181
}
202182
}
203183

204-
/**
205-
* @param MenuItemInterface $menu
206-
* @param AdminInterface $admin
207-
* @param Transition $transition
208-
* @param object $subject
209-
*/
210184
protected function transitionsItem(
211185
MenuItemInterface $menu,
212186
AdminInterface $admin,
213187
Transition $transition,
214-
$subject
188+
object $subject
215189
): void {
216190
$options = [
217191
'attributes' => [],
@@ -234,11 +208,6 @@ protected function transitionsItem(
234208
);
235209
}
236210

237-
/**
238-
* @param Transition $transition
239-
*
240-
* @return string|null
241-
*/
242211
protected function getTransitionIcon(Transition $transition): ?string
243212
{
244213
if (isset($this->options['transitions_icons'][$transition->getName()])) {
@@ -248,14 +217,7 @@ protected function getTransitionIcon(Transition $transition): ?string
248217
return $this->options['transitions_default_icon'];
249218
}
250219

251-
/**
252-
* @param AdminInterface $admin
253-
* @param Transition $transition
254-
* @param object $subject
255-
*
256-
* @return string
257-
*/
258-
protected function generateTransitionUri(AdminInterface $admin, Transition $transition, $subject): string
220+
protected function generateTransitionUri(AdminInterface $admin, Transition $transition, object $subject): string
259221
{
260222
return $admin->generateObjectUrl(
261223
'workflow_apply_transition',
@@ -264,13 +226,7 @@ protected function generateTransitionUri(AdminInterface $admin, Transition $tran
264226
);
265227
}
266228

267-
/**
268-
* @param AdminInterface $admin
269-
* @param object $subject
270-
*
271-
* @return bool
272-
*/
273-
protected function isGrantedView(AdminInterface $admin, $subject): bool
229+
protected function isGrantedView(AdminInterface $admin, object $subject): bool
274230
{
275231
try {
276232
$admin->checkAccess('viewTransitions', $subject);
@@ -281,13 +237,7 @@ protected function isGrantedView(AdminInterface $admin, $subject): bool
281237
return true;
282238
}
283239

284-
/**
285-
* @param AdminInterface $admin
286-
* @param object $subject
287-
*
288-
* @return bool
289-
*/
290-
protected function isGrantedApply(AdminInterface $admin, $subject): bool
240+
protected function isGrantedApply(AdminInterface $admin, object $subject): bool
291241
{
292242
try {
293243
$admin->checkAccess('applyTransitions', $subject);

0 commit comments

Comments
 (0)