Skip to content

Commit 4d0cc68

Browse files
Padam87lyrixx
authored andcommitted
[TwigBridge][Workflow] Added workflow_has_place twig function
1 parent bcab8b8 commit 4d0cc68

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function getFunctions()
3232
return array(
3333
new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')),
3434
new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
35+
new \Twig_SimpleFunction('workflow_has_place', array($this, 'hasPlace')),
3536
);
3637
}
3738

@@ -45,6 +46,14 @@ public function getEnabledTransitions($object, $name = null)
4546
return $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object);
4647
}
4748

49+
public function hasPlace($object, $state, $name = null)
50+
{
51+
$workflow = $this->workflowRegistry->get($object, $name);
52+
$marking = $workflow->getMarking($object);
53+
54+
return $marking->has($state);
55+
}
56+
4857
public function getName()
4958
{
5059
return 'workflow';
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Marking;
16+
use Symfony\Component\Workflow\Registry;
17+
use Symfony\Component\Workflow\Workflow;
18+
19+
class WorkflowExtensionTest extends \PHPUnit_Framework_TestCase
20+
{
21+
protected function setUp()
22+
{
23+
parent::setUp();
24+
25+
if (!class_exists('Symfony\Component\Workflow\Workflow')) {
26+
$this->markTestSkipped('The Workflow component is needed to run tests for this extension.');
27+
}
28+
}
29+
30+
public function testHasPlace()
31+
{
32+
$subject = new \stdClass();
33+
34+
$marking = new Marking(array('ordered' => true, 'waiting_for_payment' => true));
35+
36+
$workflow = $this->getMock(Workflow::class, array(), array(), '', false);
37+
$workflow->expects($this->exactly(3))
38+
->method('getMarking')
39+
->with($subject)
40+
->will($this->returnValue($marking));
41+
42+
$registry = $this->getMock(Registry::class);
43+
$registry->expects($this->exactly(3))
44+
->method('get')
45+
->with($subject)
46+
->will($this->returnValue($workflow));
47+
48+
$extension = new WorkflowExtension($registry);
49+
50+
$this->assertTrue($extension->hasPlace($subject, 'ordered'));
51+
$this->assertTrue($extension->hasPlace($subject, 'waiting_for_payment'));
52+
$this->assertFalse($extension->hasPlace($subject, 'processed'));
53+
}
54+
}

0 commit comments

Comments
 (0)