Skip to content

[DX] [Workflow] Added a way to specify a message when blocking a transition + better default message in case it is not set #34573

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
Nov 24, 2019
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
4 changes: 2 additions & 2 deletions src/Symfony/Component/Workflow/Event/GuardEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function isBlocked(): bool
return !$this->transitionBlockerList->isEmpty();
}

public function setBlocked(bool $blocked): void
public function setBlocked(bool $blocked, string $message = null): void
{
if (!$blocked) {
$this->transitionBlockerList->clear();

return;
}

$this->transitionBlockerList->add(TransitionBlocker::createUnknown());
$this->transitionBlockerList->add(TransitionBlocker::createUnknown($message));
}

public function getTransitionBlockerList(): TransitionBlockerList
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,23 @@ public function testBuildTransitionBlockerListReturnsReasonsProvidedInGuards()
$dispatcher->addListener('workflow.guard', function (GuardEvent $event) {
$event->setBlocked(true);
});
$dispatcher->addListener('workflow.guard', function (GuardEvent $event) {
$event->setBlocked(true, 'You should not pass !!');
});

$transitionBlockerList = $workflow->buildTransitionBlockerList($subject, 't1');
$this->assertCount(4, $transitionBlockerList);
$this->assertCount(5, $transitionBlockerList);
$blockers = iterator_to_array($transitionBlockerList);
$this->assertSame('Transition blocker 1', $blockers[0]->getMessage());
$this->assertSame('blocker_1', $blockers[0]->getCode());
$this->assertSame('Transition blocker 2', $blockers[1]->getMessage());
$this->assertSame('blocker_2', $blockers[1]->getCode());
$this->assertSame('Transition blocker 3', $blockers[2]->getMessage());
$this->assertSame('blocker_3', $blockers[2]->getCode());
$this->assertSame('Unknown reason.', $blockers[3]->getMessage());
$this->assertSame('The transition has been blocked by a guard (Symfony\Component\Workflow\Tests\WorkflowTest).', $blockers[3]->getMessage());
$this->assertSame('e8b5bbb9-5913-4b98-bfa6-65dbd228a82a', $blockers[3]->getCode());
$this->assertSame('You should not pass !!', $blockers[4]->getMessage());
$this->assertSame('e8b5bbb9-5913-4b98-bfa6-65dbd228a82a', $blockers[4]->getCode());
}

public function testApplyWithNotExisingTransition()
Expand Down
16 changes: 12 additions & 4 deletions src/Symfony/Component/Workflow/TransitionBlocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ public static function createBlockedByExpressionGuardListener(string $expression
/**
* Creates a blocker that says the transition cannot be made because of an
* unknown reason.
*
* This blocker code is chiefly for preserving backwards compatibility.
*/
public static function createUnknown(): self
public static function createUnknown(string $message = null, int $backtraceFrame = 2): self
{
return new static('Unknown reason.', self::UNKNOWN);
if (null !== $message) {
return new static($message, self::UNKNOWN);
}

$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $backtraceFrame + 1)[$backtraceFrame]['class'] ?? null;

if (null !== $caller) {
return new static("The transition has been blocked by a guard ($caller).", self::UNKNOWN);
}

return new static('The transition has been blocked by a guard.', self::UNKNOWN);
}

public function getMessage(): string
Expand Down