Skip to content

[Workflow] Add getEnabledTransition() to TraceableWorkflow #52814

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
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
6 changes: 6 additions & 0 deletions src/Symfony/Component/Workflow/Debug/TraceableWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Workflow\Marking;
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\TransitionBlockerList;
use Symfony\Component\Workflow\WorkflowInterface;

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

public function getEnabledTransition(object $subject, string $name): ?Transition
{
return $this->callInner(__FUNCTION__, \func_get_args());
}

public function getName(): string
{
return $this->workflow->getName();
Expand Down
100 changes: 100 additions & 0 deletions src/Symfony/Component/Workflow/Tests/Debug/TraceableWorkflowTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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\Tests\Debug;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Workflow\Debug\TraceableWorkflow;
use Symfony\Component\Workflow\Marking;
use Symfony\Component\Workflow\TransitionBlockerList;
use Symfony\Component\Workflow\Workflow;

class TraceableWorkflowTest extends TestCase
{
private MockObject|Workflow $innerWorkflow;

private StopWatch $stopwatch;

private TraceableWorkflow $traceableWorkflow;

protected function setUp(): void
{
$this->innerWorkflow = $this->createMock(Workflow::class);
$this->stopwatch = new Stopwatch();

$this->traceableWorkflow = new TraceableWorkflow(
$this->innerWorkflow,
$this->stopwatch
);
}

/**
* @dataProvider provideFunctionNames
*/
public function testCallsInner(string $function, array $args, mixed $returnValue)
{
$this->innerWorkflow->expects($this->once())
->method($function)
->willReturn($returnValue);

$this->assertSame($returnValue, $this->traceableWorkflow->{$function}(...$args));

$calls = $this->traceableWorkflow->getCalls();

$this->assertCount(1, $calls);
$this->assertSame($function, $calls[0]['method']);
$this->assertArrayHasKey('duration', $calls[0]);
$this->assertSame($returnValue, $calls[0]['return']);
}

public function testCallsInnerCatchesException()
{
$exception = new \Exception('foo');
$this->innerWorkflow->expects($this->once())
->method('can')
->willThrowException($exception);

try {
$this->traceableWorkflow->can(new \stdClass(), 'foo');

$this->fail('An exception should have been thrown.');
} catch (\Exception $e) {
$this->assertSame($exception, $e);

$calls = $this->traceableWorkflow->getCalls();

$this->assertCount(1, $calls);
$this->assertSame('can', $calls[0]['method']);
$this->assertArrayHasKey('duration', $calls[0]);
$this->assertArrayHasKey('exception', $calls[0]);
$this->assertSame($exception, $calls[0]['exception']);
}
}

public static function provideFunctionNames(): \Generator
{
$subject = new \stdClass();

yield ['getMarking', [$subject], new Marking(['place' => 1])];

yield ['can', [$subject, 'foo'], true];

yield ['buildTransitionBlockerList', [$subject, 'foo'], new TransitionBlockerList()];

yield ['apply', [$subject, 'foo'], new Marking(['place' => 1])];

yield ['getEnabledTransitions', [$subject], []];

yield ['getEnabledTransition', [$subject, 'foo'], null];
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Workflow/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"symfony/expression-language": "^5.4|^6.0|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/security-core": "^5.4|^6.0|^7.0",
"symfony/stopwatch": "^5.4|^6.0|^7.0",
"symfony/validator": "^5.4|^6.0|^7.0"
},
"conflict": {
Expand Down