[Uid] Unable to extend Uuid/Ulid and use fromString()

This commit is contained in:
Oskar Stark 2021-01-20 10:59:48 +01:00 committed by Nicolas Grekas
parent c639531fe4
commit 824777938d
5 changed files with 49 additions and 1 deletions

View File

@ -0,0 +1,18 @@
<?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\Uid\Tests\Fixtures;
use Symfony\Component\Uid\Ulid;
final class CustomUlid extends Ulid
{
}

View File

@ -0,0 +1,18 @@
<?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\Uid\Tests\Fixtures;
use Symfony\Component\Uid\Uuid;
final class CustomUuid extends Uuid
{
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Uid\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV4;
@ -118,4 +119,9 @@ class UlidTest extends TestCase
$this->assertLessThan(0, $b->compare($c));
$this->assertGreaterThan(0, $c->compare($b));
}
public function testFromStringOnExtendedClassReturnsStatic()
{
$this->assertInstanceOf(CustomUlid::class, CustomUlid::fromString((new CustomUlid())->toBinary()));
}
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Uid\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\NilUuid;
use Symfony\Component\Uid\Tests\Fixtures\CustomUuid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;
@ -185,4 +186,9 @@ class UuidTest extends TestCase
$this->assertInstanceOf(NilUuid::class, $uuid);
$this->assertSame('00000000-0000-0000-0000-000000000000', (string) $uuid);
}
public function testFromStringOnExtendedClassReturnsStatic()
{
$this->assertInstanceOf(CustomUuid::class, CustomUuid::fromString(self::A_UUID_V4));
}
}

View File

@ -79,7 +79,7 @@ class Ulid extends AbstractUid
base_convert(substr($ulid, 27, 5), 16, 32)
);
return new self(strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ'));
return new static(strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ'));
}
public function toBinary(): string