Skip to content

Commit

Permalink
bug #34294 [Workflow] Fix error when we use ValueObject for the marki…
Browse files Browse the repository at this point in the history
…ng property (FabienSalles)

This PR was merged into the 4.3 branch.

Discussion
----------

[Workflow] Fix error when we use ValueObject for the marking property

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #28203 #22031
| License       | MIT

Fix Illegal offset type in `MethodMarkingStore` class when we use Value Object for
the marking property.

Now, we can avoid to use only a string an we can have a Subject class with a Value Object like this :
```php
final class State
{
    public const DRAFT = 'draft';
    public const REVIEWED = 'reviewed';
    public const REJECTED = 'rejected';
    public const PUBLISHED = 'published';

     /** @var string */
    private $state;

    public function __construct(string $state)
    {
        // some validation
        $this->state = $state;
    }

    public function __toString()
     {
         return $this->state;
    }

    public static function Draft()
     {
         return new self(self::DRAFT);
     }
    ...
}

final class Subject
{
    private $marking;

    public function __construct(State $marking = null)
    {
        $this->marking = $marking;
    }

    public function getMarking()
    {
        return $this->marking;
    }

    public function setMarking($marking)
    {
        $this->marking = $marking instanceof State ? $marking : new State($marking);
    }

```

Commits
-------

6570d5c Fix error when we use VO for the marking property
  • Loading branch information
nicolas-grekas committed Nov 8, 2019
2 parents 618aec6 + 6570d5c commit bc726f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Expand Up @@ -61,7 +61,7 @@ public function getMarking($subject)
}

if ($this->singleState) {
$marking = [$marking => 1];
$marking = [(string) $marking => 1];
}

return new Marking($marking);
Expand Down
Expand Up @@ -52,4 +52,35 @@ public function testGetSetMarkingWithSingleState()

$this->assertEquals($marking, $marking2);
}

public function testGetMarkingWithValueObject()
{
$subject = new Subject($this->createValueObject('first_place'));

$markingStore = new MethodMarkingStore(true);

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

$this->assertInstanceOf(Marking::class, $marking);
$this->assertCount(1, $marking->getPlaces());
$this->assertSame('first_place', (string) $subject->getMarking());
}

private function createValueObject(string $markingValue)
{
return new class($markingValue) {
/** @var string */
private $markingValue;

public function __construct(string $markingValue)
{
$this->markingValue = $markingValue;
}

public function __toString()
{
return $this->markingValue;
}
};
}
}

0 comments on commit bc726f7

Please sign in to comment.