Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Workflow]Fix Marking when it must contains more than one tokens #53865

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 1 commit into from
Mar 11, 2024
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
37 changes: 30 additions & 7 deletions 37 src/Symfony/Component/Workflow/Marking.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,54 @@
class Marking
{
private $places = [];
private $context = null;
private $context;

/**
* @param int[] $representation Keys are the place name and values should be 1
*/
public function __construct(array $representation = [])
{
foreach ($representation as $place => $nbToken) {
$this->mark($place);
$this->mark($place, $nbToken);
}
}

public function mark(string $place)
public function mark(string $place, int $nbToken = 1)
{
$this->places[$place] = 1;
if ($nbToken < 1) {
throw new \LogicException(sprintf('The number of tokens must be greater than 0, "%s" given.', $nbToken));
}

if (!\array_key_exists($place, $this->places)) {
$this->places[$place] = 0;
}
$this->places[$place] += $nbToken;
}

public function unmark(string $place)
public function unmark(string $place, int $nbToken = 1)
Copy link
Member

Choose a reason for hiding this comment

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

this change is a BC break for child classes overriding the method with extra parameter(s). It should rather be done the smooth way on 7.1

{
unset($this->places[$place]);
if ($nbToken < 1) {
throw new \LogicException(sprintf('The number of tokens must be greater than 0, "%s" given.', $nbToken));
}

if (!$this->has($place)) {
throw new \LogicException(sprintf('The place "%s" is not marked.', $place));
}

$this->places[$place] -= $nbToken;

if (0 > $this->places[$place]) {
throw new \LogicException(sprintf('The place "%s" could not contain a negative token number.', $place));
}

if (0 === $this->places[$place]) {
unset($this->places[$place]);
}
}

public function has(string $place)
{
return isset($this->places[$place]);
return \array_key_exists($place, $this->places);
}

public function getPlaces()
Expand Down
54 changes: 50 additions & 4 deletions 54 src/Symfony/Component/Workflow/Tests/MarkingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,70 @@ public function testMarking()

$this->assertTrue($marking->has('a'));
$this->assertFalse($marking->has('b'));
$this->assertSame(['a' => 1], $marking->getPlaces());
$this->assertPlaces(['a' => 1], $marking);

$marking->mark('b');

$this->assertTrue($marking->has('a'));
$this->assertTrue($marking->has('b'));
$this->assertSame(['a' => 1, 'b' => 1], $marking->getPlaces());
$this->assertPlaces(['a' => 1, 'b' => 1], $marking);

$marking->unmark('a');

$this->assertFalse($marking->has('a'));
$this->assertTrue($marking->has('b'));
$this->assertSame(['b' => 1], $marking->getPlaces());
$this->assertPlaces(['b' => 1], $marking);

$marking->unmark('b');

$this->assertFalse($marking->has('a'));
$this->assertFalse($marking->has('b'));
$this->assertSame([], $marking->getPlaces());
$this->assertPlaces([], $marking);

$marking->mark('a');
$this->assertPlaces(['a' => 1], $marking);

$marking->mark('a');
$this->assertPlaces(['a' => 2], $marking);

$marking->unmark('a');
$this->assertPlaces(['a' => 1], $marking);

$marking->unmark('a');
$this->assertPlaces([], $marking);
}

