[TwigBridge][Workflow] Added workflow_has_place twig function

This commit is contained in:
Adam Prager 2016-12-06 14:42:30 +01:00 committed by Grégoire Pineau
parent bcab8b8ac8
commit 4d0cc6836f
2 changed files with 63 additions and 0 deletions

View File

@ -32,6 +32,7 @@ class WorkflowExtension extends \Twig_Extension
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 @@ class WorkflowExtension extends \Twig_Extension
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';

View File

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'));
}
}