Skip to content

Commit

Permalink
bug #39900 [Uid] Unable to extend Uuid/Ulid and use fromString() (Osk…
Browse files Browse the repository at this point in the history
…arStark)

This PR was squashed before being merged into the 5.1 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | ---
| License       | MIT
| Doc PR        | -

Before:
```php
CustomUuid::fromString(...) // returns `Uuid`
```

After:
```php
CustomUuid::fromString(...) // returns `CustomUuid`
```

same for `Ulid` class.

cc @fancyweb

Commits
-------

8247779 [Uid] Unable to extend Uuid/Ulid and use fromString()
  • Loading branch information
nicolas-grekas committed Jan 20, 2021
2 parents c639531 + 8247779 commit 64c8a55
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Symfony/Component/Uid/Tests/Fixtures/CustomUlid.php
@@ -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
{
}
18 changes: 18 additions & 0 deletions src/Symfony/Component/Uid/Tests/Fixtures/CustomUuid.php
@@ -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
{
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/Uid/Tests/UlidTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -118,4 +119,9 @@ public function testCompare()
$this->assertLessThan(0, $b->compare($c));
$this->assertGreaterThan(0, $c->compare($b));
}

public function testFromStringOnExtendedClassReturnsStatic()
{
$this->assertInstanceOf(CustomUlid::class, CustomUlid::fromString((new CustomUlid())->toBinary()));
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Expand Up @@ -13,6 +13,7 @@

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;
Expand Down Expand Up @@ -185,4 +186,9 @@ public function testNilUuid()
$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));
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Uid/Ulid.php
Expand Up @@ -79,7 +79,7 @@ public static function fromString(string $ulid): parent
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
Expand Down

0 comments on commit 64c8a55

Please sign in to comment.