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).
81 lines
2.8 KiB
PHP
81 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Ramsey\Uuid\Test\Nonstandard;
|
|
|
|
use Ramsey\Uuid\Exception\InvalidArgumentException;
|
|
use Ramsey\Uuid\Nonstandard\Fields;
|
|
use Ramsey\Uuid\Test\TestCase;
|
|
use Ramsey\Uuid\Type\Hexadecimal;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use function hex2bin;
|
|
use function serialize;
|
|
use function str_replace;
|
|
use function unserialize;
|
|
|
|
class FieldsTest extends TestCase
|
|
{
|
|
public function testConstructorThrowsExceptionIfNotSixteenByteString(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->expectExceptionMessage(
|
|
'The byte string must be 16 bytes long; received 6 bytes'
|
|
);
|
|
|
|
new Fields('foobar');
|
|
}
|
|
|
|
/**
|
|
* @param string|int $expectedValue
|
|
*
|
|
* @dataProvider fieldGetterMethodProvider
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint
|
|
*/
|
|
public function testFieldGetterMethods(string $uuid, string $methodName, $expectedValue): void
|
|
{
|
|
$bytes = (string) hex2bin(str_replace('-', '', $uuid));
|
|
$fields = new Fields($bytes);
|
|
|
|
$result = $fields->$methodName();
|
|
|
|
if ($result instanceof Hexadecimal) {
|
|
$this->assertSame($expectedValue, $result->toString());
|
|
} else {
|
|
$this->assertSame($expectedValue, $result);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
|
|
*/
|
|
public function fieldGetterMethodProvider(): array
|
|
{
|
|
return [
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getClockSeq', '0b21'],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getClockSeqHiAndReserved', '0b'],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getClockSeqLow', '21'],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getNode', '0800200c9a66'],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimeHiAndVersion', '91e1'],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimeLow', 'ff6f8cb0'],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimeMid', 'c57d'],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getVariant', Uuid::RESERVED_NCS],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getVersion', null],
|
|
['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'isNil', false],
|
|
];
|
|
}
|
|
|
|
public function testSerializingFields(): void
|
|
{
|
|
$bytes = (string) hex2bin(str_replace('-', '', 'ff6f8cb0-c57d-91e1-0b21-0800200c9a66'));
|
|
$fields = new Fields($bytes);
|
|
|
|
$serializedFields = serialize($fields);
|
|
$unserializedFields = unserialize($serializedFields);
|
|
|
|
$this->assertEquals($fields, $unserializedFields);
|
|
}
|
|
}
|