Skip to content

Commit

Permalink
bug #46008 [Workflow] Catch error when trying to get an uninitialized…
Browse files Browse the repository at this point in the history
… marking (lyrixx)

This PR was merged into the 4.4 branch.

Discussion
----------

[Workflow] Catch error when trying to get an uninitialized marking

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #44213
| License       | MIT
| Doc PR        |

Commits
-------

141320d [Workflow] Catch error when trying to get an uninitialized marking
  • Loading branch information
nicolas-grekas committed Apr 12, 2022
2 parents 58085fb + 141320d commit a90648c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Expand Up @@ -54,7 +54,15 @@ public function getMarking($subject): Marking
throw new LogicException(sprintf('The method "%s::%s()" does not exist.', \get_class($subject), $method));
}

$marking = $subject->{$method}();
$marking = null;
try {
$marking = $subject->{$method}();
} catch (\Error $e) {
$unInitializedPropertyMassage = sprintf('Typed property %s::$%s must not be accessed before initialization', get_debug_type($subject), $this->property);
if ($e->getMessage() !== $unInitializedPropertyMassage) {
throw $e;
}
}

if (null === $marking) {
return new Marking();
Expand Down
Expand Up @@ -87,6 +87,36 @@ public function testGetMarkingWithValueObject()
$this->assertSame('first_place', (string) $subject->getMarking());
}

/**
* @requires PHP 7.4
*/
public function testGetMarkingWithUninitializedProperty()
{
$subject = new SubjectWithType();

$markingStore = new MethodMarkingStore(true);

$marking = $markingStore->getMarking($subject);

$this->assertInstanceOf(Marking::class, $marking);
$this->assertCount(0, $marking->getPlaces());
}

/**
* @requires PHP 7.4
*/
public function testGetMarkingWithUninitializedProperty2()
{
$subject = new SubjectWithType();

$markingStore = new MethodMarkingStore(true, 'marking2');

$this->expectException(\Error::class);
$this->expectExceptionMessage('Typed property Symfony\Component\Workflow\Tests\MarkingStore\SubjectWithType::$marking must not be accessed before initialization');

$markingStore->getMarking($subject);
}

private function createValueObject(string $markingValue)
{
return new class($markingValue) {
Expand Down

0 comments on commit a90648c

Please sign in to comment.