mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-14 15:56:48 +03:00
a252f29847
As documented in https://wiki.php.net/rfc/use_global_elements, the engine (by default) does a local namespace lookup, then falls back to global namespace when first calling a global function referenced in namespaced code, unless that function is referenced via fully qualified name (FQN). By using the FQN, the actual symbol can be looked up at compile-time, both by the PHP engine and by static analysis tooling, allowing for compiler (in particular) optimizations to replace known hot-path functions with specialized opcodes. Sadly, no actual benchmark at hand: the improvements can be minimal or massive, depending on where this library is used (tight loops being most relevant).
102 lines
3.0 KiB
PHP
102 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Ramsey\Uuid\Test\Encoder;
|
|
|
|
use Mockery;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use Ramsey\Uuid\Builder\UuidBuilderInterface;
|
|
use Ramsey\Uuid\Codec\CodecInterface;
|
|
use Ramsey\Uuid\Codec\TimestampLastCombCodec;
|
|
use Ramsey\Uuid\Rfc4122\Fields;
|
|
use Ramsey\Uuid\Rfc4122\FieldsInterface;
|
|
use Ramsey\Uuid\Test\TestCase;
|
|
use Ramsey\Uuid\UuidInterface;
|
|
|
|
use function hex2bin;
|
|
use function implode;
|
|
|
|
class TimestampLastCombCodecTest extends TestCase
|
|
{
|
|
/**
|
|
* @var CodecInterface
|
|
*/
|
|
private $codec;
|
|
|
|
/**
|
|
* @var MockObject & UuidBuilderInterface
|
|
*/
|
|
private $builderMock;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->builderMock = $this->getMockBuilder(UuidBuilderInterface::class)->getMock();
|
|
$this->codec = new TimestampLastCombCodec($this->builderMock);
|
|
}
|
|
|
|
public function testEncoding(): void
|
|
{
|
|
$fields = new Fields((string) hex2bin('0800200c9a6611e19b21ff6f8cb0c57d'));
|
|
|
|
$uuidMock = Mockery::mock(UuidInterface::class, [
|
|
'getFields' => $fields,
|
|
]);
|
|
|
|
$encodedUuid = $this->codec->encode($uuidMock);
|
|
|
|
$this->assertSame('0800200c-9a66-11e1-9b21-ff6f8cb0c57d', $encodedUuid);
|
|
}
|
|
|
|
public function testBinaryEncoding(): void
|
|
{
|
|
$fields = Mockery::mock(FieldsInterface::class, [
|
|
'getBytes' => hex2bin('0800200c9a6611e19b21ff6f8cb0c57d'),
|
|
]);
|
|
|
|
/** @var MockObject & UuidInterface $uuidMock */
|
|
$uuidMock = $this->getMockBuilder(UuidInterface::class)->getMock();
|
|
$uuidMock->expects($this->any())->method('getFields')->willReturn($fields);
|
|
|
|
$encodedUuid = $this->codec->encodeBinary($uuidMock);
|
|
|
|
$this->assertSame(hex2bin('0800200c9a6611e19b21ff6f8cb0c57d'), $encodedUuid);
|
|
}
|
|
|
|
public function testDecoding(): void
|
|
{
|
|
$this->builderMock->expects($this->exactly(1))
|
|
->method('build')
|
|
->with(
|
|
$this->codec,
|
|
hex2bin(implode('', [
|
|
'time_low' => '0800200c',
|
|
'time_mid' => '9a66',
|
|
'time_hi_and_version' => '11e1',
|
|
'clock_seq_hi_and_reserved' => '9b',
|
|
'clock_seq_low' => '21',
|
|
'node' => 'ff6f8cb0c57d',
|
|
]))
|
|
);
|
|
$this->codec->decode('0800200c-9a66-11e1-9b21-ff6f8cb0c57d');
|
|
}
|
|
|
|
public function testBinaryDecoding(): void
|
|
{
|
|
$this->builderMock->expects($this->exactly(1))
|
|
->method('build')
|
|
->with(
|
|
$this->codec,
|
|
hex2bin(implode('', [
|
|
'time_low' => '0800200c',
|
|
'time_mid' => '9a66',
|
|
'time_hi_and_version' => '11e1',
|
|
'clock_seq_hi_and_reserved' => '9b',
|
|
'clock_seq_low' => '21',
|
|
'node' => 'ff6f8cb0c57d',
|
|
]))
|
|
);
|
|
$this->codec->decodeBytes((string) hex2bin('0800200c9a6611e19b21ff6f8cb0c57d'));
|
|
}
|
|
}
|