Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f0be16

Browse files
author
Amrouche Hamza
committed
[Workflow] Introduce a Workflow interface
1 parent bf4b09f commit 1f0be16

15 files changed

+316
-49
lines changed

UPGRADE-4.1.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ Translation
1616

1717
* The `FileDumper::setBackup()` method is deprecated and will be removed in 5.0.
1818
* The `TranslationWriter::disableBackup()` method is deprecated and will be removed in 5.0.
19+
20+
Workflow
21+
--------
22+
23+
* Deprecated the `add` method in favor of the `addWorkflow` method in `Workflow\Registry`
24+
* Deprecated `SupportStrategyInterface` in favor of `WorkflowSupportStrategyInterface`
25+
* Deprecated the class `ClassInstanceSupportStrategy` in favor of the class `InstanceOfSupportStrategy`

UPGRADE-5.0.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ Translation
1616

1717
* The `FileDumper::setBackup()` method has been removed.
1818
* The `TranslationWriter::disableBackup()` method has been removed.
19+
20+
Workflow
21+
--------
22+
23+
* `add` method has been removed use `addWorkflow` method in `Workflow\Registry` instead
24+
* `SupportStrategyInterface` has been removed, use `WorkflowSupportStrategyInterface` instead
25+
* `ClassInstanceSupportStrategy` has been removed, use `InstanceOfSupportStrategy` instead

src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Bridge\Twig\Extension\WorkflowExtension;
1616
use Symfony\Component\Workflow\Definition;
1717
use Symfony\Component\Workflow\Registry;
18-
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
18+
use Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy;
1919
use Symfony\Component\Workflow\Transition;
2020
use Symfony\Component\Workflow\Workflow;
2121

@@ -38,11 +38,48 @@ protected function setUp()
3838
$workflow = new Workflow($definition);
3939

4040
$registry = new Registry();
41-
$registry->add($workflow, new ClassInstanceSupportStrategy(\stdClass::class));
41+
$registry->addWorkflow($workflow, new InstanceOfSupportStrategy(\stdClass::class));
4242

4343
$this->extension = new WorkflowExtension($registry);
4444
}
4545

