diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index dee3db5a37655..88480b7b8f713 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 WorkflowExtension (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 c2c5a55af954f..673dbcfe685af 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_marked_place', array($this, 'hasMarkedPlace')), ); } @@ -45,6 +46,11 @@ public function getEnabledTransitions($object, $name = null) return $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object); } + public function hasMarkedPlace($object, $place, $name = null) + { + return $this->workflowRegistry->get($object, $name)->getMarking($object)->has($place); + } + public function getName() { return 'workflow'; 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') }}"; 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..27a55670a35d2 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php @@ -0,0 +1,76 @@ + + * + * 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\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.'); + } + + $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); + + $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($this->extension->hasMarkedPlace($subject, 'ordered')); + $this->assertTrue($this->extension->hasMarkedPlace($subject, 'waiting_for_payment')); + $this->assertFalse($this->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()) {