From 4d0cc6836f203951d064ceee39e02c6b8df45af8 Mon Sep 17 00:00:00 2001 From: Adam Prager Date: Tue, 6 Dec 2016 14:42:30 +0100 Subject: [PATCH 1/4] [TwigBridge][Workflow] Added workflow_has_place twig function --- .../Twig/Extension/WorkflowExtension.php | 9 ++++ .../Tests/Extension/WorkflowExtensionTest.php | 54 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php diff --git a/src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php b/src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php index c2c5a55af954f..aae72e80f6e39 100644 --- a/src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php @@ -32,6 +32,7 @@ public function getFunctions() return array( new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')), new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')), + new \Twig_SimpleFunction('workflow_has_place', array($this, 'hasPlace')), ); } @@ -45,6 +46,14 @@ public function getEnabledTransitions($object, $name = null) return $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object); } + public function hasPlace($object, $state, $name = null) + { + $workflow = $this->workflowRegistry->get($object, $name); + $marking = $workflow->getMarking($object); + + return $marking->has($state); + } + public function getName() { return 'workflow'; diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php new file mode 100644 index 0000000000000..4ca4efabfdc86 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Tests\Extension; + +use Symfony\Bridge\Twig\Extension\WorkflowExtension; +use Symfony\Component\Workflow\Marking; +use Symfony\Component\Workflow\Registry; +use Symfony\Component\Workflow\Workflow; + +class WorkflowExtensionTest extends \PHPUnit_Framework_TestCase +{ + protected function setUp() + { + parent::setUp(); + + if (!class_exists('Symfony\Component\Workflow\Workflow')) { + $this->markTestSkipped('The Workflow component is needed to run tests for this extension.'); + } + } + + public function testHasPlace() + { + $subject = new \stdClass(); + + $marking = new Marking(array('ordered' => true, 'waiting_for_payment' => true)); + + $workflow = $this->getMock(Workflow::class, array(), array(), '', false); + $workflow->expects($this->exactly(3)) + ->method('getMarking') + ->with($subject) + ->will($this->returnValue($marking)); + + $registry = $this->getMock(Registry::class); + $registry->expects($this->exactly(3)) + ->method('get') + ->with($subject) + ->will($this->returnValue($workflow)); + + $extension = new WorkflowExtension($registry); + + $this->assertTrue($extension->hasPlace($subject, 'ordered')); + $this->assertTrue($extension->hasPlace($subject, 'waiting_for_payment')); + $this->assertFalse($extension->hasPlace($subject, 'processed')); + } +} From efe500da84b39d7a221b81121536e0617dfeaa34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 12 Jan 2017 18:39:43 +0100 Subject: [PATCH 2/4] [TwigBridge][Workflow] Fixed code and tests --- src/Symfony/Bridge/Twig/CHANGELOG.md | 10 ++++-- .../Twig/Extension/WorkflowExtension.php | 9 ++--- .../Tests/Extension/WorkflowExtensionTest.php | 34 +++++++------------ src/Symfony/Component/Workflow/Marking.php | 2 +- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index dee3db5a37655..804b2fc937b1a 100644 --- a/src/Symfony/Bridge/Twig/CHANGELOG.md +++ b/src/Symfony/Bridge/Twig/CHANGELOG.md @@ -1,14 +1,19 @@ CHANGELOG ========= +3.3.0 +----- + + * added a `workflow_has_marked_place` function + 3.2.0 ----- * added `AppVariable::getToken()` * Deprecated the possibility to inject the Form `TwigRenderer` into the `FormExtension`. - * [BC BREAK] Registering the `FormExtension` without configuring a runtime loader for the `TwigRenderer` + * [BC BREAK] Registering the `FormExtension` without configuring a runtime loader for the `TwigRenderer` doesn't work anymore. - + Before: ```php @@ -36,6 +41,7 @@ CHANGELOG $twig->addExtension(new FormExtension()); ``` * Deprecated the `TwigRendererEngineInterface` interface. + * added WorkflownExtension (provides `workflow_can` and `workflow_transitions`) 2.7.0 ----- diff --git a/src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php b/src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php index aae72e80f6e39..673dbcfe685af 100644 --- a/src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php @@ -32,7 +32,7 @@ public function getFunctions() return array( new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')), new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')), - new \Twig_SimpleFunction('workflow_has_place', array($this, 'hasPlace')), + new \Twig_SimpleFunction('workflow_has_marked_place', array($this, 'hasMarkedPlace')), ); } @@ -46,12 +46,9 @@ public function getEnabledTransitions($object, $name = null) return $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object); } - public function hasPlace($object, $state, $name = null) + public function hasMarkedPlace($object, $place, $name = null) { - $workflow = $this->workflowRegistry->get($object, $name); - $marking = $workflow->getMarking($object); - - return $marking->has($state); + return $this->workflowRegistry->get($object, $name)->getMarking($object)->has($place); } public function getName() diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php index 4ca4efabfdc86..7929fcf55aebb 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php @@ -12,6 +12,7 @@ namespace Symfony\Bridge\Twig\Tests\Extension; use Symfony\Bridge\Twig\Extension\WorkflowExtension; +use Symfony\Component\Workflow\Definition; use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\Registry; use Symfony\Component\Workflow\Workflow; @@ -20,35 +21,26 @@ class WorkflowExtensionTest extends \PHPUnit_Framework_TestCase { protected function setUp() { - parent::setUp(); - - if (!class_exists('Symfony\Component\Workflow\Workflow')) { + if (!class_exists(Workflow::class)) { $this->markTestSkipped('The Workflow component is needed to run tests for this extension.'); } } - public function testHasPlace() + public function testHasMarkedPlace() { - $subject = new \stdClass(); - - $marking = new Marking(array('ordered' => true, 'waiting_for_payment' => true)); + $definition = new Definition(['ordered', 'waiting_for_payment', 'processed'], []); + $workflow = new Workflow($definition); - $workflow = $this->getMock(Workflow::class, array(), array(), '', false); - $workflow->expects($this->exactly(3)) - ->method('getMarking') - ->with($subject) - ->will($this->returnValue($marking)); - - $registry = $this->getMock(Registry::class); - $registry->expects($this->exactly(3)) - ->method('get') - ->with($subject) - ->will($this->returnValue($workflow)); + $registry = new Registry(); + $registry->add($workflow, \stdClass::class); $extension = new WorkflowExtension($registry); - $this->assertTrue($extension->hasPlace($subject, 'ordered')); - $this->assertTrue($extension->hasPlace($subject, 'waiting_for_payment')); - $this->assertFalse($extension->hasPlace($subject, 'processed')); + $subject = new \stdClass(); + $subject->marking = array('ordered' => 1, 'waiting_for_payment' => 1); + + $this->assertTrue($extension->hasMarkedPlace($subject, 'ordered')); + $this->assertTrue($extension->hasMarkedPlace($subject, 'waiting_for_payment')); + $this->assertFalse($extension->hasMarkedPlace($subject, 'processed')); } } diff --git a/src/Symfony/Component/Workflow/Marking.php b/src/Symfony/Component/Workflow/Marking.php index 0d8bab25b7fe3..d44ae9fb0863d 100644 --- a/src/Symfony/Component/Workflow/Marking.php +++ b/src/Symfony/Component/Workflow/Marking.php @@ -21,7 +21,7 @@ class Marking private $places = array(); /** - * @param string[] $representation Keys are the place name and values should be 1 + * @param int[] $representation Keys are the place name and values should be 1 */ public function __construct(array $representation = array()) { From 77f820ee7c66189d4f9d23b3e16273dd2d642b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 12 Jan 2017 18:47:22 +0100 Subject: [PATCH 3/4] [TwigBridge][Workflow] Added more tests on WorkflowExtension --- .../Tests/Extension/WorkflowExtensionTest.php | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php index 7929fcf55aebb..27a55670a35d2 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php @@ -15,32 +15,62 @@ use Symfony\Component\Workflow\Definition; use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\Registry; +use Symfony\Component\Workflow\Transition; use Symfony\Component\Workflow\Workflow; class WorkflowExtensionTest extends \PHPUnit_Framework_TestCase { + private $extension; + protected function setUp() { if (!class_exists(Workflow::class)) { $this->markTestSkipped('The Workflow component is needed to run tests for this extension.'); } - } - public function testHasMarkedPlace() - { - $definition = new Definition(['ordered', 'waiting_for_payment', 'processed'], []); + $places = array('ordered', 'waiting_for_payment', 'processed'); + $transitions = array( + new Transition('t1', 'ordered', 'waiting_for_payment'), + new Transition('t2', 'waiting_for_payment', 'processed'), + ); + $definition = new Definition($places, $transitions); $workflow = new Workflow($definition); $registry = new Registry(); $registry->add($workflow, \stdClass::class); - $extension = new WorkflowExtension($registry); + $this->extension = new WorkflowExtension($registry); + } + + public function testCanTransition() + { + $subject = new \stdClass(); + $subject->marking = array(); + + $this->assertTrue($this->extension->canTransition($subject, 't1')); + $this->assertFalse($this->extension->canTransition($subject, 't2')); + } + + public function testGetEnabledTransitions() + { + $subject = new \stdClass(); + $subject->marking = array(); + + $transitions = $this->extension->getEnabledTransitions($subject); + $this->assertCount(1, $transitions); + $this->assertInstanceOf(Transition::class, $transitions[0]); + $this->assertSame('t1', $transitions[0]->getName()); + } + + public function testHasMarkedPlace() + { $subject = new \stdClass(); + $subject->marking = array(); $subject->marking = array('ordered' => 1, 'waiting_for_payment' => 1); - $this->assertTrue($extension->hasMarkedPlace($subject, 'ordered')); - $this->assertTrue($extension->hasMarkedPlace($subject, 'waiting_for_payment')); - $this->assertFalse($extension->hasMarkedPlace($subject, 'processed')); + $this->assertTrue($this->extension->hasMarkedPlace($subject, 'ordered')); + $this->assertTrue($this->extension->hasMarkedPlace($subject, 'waiting_for_payment')); + $this->assertFalse($this->extension->hasMarkedPlace($subject, 'processed')); } } From 108c89d1cf9b8ab2bc46432679d069e47f4f523a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 12 Jan 2017 18:49:13 +0100 Subject: [PATCH 4/4] [TwigBridge] Removed unused class property --- src/Symfony/Bridge/Twig/CHANGELOG.md | 2 +- src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php | 2 -- .../Bridge/Twig/Tests/Extension/ExpressionExtensionTest.php | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index 804b2fc937b1a..88480b7b8f713 100644 --- a/src/Symfony/Bridge/Twig/CHANGELOG.md +++ b/src/Symfony/Bridge/Twig/CHANGELOG.md @@ -41,7 +41,7 @@ CHANGELOG $twig->addExtension(new FormExtension()); ``` * Deprecated the `TwigRendererEngineInterface` interface. - * added WorkflownExtension (provides `workflow_can` and `workflow_transitions`) + * added WorkflowExtension (provides `workflow_can` and `workflow_transitions`) 2.7.0 ----- diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php index e1883a526d767..e322189a0dace 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php @@ -16,8 +16,6 @@ class CodeExtensionTest extends \PHPUnit_Framework_TestCase { - protected $helper; - public function testFormatFile() { $expected = sprintf('%s at line 25', substr(__FILE__, 5), __FILE__); diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/ExpressionExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/ExpressionExtensionTest.php index 6d457e395b07e..df3eb8afcdc38 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/ExpressionExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/ExpressionExtensionTest.php @@ -15,8 +15,6 @@ class ExpressionExtensionTest extends \PHPUnit_Framework_TestCase { - protected $helper; - public function testExpressionCreation() { $template = "{{ expression('1 == 1') }}";