46+
/**
47+
* @group legacy
48+
*/
49+
protected function setUpLegacyAdd()
50+
{
51+
if (!class_exists(Workflow::class)) {
52+
$this->markTestSkipped('The Workflow component is needed to run tests for this extension.');
53+
}
54+
55+
$places = array('ordered', 'waiting_for_payment', 'processed');
56+
$transitions = array(
57+
new Transition('t1', 'ordered', 'waiting_for_payment'),
58+
new Transition('t2', 'waiting_for_payment', 'processed'),
59+
);
60+
$definition = new Definition($places, $transitions);
61+
$workflow = new Workflow($definition);
62+
63+
$registry = new Registry();
64+
$registry->add($workflow, new InstanceOfSupportStrategy(\stdClass::class));
65+
66+
$this->extension = new WorkflowExtension($registry);
67+
}
68+
69+
/**
70+
* @group legacy
71+
* @expectedDeprecation Symfony\Component\Workflow\Registry::add is deprecated since Symfony 4.1. Use addWorkflow() instead.
72+
*/
73+
public function testCanTransitionLegacy()
74+
{
75+
$this->setUpLegacyAdd();
76+
$subject = new \stdClass();
77+
$subject->marking = array();
78+
79+
$this->assertTrue($this->extension->canTransition($subject, 't1'));
80+
$this->assertFalse($this->extension->canTransition($subject, 't2'));
81+
}
82+
4683
public function testCanTransition()
4784
{
4885
$subject = new \stdClass();

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
498498
// Add workflow to Registry
499499
if ($workflow['supports']) {
500500
foreach ($workflow['supports'] as $supportedClassName) {
501-
$strategyDefinition = new Definition(Workflow\SupportStrategy\ClassInstanceSupportStrategy::class, array($supportedClassName));
501+
$strategyDefinition = new Definition(Workflow\SupportStrategy\InstanceOfSupportStrategy::class, array($supportedClassName));
502502
$strategyDefinition->setPublic(false);
503503
$registryDefinition->addMethodCall('add', array(new Reference($workflowId), $strategyDefinition));
504504
}

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* Deprecate the usage of `add(Workflow $workflow, $supportStrategy)` in `Workflow/Registry`, use `addWorkflow(WorkflowInterface, $supportStrategy)` instead
8+
* Deprecate the usage of `SupportStrategyInterface`, use `WorkflowSupportStrategyInterface` instead
9+
* The `Workflow` class now implements `WorkflowInterface`
10+
* Deprecated the class `ClassInstanceSupportStrategy` in favor of the class `InstanceOfSupportStrategy`
11+
412
4.0.0
513
-----
614

src/Symfony/Component/Workflow/Registry.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
1515
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface;
16+
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
1617

1718
/**
1819
* @author Fabien Potencier <[email protected]>
@@ -25,13 +26,17 @@ class Registry
2526
/**
2627
* @param Workflow $workflow
2728
* @param SupportStrategyInterface $supportStrategy
29+
*
30+
* @deprecated since Symfony 4.1. Use addWorkflow() instead.
2831
*/
2932
public function add(Workflow $workflow, $supportStrategy)
3033
{
31-
if (!$supportStrategy instanceof SupportStrategyInterface) {
32-
throw new \InvalidArgumentException('The "supportStrategy" is not an instance of SupportStrategyInterface.');
33-
}
34+
@trigger_error(sprintf('%s is deprecated since Symfony 4.1. Use addWorkflow() instead.', __METHOD__), E_USER_DEPRECATED);
35+
$this->workflows[] = array($workflow, $supportStrategy);
36+
}
3437

38+
public function addWorkflow(WorkflowInterface $workflow, WorkflowSupportStrategyInterface $supportStrategy)
39+
{
3540
$this->workflows[] = array($workflow, $supportStrategy);
3641
}
3742

@@ -61,7 +66,7 @@ public function get($subject, $workflowName = null)
6166
return $matched;
6267
}
6368

64-
private function supports(Workflow $workflow, SupportStrategyInterface $supportStrategy, $subject, $workflowName): bool
69+
private function supports(WorkflowInterface $workflow, $supportStrategy, $subject, $workflowName): bool
6570
{
6671
if (null !== $workflowName && $workflowName !== $workflow->getName()) {
6772
return false;

src/Symfony/Component/Workflow/SupportStrategy/ClassInstanceSupportStrategy.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\Workflow\SupportStrategy;
413

14+
@trigger_error(sprintf('"%s" is deprecated since Symfony 4.1. Use "%s" instead.', ClassInstanceSupportStrategy::class, InstanceOfSupportStrategy::class), E_USER_DEPRECATED);
15+
516
use Symfony\Component\Workflow\Workflow;
617

718
/**
819
* @author Andreas Kleemann <[email protected]>
20+
*
21+
* @deprecated since version 4.1, to be removed in 5.0. Use InstanceOfSupportStrategy instead
922
*/
1023
final class ClassInstanceSupportStrategy implements SupportStrategyInterface
1124
{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\SupportStrategy;
13+
14+
use Symfony\Component\Workflow\WorkflowInterface;
15+
16+
/**
17+
* @author Andreas Kleemann <[email protected]>
18+
* @author Amrouche Hamza <[email protected]>
19+
*/
20+
final class InstanceOfSupportStrategy implements WorkflowSupportStrategyInterface
21+
{
22+
private $className;
23+
24+
public function __construct(string $className)
25+
{
26+
$this->className = $className;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function supports(WorkflowInterface $workflow, $subject): bool
33+
{
34+
return $subject instanceof $this->className;
35+
}
36+
37+
public function getClassName(): string
38+
{
39+
return $this->className;
40+
}
41+
}

src/Symfony/Component/Workflow/SupportStrategy/SupportStrategyInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
/**
1717
* @author Andreas Kleemann <[email protected]>
18+
*
19+
* @deprecated since version 4.1, to be removed in 5.0. Use WorkflowSupportStrategyInterface instead
1820
*/
1921
interface SupportStrategyInterface
2022
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\SupportStrategy;
13+
14+
use Symfony\Component\Workflow\WorkflowInterface;
15+
16+
/**
17+
* @author Amrouche Hamza <[email protected]>
18+
*/
19+
interface WorkflowSupportStrategyInterface
20+
{
21+
/**
22+
* @param WorkflowInterface $workflow
23+
* @param object $subject
24+
*
25+
* @return bool
26+
*/
27+
public function supports(WorkflowInterface $workflow, $subject): bool;
28+
}

src/Symfony/Component/Workflow/Tests/RegistryTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
99
use Symfony\Component\Workflow\Registry;
1010
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface;
11+
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
1112
use Symfony\Component\Workflow\Workflow;
1213

1314
class RegistryTest extends TestCase
@@ -18,16 +19,25 @@ protected function setUp()
1819
{
1920
$this->registry = new Registry();
2021

21-
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createSupportStrategy(Subject1::class));
22-
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), $this->createSupportStrategy(Subject2::class));
23-
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), $this->createSupportStrategy(Subject2::class));
22+
$this->registry->addWorkflow(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createWorkflowSupportStrategy(Subject1::class));
23+
$this->registry->addWorkflow(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), $this->createWorkflowSupportStrategy(Subject2::class));
24+
$this->registry->addWorkflow(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), $this->createWorkflowSupportStrategy(Subject2::class));
2425
}
2526

