8000 [TwigBridge][Worklow] Added a new workflow_has_place function by lyrixx · Pull Request #21253 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge][Worklow] Added a new workflow_has_place function #21253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,6 +41,7 @@ CHANGELOG
$twig->addExtension(new FormExtension());
```
* Deprecated the `TwigRendererEngineInterface` interface.
* added WorkflowExtension (provides `workflow_can` and `workflow_transitions`)

2.7.0
-----
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
);
}

Expand All @@ -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';
Expand Down
2 changes: 0 additions & 2 deletions src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

class CodeExtensionTest extends \PHPUnit_Framework_TestCase
{
protected $helper;

public function testFormatFile()
{
$expected = sprintf('<a href="proto://foobar%s#&amp;line=25" title="Click to open this file" class="file_link">%s at line 25</a>', substr(__FILE__, 5), __FILE__);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

class ExpressionExtensionTest extends \PHPUnit_Framework_TestCase
{
protected $helper;

public function testExpressionCreation()
{
$template = "{{ expression('1 == 1') }}";
Expand Down
76 changes: 76 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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\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);< 8000 /span>

$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'));
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Workflow/Marking.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
0