8000 minor · msgphp/msgphp@d0c96e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0c96e7

Browse files
committed
minor
1 parent 3004d3f commit d0c96e7

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

src/Domain/GenericDomainId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static function fromValue($value): DomainId
2727
if (null === $value || \is_string($value)) {
2828
return new self($value);
2929
}
30-
if (is_numeric($value)) {
30+
if (is_numeric($value) || (\is_object($value) && method_exists($value, '__toString'))) {
3131
return new self((string) $value);
3232
}
3333

src/Domain/Infrastructure/Uid/DomainUuid.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ public function __toString(): string
2828

2929
public static function fromValue($value): DomainId
3030
{
31-
if (null === $value || $value instanceof UuidInterface) {
31+
if ($value instanceof UuidInterface) {
3232
return new self($value);
3333
}
34+
if (null === $value) {
35+
return new self(Uuid::fromString(Uuid::NIL));
36+
}
37+
if (\is_object($value) && method_exists($value, '__toString')) {
38+
return new self(Uuid::fromString((string) $value));
39+
}
3440
if (\is_string($value)) {
3541
return new self(Uuid::fromString($value));
3642
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Domain\Tests\Fixtures;
6+
7+
final class StringableValue
8+
{
9+
/** @var string */
10+
private $value;
11+
12+
public function __construct(string $value)
13+
{
14+
$this->value = $value;
15+
}
16+
17+
public function __toString(): string
18+
{
19+
return $this->value;
20+
}
21+
}

src/Domain/Tests/GenericDomainIdTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use MsgPhp\Domain\DomainId;
88
use MsgPhp\Domain\GenericDomainId;
9+
use MsgPhp\Domain\Tests\Fixtures\StringableValue;
910
use PHPUnit\Framework\TestCase;
1011

1112
/**
@@ -20,6 +21,7 @@ public function testFromValue(): void
2021
self::assertSame((array) new GenericDomainId(null), (array) GenericDomainId::fromValue(null));
2122
self::assertSame((array) new GenericDomainId(''), (array) GenericDomainId::fromValue(null));
2223
self::assertSame((array) new GenericDomainId('foo'), (array) GenericDomainId::fromValue('foo'));
24+
self::assertSame((array) new GenericDomainId('foo'), (array) GenericDomainId::fromValue(new StringableValue('foo')));
2325
self::assertSame((array) new GenericDomainId('1'), (array) GenericDomainId::fromValue(1));
2426
self::assertSame((array) new GenericDomainId(' '), (array) GenericDomainId::fromValue(' '));
2527
}
@@ -31,13 +33,12 @@ public function testFromInvalidValue(): void
3133
GenericDomainId::fromValue(true);
3234
}
3335

34-
public function testIsNil(): void
36+
/**
37+
* @dataProvider provideIds
38+
*/
39+
public function testIsNil(DomainId $id, string $value): void
3540
{
36-
self::assertTrue((new GenericDomainId())->isNil());
37-
self::assertTrue((new GenericDomainId(null))->isNil());
38-
self::assertTrue((new GenericDomainId(''))->isNil());
39-
self::assertFalse((new GenericDomainId(' '))->isNil());
40-
self::assertFalse((new GenericDomainId('foo'))->isNil());
41+
self::assertSame('' === $value, $id->isNil());
4142
}
4243

4344
public function testEquals(): void
@@ -83,6 +84,8 @@ public function provideIds(): iterable
8384
{
8485
yield [new GenericDomainId(), ''];
8586
yield [new GenericDomainId(null), ''];
87+
yield [new GenericDomainId(''), ''];
88+
yield [new GenericDomainId('0'), '0'];
8689
yield [new GenericDomainId('foo'), 'foo'];
8790
yield [new GenericDomainId(' '), ' '];
8891
}

src/Domain/Tests/Infrastructure/Uid/DomainUuidTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use MsgPhp\Domain\GenericDomainId;
88
use MsgPhp\Domain\Infrastructure\Uid\DomainUuid;
9+
use MsgPhp\Domain\Tests\Fixtures\StringableValue;
910
use PHPUnit\Framework\TestCase;
1011
use Ramsey\Uuid\Uuid;
1112

@@ -20,16 +21,16 @@ public function testFromValue(): void
2021

2122
self::assertInstanceOf(DomainUuid::class, DomainUuid::fromValue(null));
2223
self::assertSame((array) new DomainUuid($uuid), (array) DomainUuid::fromValue($uuid));
23-
self::assertNotSame((array) new DomainUuid(), (array) DomainUuid::fromValue(null));
24-
self::assertNotSame((array) new DomainUuid(null), (array) DomainUuid::fromValue(null));
25-
self::assertNotSame((array) new DomainUuid($uuid), (array) DomainUuid::fromValue(null));
24+
self::assertSame(Uuid::NAMESPACE_DNS, DomainUuid::fromValue(Uuid::NAMESPACE_DNS)->toString());
25+
self::assertSame(Uuid::NIL, DomainUuid::fromValue(new StringableValue(Uuid::NIL))->toString());
26+
self::assertSame(Uuid::NIL, DomainUuid::fromValue(null)->toString());
2627
}
2728

2829
public function testFromValueWithInvalidUuid(): void
2930
{
3031
$this->expectException(\LogicException::class);
3132

32-
DomainUuid::fromValue('foo');
33+
DomainUuid::fromValue('');
3334
}
3435

3536
public function testFromInvalidValue(): void

0 commit comments

Comments
 (0)
0