2627
protected function tearDown()
2728
{
2829
$this->registry = null;
2930
}
3031

32+
/**
33+
* @group legacy
34+
* @expectedDeprecation Symfony\Component\Workflow\Registry::add is deprecated since Symfony 4.1. Use addWorkflow() instead.
35+
*/
36+
public function testAddIsDeprecated()
37+
{
38+
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createSupportStrategy(Subject1::class));
39+
}
40+
3141
public function testGetWithSuccess()
3242
{
3343
$workflow = $this->registry->get(new Subject1());
@@ -65,6 +75,9 @@ public function testGetWithNoMatch()
6575
$this->assertSame('workflow1', $w1->getName());
6676
}
6777

78+
/**
79+
* @group legacy
80+
*/
6881
private function createSupportStrategy($supportedClassName)
6982
{
7083
$strategy = $this->getMockBuilder(SupportStrategyInterface::class)->getMock();
@@ -75,6 +88,20 @@ private function createSupportStrategy($supportedClassName)
7588

7689
return $strategy;
7790
}
91+
92+
/**
93+
* @group legacy
94+
*/
95+
private function createWorkflowSupportStrategy($supportedClassName)
96+
{
97+
$strategy = $this->getMockBuilder(WorkflowSupportStrategyInterface::class)->getMock();
98+
$strategy->expects($this->any())->method('supports')
99+
->will($this->returnCallback(function ($workflow, $subject) use ($supportedClassName) {
100+
return $subject instanceof $supportedClassName;
101+
}));
102+
103+
return $strategy;
104+
}
78105
}
79106

80107
class Subject1

src/Symfony/Component/Workflow/Tests/SupportStrategy/ClassInstanceSupportStrategyTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
77
use Symfony\Component\Workflow\Workflow;
88

9+
/**
10+
* @group legacy
11+
*/
912
class ClassInstanceSupportStrategyTest extends TestCase
1013
{
14+
/**
15+
* @expectedDeprecation "Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy" is deprecated since Symfony 4.1. Use "Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy" instead.
16+
*/
1117
public function testSupportsIfClassInstance()
1218
{
13-
$strategy = new ClassInstanceSupportStrategy('Symfony\Component\Workflow\Tests\SupportStrategy\Subject1');
19+
$strategy = new ClassInstanceSupportStrategy(Subject1::class);
1420

1521
$this->assertTrue($strategy->supports($this->createWorkflow(), new Subject1()));
1622
}
1723

1824
public function testSupportsIfNotClassInstance()
1925
{
20-
$strategy = new ClassInstanceSupportStrategy('Symfony\Component\Workflow\Tests\SupportStrategy\Subject2');
26+
$strategy = new ClassInstanceSupportStrategy(Subject2::class);
2127

2228
$this->assertFalse($strategy->supports($this->createWorkflow(), new Subject1()));
2329
}
@@ -29,10 +35,3 @@ private function createWorkflow()
2935
->getMock();
3036
}
3137
}
32-
33-
class Subject1
34-
{
35-
}
36-
class Subject2
37-
{
38-
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Symfony\Component\Workflow\Tests\SupportStrategy;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy;
7+
use Symfony\Component\Workflow\Workflow;
8+
9+
class InstanceOfSupportStrategyTest extends TestCase
10+
{
11+
public function testSupportsIfClassInstance()
12+
{
13+
$strategy = new InstanceOfSupportStrategy(Subject1::class);
14+
15+
$this->assertTrue($strategy->supports($this->createWorkflow(), new Subject1()));
16+
}
17+
18+
public function testSupportsIfNotClassInstance()
19+
{
20+
$strategy = new InstanceOfSupportStrategy(Subject2::class);
21+
22+
$this->assertFalse($strategy->supports($this->createWorkflow(), new Subject1()));
23+
}
24+
25+
private function createWorkflow()
26+
{
27+
return $this->getMockBuilder(Workflow::class)
28+
->disableOriginalConstructor()
29+
->getMock();
30+
}
31+
}
32+
33+
class Subject1
34+
{
35+
}
36+
class Subject2
37+
{
38+
}

0 commit comments

Comments
 (0)