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

Skip to content

Commit 6bfb447

Browse files
committed
[Workflow][Twig] Added workflow_has_place twig function
1 parent 16cea37 commit 6bfb447

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Twig\Extension;
1313

1414
use Symfony\Component\Workflow\Registry;
15+
use Symfony\Component\Workflow\Transition;
1516

1617
/**
1718
* WorkflowExtension.
@@ -32,6 +33,7 @@ public function getFunctions()
3233
return array(
3334
new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')),
3435
new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
36+
new \Twig_SimpleFunction('workflow_has_place', array($this, 'hasPlace')),
3537
);
3638
}
3739

@@ -45,6 +47,14 @@ public function getEnabledTransitions($object, $name = null)
4547
return $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object);
4648
}
4749

50+
public function hasPlace($object, $state, $name = null)
51+
{
52+
$workflow = $this->workflowRegistry->get($object, $name);
53+
$marking = $workflow->getMarking($object);
54+
55+
return $marking->has($state);
56+
}
57+
4858
public function getName()
4959
{
5060
return 'workflow';
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Bridge\Twig\Tests\Extension;
13+
14+
use Symfony\Bridge\Twig\Extension\WorkflowExtension;
15+
use Symfony\Component\Workflow\Definition;
16+
use Symfony\Component\Workflow\Marking;
17+
use Symfony\Component\Workflow\Registry;
18+
use Symfony\Component\Workflow\Transition;
19+
use Symfony\Component\Workflow\Workflow;
20+
21+
class WorkflowExtensionTest extends \PHPUnit_Framework_TestCase
22+
{
23+
protected function setUp()
24+
{
25+
parent::setUp();
26+
27+
if (!class_exists('Symfony\Component\Workflow\Workflow')) {
28+
$this->markTestSkipped('The Workflow component is needed to run tests for this extension.');
29+
}
30+
}
31+
32+
public function testHasPlace()
33+
{
34+
$subject = new \stdClass();
35+
36+
$marking = new Marking(array('ordered' => true, 'waiting_for_payment' => true));
37+
38+
$workflow = $this->getMock(Workflow::class, array(), array(), '', false);
39+
$workflow->expects($this->exactly(3))
40+
->method('getMarking')
41+
->with($subject)
42+
->will($this->returnValue($marking));
43+
44+
$registry = $this->getMock(Registry::class);
45+
$registry->expects($this->exactly(3))
46+
->method('get')
47+
->with($subject)
48+
->will($this->returnValue($workflow));
49+
50+
$extension = new WorkflowExtension($registry);
51+
52+
$this->assertTrue($extension->hasPlace($subject, 'ordered'));
53+
$this->assertTrue($extension->hasPlace($subject, 'waiting_for_payment'));
54+
$this->assertFalse($extension->hasPlace($subject, 'processed'));
55+
}
56+
}

0 commit comments

Comments
 (0)