Skip to content

Commit 06bcb03

Browse files
[Workflow] Add getEnabledTransition() to TraceableWorkflow
1 parent ba50c4e commit 06bcb03

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

Debug/TraceableWorkflow.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Workflow\Marking;
1717
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
1818
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
19+
use Symfony\Component\Workflow\Transition;
1920
use Symfony\Component\Workflow\TransitionBlockerList;
2021
use Symfony\Component\Workflow\WorkflowInterface;
2122

@@ -57,6 +58,11 @@ public function getEnabledTransitions(object $subject): array
5758
return $this->callInner(__FUNCTION__, \func_get_args());
5859
}
5960

61+
public function getEnabledTransition(object $subject, string $name): ?Transition
62+
{
63+
return $this->callInner(__FUNCTION__, \func_get_args());
64+
}
65+
6066
public function getName(): string
6167
{
6268
return $this->workflow->getName();

Tests/Debug/TraceableWorkflowTest.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\Tests\Debug;
13+
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Stopwatch\Stopwatch;
17+
use Symfony\Component\Workflow\Debug\TraceableWorkflow;
18+
use Symfony\Component\Workflow\Marking;
19+
use Symfony\Component\Workflow\TransitionBlockerList;
20+
use Symfony\Component\Workflow\Workflow;
21+
22+
class TraceableWorkflowTest extends TestCase
23+
{
24+
private MockObject|Workflow $innerWorkflow;
25+
26+
private StopWatch $stopwatch;
27+
28+
private TraceableWorkflow $traceableWorkflow;
29+
30+
protected function setUp(): void
31+
{
32+
$this->innerWorkflow = $this->createMock(Workflow::class);
33+
$this->stopwatch = new Stopwatch();
34+
35+
$this->traceableWorkflow = new TraceableWorkflow(
36+
$this->innerWorkflow,
37+
$this->stopwatch
38+
);
39+
}
40+
41+
/**
42+
* @dataProvider provideFunctionNames
43+
*/
44+
public function testCallsInner(string $function, array $args, mixed $returnValue)
45+
{
46+
$this->innerWorkflow->expects($this->once())
47+
->method($function)
48+
->willReturn($returnValue);
49+
50+
$this->assertSame($returnValue, $this->traceableWorkflow->{$function}(...$args));
51+
52+
$calls = $this->traceableWorkflow->getCalls();
53+
54+
$this->assertCount(1, $calls);
55+
$this->assertSame($function, $calls[0]['method']);
56+
$this->assertArrayHasKey('duration', $calls[0]);
57+
$this->assertSame($returnValue, $calls[0]['return']);
58+
}
59+
60+
public function testCallsInnerCatchesException()
61+
{
62+
$exception = new \Exception('foo');
63+
$this->innerWorkflow->expects($this->once())
64+
->method('can')
65+
->willThrowException($exception);
66+
67+
try {
68+
$this->traceableWorkflow->can(new \stdClass(), 'foo');
69+
70+
$this->fail('An exception should have been thrown.');
71+
} catch (\Exception $e) {
72+
$this->assertSame($exception, $e);
73+
74+
$calls = $this->traceableWorkflow->getCalls();
75+
76+
$this->assertCount(1, $calls);
77+
$this->assertSame('can', $calls[0]['method']);
78+
$this->assertArrayHasKey('duration', $calls[0]);
79+
$this->assertArrayHasKey('exception', $calls[0]);
80+
$this->assertSame($exception, $calls[0]['exception']);
81+
}
82+
}
83+
84+
public static function provideFunctionNames(): \Generator
85+
{
86+
$subject = new \stdClass();
87+
88+
yield ['getMarking', [$subject], new Marking(['place' => 1])];
89+
90+
yield ['can', [$subject, 'foo'], true];
91+
92+
yield ['buildTransitionBlockerList', [$subject, 'foo'], new TransitionBlockerList()];
93+
94+
yield ['apply', [$subject, 'foo'], new Marking(['place' => 1])];
95+
96+
yield ['getEnabledTransitions', [$subject], []];
97+
98+
yield ['getEnabledTransition', [$subject, 'foo'], null];
99+
}
100+
}

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"symfony/expression-language": "^5.4|^6.0|^7.0",
3232
"symfony/http-kernel": "^6.4|^7.0",
3333
"symfony/security-core": "^5.4|^6.0|^7.0",
34+
"symfony/stopwatch": "^5.4|^6.0|^7.0",
3435
"symfony/validator": "^5.4|^6.0|^7.0"
3536
},
3637
"conflict": {

0 commit comments

Comments
 (0)