public function testGuardNotMarked()
{
$marking = new Marking([]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The place "a" is not marked.');
$marking->unmark('a');
}

public function testGuardNotNbTokenLowerThanZero()
{
$marking = new Marking(['a' => 1]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The place "a" could not contain a negative token number.');
$marking->unmark('a', 2);
}

public function testGuardNotNbTokenEquals0()
{
$marking = new Marking(['a' => 1]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The number of tokens must be greater than 0, "0" given.');
$marking->unmark('a', 0);
}

private function assertPlaces(array $expected, Marking $marking)
{
$places = $marking->getPlaces();
ksort($places);
$this->assertSame($expected, $places);
}
}
39 changes: 39 additions & 0 deletions 39 src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,43 @@ private static function createComplexStateMachineDefinition()
// | d | -------------+
// +-----+
}

private static function createWorkflowWithSameNameBackTransition(): Definition
{
$places = range('a', 'c');

$transitions = [];
$transitions[] = new Transition('a_to_bc', 'a', ['b', 'c']);
$transitions[] = new Transition('back1', 'b', 'a');
$transitions[] = new Transition('back1', 'c', 'b');
$transitions[] = new Transition('back2', 'c', 'b');
$transitions[] = new Transition('back2', 'b', 'a');
$transitions[] = new Transition('c_to_cb', 'c', ['b', 'c']);

return new Definition($places, $transitions);

// The graph looks like:
// +-----------------------------------------------------------------+
// | |
// | |
// | +---------------------------------------------+ |
// v | v |
// +---+ +---------+ +-------+ +---------+ +---+ +-------+
// | a | --> | a_to_bc | --> | | --> | back2 | --> | | --> | back2 |
// +---+ +---------+ | | +---------+ | | +-------+
// ^ | | | |
// | | c | <-----+ | b |
// | | | | | |
// | | | +---------+ | | +-------+
// | | | --> | c_to_cb | --> | | --> | back1 |
// | +-------+ +---------+ +---+ +-------+
// | | ^ |
// | | | |
// | v | |
// | +-------+ | |
// | | back1 | ----------------------+ |
// | +-------+ |
// | |
// +-----------------------------------------------------------------+
}
}
85 changes: 73 additions & 12 deletions 85 src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,28 +330,32 @@ public function testApplyWithSameNameTransition()

$marking = $workflow->apply($subject, 'a_to_bc');

$this->assertFalse($marking->has('a'));
$this->assertTrue($marking->has('b'));
$this->assertTrue($marking->has('c'));
$this->assertPlaces([
'b' => 1,
'c' => 1,
], $marking);

$marking = $workflow->apply($subject, 'to_a');

$this->assertTrue($marking->has('a'));
$this->assertFalse($marking->has('b'));
$this->assertFalse($marking->has('c'));
// Two tokens in "a"
$this->assertPlaces([
'a' => 2,
], $marking);

$workflow->apply($subject, 'a_to_bc');
$marking = $workflow->apply($subject, 'b_to_c');

$this->assertFalse($marking->has('a'));
$this->assertFalse($marking->has('b'));
$this->assertTrue($marking->has('c'));
$this->assertPlaces([
'a' => 1,
'c' => 2,
], $marking);

$marking = $workflow->apply($subject, 'to_a');

$this->assertTrue($marking->has('a'));
$this->assertFalse($marking->has('b'));
$this->assertFalse($marking->has('c'));
$this->assertPlaces([
'a' => 2,
'c' => 1,
], $marking);
}

public function testApplyWithSameNameTransition2()
Expand Down Expand Up @@ -785,6 +789,63 @@ public function testGetEnabledTransitionsWithSameNameTransition()
$this->assertSame('to_a', $transitions[1]->getName());
$this->assertSame('to_a', $transitions[2]->getName());
}

/**
* @@testWith ["back1"]
* ["back2"]
*/
public function testApplyWithSameNameBackTransition(string $transition)
{
$definition = $this->createWorkflowWithSameNameBackTransition();
$workflow = new Workflow($definition, new MethodMarkingStore());

$subject = new Subject();

$marking = $workflow->apply($subject, 'a_to_bc');
$this->assertPlaces([
'b' => 1,
'c' => 1,
], $marking);

$marking = $workflow->apply($subject, $transition);
$this->assertPlaces([
'a' => 1,
'b' => 1,
], $marking);

$marking = $workflow->apply($subject, $transition);
$this->assertPlaces([
'a' => 2,
], $marking);

$marking = $workflow->apply($subject, 'a_to_bc');
$this->assertPlaces([
'a' => 1,
'b' => 1,
'c' => 1,
], $marking);

$marking = $workflow->apply($subject, 'c_to_cb');
$this->assertPlaces([
'a' => 1,
'b' => 2,
'c' => 1,
], $marking);

$marking = $workflow->apply($subject, 'c_to_cb');
$this->assertPlaces([
'a' => 1,
'b' => 3,
'c' => 1,
], $marking);
}

private function assertPlaces(array $expected, Marking $marking)
{
$places = $marking->getPlaces();
ksort($places);
$this->assertSame($expected, $places);
}
}

class EventDispatcherMock implements \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.