Skip to content

[Workflow] Added new validator to make sure each place has unique translation names #21271

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface;
use Symfony\Component\Workflow\Validator\StateMachineValidator;
use Symfony\Component\Workflow\Validator\UniqueTransitionNameValidator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing composer.json update I guess, for ^3.2.3

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reference to symfony/workflow at all in the composer.json of the framework bundle. Should I still add one?

use Symfony\Component\Workflow\Validator\WorkflowValidator;

/**
Expand All @@ -40,6 +41,7 @@ public function process(ContainerBuilder $container)
}

$this->createValidator($tag)->validate($definition, $tag['name']);
(new UniqueTransitionNameValidator())->validate($definition, $tag['name']);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Symfony\Component\Workflow\Tests\Validator;

use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Validator\UniqueTransitionNameValidator;

class UniqueTransitionNameValidatorTest extends \PHPUnit_Framework_TestCase
{
use WorkflowBuilderTrait;

/**
* @expectedException \Symfony\Component\Workflow\Exception\InvalidDefinitionException
* @expectedExceptionMessage All transitions for a place must have an unique name. Multiple transitions named "t1" where found for place "a" in workflow "foo".
*/
public function testWorkflowWithInvalidNames()
{
$places = range('a', 'c');

$transitions = array();
$transitions[] = new Transition('t1', 'a', 'b');
$transitions[] = new Transition('t1', 'a', 'c');

$definition = new Definition($places, $transitions);

(new UniqueTransitionNameValidator())->validate($definition, 'foo');
}

public function testSameTransitionNameButNotSamePlace()
{
$places = range('a', 'd');

$transitions = array();
$transitions[] = new Transition('t1', 'a', 'b');
$transitions[] = new Transition('t1', 'b', 'c');
$transitions[] = new Transition('t1', 'd', 'c');

$definition = new Definition($places, $transitions);

(new UniqueTransitionNameValidator())->validate($definition, 'foo');
}

public function testValidWorkflow()
{
$definition = $this->createSimpleWorkflowDefinition();

(new UniqueTransitionNameValidator())->validate($definition, 'foo');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Component\Workflow\Validator;

use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Exception\InvalidDefinitionException;

/**
* Make sure all transitions for one place has unique name.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class UniqueTransitionNameValidator implements DefinitionValidatorInterface
{
public function validate(Definition $definition, $name)
{
$places = array_fill_keys($definition->getPlaces(), array());
foreach ($definition->getTransitions() as $transition) {
foreach ($transition->getFroms() as $from) {
if (in_array($transition->getName(), $places[$from])) {
throw new InvalidDefinitionException(sprintf('All transitions for a place must have an unique name. Multiple transitions named "%s" where found for place "%s" in workflow "%s".', $transition->getName(), $from, $name));
}
$places[$from][] = $transition->getName();
}
}
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy