mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-14 15:56:48 +03:00
772 lines
25 KiB
PHP
772 lines
25 KiB
PHP
<?php
|
|
namespace Rhumsaa\Uuid;
|
|
|
|
require 'TestCase.php';
|
|
|
|
class UuidTest extends TestCase
|
|
{
|
|
protected function setUp()
|
|
{
|
|
Uuid::$force32Bit = false;
|
|
Uuid::$forceNoBigNumber = false;
|
|
Uuid::$ignoreSystemNode = false;
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::fromString
|
|
* @covers Rhumsaa\Uuid\Uuid::__construct
|
|
*/
|
|
public function testFromString()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
|
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::fromString
|
|
*/
|
|
public function testFromStringWithCurlyBraces()
|
|
{
|
|
$uuid = Uuid::fromString('{ff6f8cb0-c57d-11e1-9b21-0800200c9a66}');
|
|
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
|
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::fromString
|
|
* @expectedException InvalidArgumentException
|
|
* @expectedExceptionMessage Invalid UUID string:
|
|
*/
|
|
public function testFromStringWithInvalidUuidString()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21');
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::fromString
|
|
*/
|
|
public function testFromStringWithUrn()
|
|
{
|
|
$uuid = Uuid::fromString('urn:uuid:ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
|
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getBytes
|
|
*/
|
|
public function testGetBytes()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(16, strlen($uuid->getBytes()));
|
|
$this->assertEquals('/2+MsMV9EeGbIQgAIAyaZg==', base64_encode($uuid->getBytes()));
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getClockSeqHiAndReserved
|
|
*/
|
|
public function testGetClockSeqHiAndReserved()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(155, $uuid->getClockSeqHiAndReserved());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getClockSeqHiAndReservedHex
|
|
*/
|
|
public function testGetClockSeqHiAndReservedHex()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('9b', $uuid->getClockSeqHiAndReservedHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getClockSeqLow
|
|
*/
|
|
public function testGetClockSeqLow()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(33, $uuid->getClockSeqLow());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getClockSeqLowHex
|
|
*/
|
|
public function testGetClockSeqLowHex()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('21', $uuid->getClockSeqLowHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getClockSequence
|
|
*/
|
|
public function testGetClockSequence()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(6945, $uuid->getClockSequence());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getClockSequenceHex
|
|
*/
|
|
public function testGetClockSequenceHex()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('1b21', $uuid->getClockSequenceHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getDateTime
|
|
*/
|
|
public function testGetDateTime()
|
|
{
|
|
// Check a recent date
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
|
$this->assertEquals('Wed, 04 Jul 2012 02:14:34 +0000', $uuid->getDateTime()->format('r'));
|
|
|
|
// Check an old date
|
|
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
|
|
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
|
$this->assertEquals('Sun, 16 Oct 1582 16:34:04 +0000', $uuid->getDateTime()->format('r'));
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getDateTime
|
|
* @covers Rhumsaa\Uuid\UnsupportedOperationException
|
|
* @expectedException Rhumsaa\Uuid\UnsupportedOperationException
|
|
* @expectedExceptionMessage Not a time-based UUID
|
|
*/
|
|
public function testGetDateTimeFromNonVersion1Uuid()
|
|
{
|
|
// Using a version 4 UUID to test
|
|
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
|
|
$date = $uuid->getDateTime();
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getFields
|
|
*/
|
|
public function testGetFields()
|
|
{
|
|
$this->skip64BitTest();
|
|
|
|
$fields = array(
|
|
'time_low' => 4285500592,
|
|
'time_mid' => 50557,
|
|
'time_hi_and_version' => 4577,
|
|
'clock_seq_hi_and_reserved' => 155,
|
|
'clock_seq_low' => 33,
|
|
'node' => 8796630719078,
|
|
);
|
|
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
|
|
$this->assertEquals($fields, $uuid->getFields());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getFieldsHex
|
|
*/
|
|
public function testGetFieldsHex()
|
|
{
|
|
$fields = array(
|
|
'time_low' => 'ff6f8cb0',
|
|
'time_mid' => 'c57d',
|
|
'time_hi_and_version' => '11e1',
|
|
'clock_seq_hi_and_reserved' => '9b',
|
|
'clock_seq_low' => '21',
|
|
'node' => '0800200c9a66',
|
|
);
|
|
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
|
|
$this->assertEquals($fields, $uuid->getFieldsHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getLeastSignificantBits
|
|
*/
|
|
public function testGetLeastSignificantBits()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(-7268519526968550810, $uuid->getLeastSignificantBits());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getLeastSignificantBitsHex
|
|
*/
|
|
public function testGetLeastSignificantBitsHex()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('9b210800200c9a66', $uuid->getLeastSignificantBitsHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getMostSignificantBits
|
|
*/
|
|
public function testGetMostSignificantBits()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(-40659180767604255, $uuid->getMostSignificantBits());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getMostSignificantBitsHex
|
|
*/
|
|
public function testGetMostSignificantBitsHex()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('ff6f8cb0c57d11e1', $uuid->getMostSignificantBitsHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getNode
|
|
*/
|
|
public function testGetNode()
|
|
{
|
|
$this->skip64BitTest();
|
|
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(8796630719078, $uuid->getNode());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getNodeHex
|
|
*/
|
|
public function testGetNodeHex()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('0800200c9a66', $uuid->getNodeHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimeHiAndVersion
|
|
*/
|
|
public function testGetTimeHiAndVersion()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(4577, $uuid->getTimeHiAndVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimeHiAndVersionHex
|
|
*/
|
|
public function testGetTimeHiAndVersionHex()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('11e1', $uuid->getTimeHiAndVersionHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimeLow
|
|
*/
|
|
public function testGetTimeLow()
|
|
{
|
|
$this->skip64BitTest();
|
|
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(4285500592, $uuid->getTimeLow());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimeLowHex
|
|
*/
|
|
public function testGetTimeLowHex()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('ff6f8cb0', $uuid->getTimeLowHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimeMid
|
|
*/
|
|
public function testGetTimeMid()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(50557, $uuid->getTimeMid());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimeMidHex
|
|
*/
|
|
public function testGetTimeMidHex()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('c57d', $uuid->getTimeMidHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimestamp
|
|
*/
|
|
public function testGetTimestamp()
|
|
{
|
|
$this->skip64BitTest();
|
|
|
|
// Check for a recent date
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(135606608744910000, $uuid->getTimestamp());
|
|
|
|
// Check for an old date
|
|
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
|
|
$this->assertEquals(1460440000000, $uuid->getTimestamp());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimestampHex
|
|
*/
|
|
public function testGetTimestampHex()
|
|
{
|
|
// Check for a recent date
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('1e1c57dff6f8cb0', $uuid->getTimestampHex());
|
|
|
|
// Check for an old date
|
|
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
|
|
$this->assertEquals('00001540901e600', $uuid->getTimestampHex());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimestamp
|
|
* @covers Rhumsaa\Uuid\UnsupportedOperationException
|
|
* @expectedException Rhumsaa\Uuid\UnsupportedOperationException
|
|
* @expectedExceptionMessage Not a time-based UUID
|
|
*/
|
|
public function testGetTimestampFromNonVersion1Uuid()
|
|
{
|
|
// Using a version 4 UUID to test
|
|
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
|
|
$ts = $uuid->getTimestamp();
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getTimestampHex
|
|
* @covers Rhumsaa\Uuid\UnsupportedOperationException
|
|
* @expectedException Rhumsaa\Uuid\UnsupportedOperationException
|
|
* @expectedExceptionMessage Not a time-based UUID
|
|
*/
|
|
public function testGetTimestampHexFromNonVersion1Uuid()
|
|
{
|
|
// Using a version 4 UUID to test
|
|
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
|
|
$ts = $uuid->getTimestampHex();
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getUrn
|
|
*/
|
|
public function testGetUrn()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('urn:uuid:ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->getUrn());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getVariant
|
|
*/
|
|
public function testGetVariantForReservedNcs()
|
|
{
|
|
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-0b21-0800200c9a66');
|
|
$this->assertEquals(0, $uuid1->getVariant());
|
|
|
|
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-1b21-0800200c9a66');
|
|
$this->assertEquals(0, $uuid2->getVariant());
|
|
|
|
$uuid3 = Uuid::fromString('ff6f8cb0-c57d-11e1-2b21-0800200c9a66');
|
|
$this->assertEquals(0, $uuid3->getVariant());
|
|
|
|
$uuid4 = Uuid::fromString('ff6f8cb0-c57d-11e1-3b21-0800200c9a66');
|
|
$this->assertEquals(0, $uuid4->getVariant());
|
|
|
|
$uuid5 = Uuid::fromString('ff6f8cb0-c57d-11e1-4b21-0800200c9a66');
|
|
$this->assertEquals(0, $uuid5->getVariant());
|
|
|
|
$uuid6 = Uuid::fromString('ff6f8cb0-c57d-11e1-5b21-0800200c9a66');
|
|
$this->assertEquals(0, $uuid6->getVariant());
|
|
|
|
$uuid7 = Uuid::fromString('ff6f8cb0-c57d-11e1-6b21-0800200c9a66');
|
|
$this->assertEquals(0, $uuid7->getVariant());
|
|
|
|
$uuid8 = Uuid::fromString('ff6f8cb0-c57d-11e1-7b21-0800200c9a66');
|
|
$this->assertEquals(0, $uuid8->getVariant());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getVariant
|
|
*/
|
|
public function testGetVariantForRfc4122()
|
|
{
|
|
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-8b21-0800200c9a66');
|
|
$this->assertEquals(2, $uuid1->getVariant());
|
|
|
|
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(2, $uuid2->getVariant());
|
|
|
|
$uuid3 = Uuid::fromString('ff6f8cb0-c57d-11e1-ab21-0800200c9a66');
|
|
$this->assertEquals(2, $uuid3->getVariant());
|
|
|
|
$uuid4 = Uuid::fromString('ff6f8cb0-c57d-11e1-bb21-0800200c9a66');
|
|
$this->assertEquals(2, $uuid4->getVariant());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getVariant
|
|
*/
|
|
public function testGetVariantForReservedMicrosoft()
|
|
{
|
|
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-cb21-0800200c9a66');
|
|
$this->assertEquals(6, $uuid1->getVariant());
|
|
|
|
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-db21-0800200c9a66');
|
|
$this->assertEquals(6, $uuid2->getVariant());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getVariant
|
|
*/
|
|
public function testGetVariantForReservedFuture()
|
|
{
|
|
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-eb21-0800200c9a66');
|
|
$this->assertEquals(7, $uuid1->getVariant());
|
|
|
|
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-fb21-0800200c9a66');
|
|
$this->assertEquals(7, $uuid2->getVariant());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
|
*/
|
|
public function testGetVersionForVersion1()
|
|
{
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals(1, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
|
*/
|
|
public function testGetVersionForVersion2()
|
|
{
|
|
$uuid = Uuid::fromString('6fa459ea-ee8a-2ca4-894e-db77e160355e');
|
|
$this->assertEquals(2, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
|
*/
|
|
public function testGetVersionForVersion3()
|
|
{
|
|
$uuid = Uuid::fromString('6fa459ea-ee8a-3ca4-894e-db77e160355e');
|
|
$this->assertEquals(3, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
|
*/
|
|
public function testGetVersionForVersion4()
|
|
{
|
|
$uuid = Uuid::fromString('6fabf0bc-603a-42f2-925b-d9f779bd0032');
|
|
$this->assertEquals(4, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
|
*/
|
|
public function testGetVersionForVersion5()
|
|
{
|
|
$uuid = Uuid::fromString('886313e1-3b8a-5372-9b90-0c9aee199e5d');
|
|
$this->assertEquals(5, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::toString
|
|
* @covers Rhumsaa\Uuid\Uuid::__toString
|
|
*/
|
|
public function testToString()
|
|
{
|
|
// Check with a recent date
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
|
|
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', sprintf('%s', $uuid));
|
|
|
|
// Check with an old date
|
|
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
|
|
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', $uuid->toString());
|
|
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', sprintf('%s', $uuid));
|
|
}
|
|
|
|
/**
|
|
* This calls php_uname() in getNodeFromSystem. The first time it is
|
|
* called, it returns "WIN." Each additional times, it returns the
|
|
* normal system php_uname().
|
|
*
|
|
* See the bottom of this test file to see where we are overriding
|
|
* php_uname() for the purpose of this test.
|
|
*
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid1
|
|
* @covers Rhumsaa\Uuid\Uuid::getNodeFromSystem
|
|
*/
|
|
public function testUuid1CoverageForWindows()
|
|
{
|
|
$uuid = Uuid::uuid1();
|
|
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
|
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(1, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid1
|
|
* @covers Rhumsaa\Uuid\Uuid::getNodeFromSystem
|
|
*/
|
|
public function testUuid1()
|
|
{
|
|
$uuid = Uuid::uuid1();
|
|
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
|
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(1, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid1
|
|
*/
|
|
public function testUuid1WithNodeAndClockSequence()
|
|
{
|
|
$this->skip64BitTest();
|
|
|
|
$uuid = Uuid::uuid1(0x0800200c9a66, 0x1669);
|
|
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
|
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(1, $uuid->getVersion());
|
|
$this->assertEquals(5737, $uuid->getClockSequence());
|
|
$this->assertEquals(8796630719078, $uuid->getNode());
|
|
$this->assertEquals('9669-0800200c9a66', substr($uuid->toString(), 19));
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid1
|
|
*/
|
|
public function testUuid1WithNodeAndClockSequence32Bit()
|
|
{
|
|
$uuid = Uuid::uuid1(0x7fffffff, 0x1669);
|
|
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
|
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(1, $uuid->getVersion());
|
|
$this->assertEquals(5737, $uuid->getClockSequence());
|
|
$this->assertEquals(2147483647, $uuid->getNode());
|
|
$this->assertEquals('9669-00007fffffff', substr($uuid->toString(), 19));
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid1
|
|
*/
|
|
public function testUuid1WithRandomNode()
|
|
{
|
|
Uuid::$ignoreSystemNode = true;
|
|
|
|
$uuid = Uuid::uuid1();
|
|
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
|
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(1, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* The "python.org" UUID is a known entity, so we're testing that this
|
|
* library generates a matching UUID for the same name.
|
|
* @see http://docs.python.org/library/uuid.html
|
|
*
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid3
|
|
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
|
|
*/
|
|
public function testUuid3WithNamespaceAsUuidObject()
|
|
{
|
|
$nsUuid = Uuid::fromString(Uuid::NAMESPACE_DNS);
|
|
$uuid = Uuid::uuid3($nsUuid, 'python.org');
|
|
$this->assertEquals('6fa459ea-ee8a-3ca4-894e-db77e160355e', $uuid->toString());
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(3, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* The "python.org" UUID is a known entity, so we're testing that this
|
|
* library generates a matching UUID for the same name.
|
|
* @see http://docs.python.org/library/uuid.html
|
|
*
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid3
|
|
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
|
|
*/
|
|
public function testUuid3WithNamespaceAsUuidString()
|
|
{
|
|
$uuid = Uuid::uuid3(Uuid::NAMESPACE_DNS, 'python.org');
|
|
$this->assertEquals('6fa459ea-ee8a-3ca4-894e-db77e160355e', $uuid->toString());
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(3, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid4
|
|
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
|
|
*/
|
|
public function testUuid4()
|
|
{
|
|
$uuid = Uuid::uuid4();
|
|
$this->assertInstanceOf('Rhumsaa\Uuid\Uuid', $uuid);
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(4, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* The "python.org" UUID is a known entity, so we're testing that this
|
|
* library generates a matching UUID for the same name.
|
|
* @see http://docs.python.org/library/uuid.html
|
|
*
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid5
|
|
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
|
|
*/
|
|
public function testUuid5WithNamespaceAsUuidObject()
|
|
{
|
|
$nsUuid = Uuid::fromString(Uuid::NAMESPACE_DNS);
|
|
$uuid = Uuid::uuid5($nsUuid, 'python.org');
|
|
$this->assertEquals('886313e1-3b8a-5372-9b90-0c9aee199e5d', $uuid->toString());
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(5, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* The "python.org" UUID is a known entity, so we're testing that this
|
|
* library generates a matching UUID for the same name.
|
|
* @see http://docs.python.org/library/uuid.html
|
|
*
|
|
* @covers Rhumsaa\Uuid\Uuid::uuid5
|
|
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
|
|
*/
|
|
public function testUuid5WithNamespaceAsUuidString()
|
|
{
|
|
$uuid = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'python.org');
|
|
$this->assertEquals('886313e1-3b8a-5372-9b90-0c9aee199e5d', $uuid->toString());
|
|
$this->assertEquals(2, $uuid->getVariant());
|
|
$this->assertEquals(5, $uuid->getVersion());
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::compareTo
|
|
*/
|
|
public function testCompareTo()
|
|
{
|
|
// $uuid1 and $uuid2 are identical
|
|
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
|
|
// The next three UUIDs are used for comparing msb and lsb in
|
|
// the compareTo() method
|
|
|
|
// msb are less than $uuid4, lsb are greater than $uuid5
|
|
$uuid3 = Uuid::fromString('44cca71e-d13d-11e1-a959-c8bcc8a476f4');
|
|
|
|
// msb are greater than $uuid3, lsb are equal to those in $uuid3
|
|
$uuid4 = Uuid::fromString('44cca71e-d13d-11e2-a959-c8bcc8a476f4');
|
|
|
|
// msb are equal to those in $uuid3, lsb are less than in $uuid3
|
|
$uuid5 = Uuid::fromString('44cca71e-d13d-11e1-a959-c8bcc8a476f3');
|
|
|
|
$this->assertEquals(0, $uuid1->compareTo($uuid2));
|
|
$this->assertEquals(0, $uuid2->compareTo($uuid1));
|
|
$this->assertEquals(-1, $uuid3->compareTo($uuid4));
|
|
$this->assertEquals(1, $uuid4->compareTo($uuid3));
|
|
$this->assertEquals(-1, $uuid5->compareTo($uuid3));
|
|
$this->assertEquals(1, $uuid3->compareto($uuid5));
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::equals
|
|
*/
|
|
public function testEquals()
|
|
{
|
|
$uuid1 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'python.org');
|
|
$uuid2 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'python.org');
|
|
$uuid3 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net');
|
|
|
|
$this->assertTrue($uuid1->equals($uuid2));
|
|
$this->assertFalse($uuid1->equals($uuid3));
|
|
$this->assertFalse($uuid1->equals(null));
|
|
$this->assertFalse($uuid1->equals(new \stdClass()));
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::hasBigNumber
|
|
*/
|
|
public function testHasBigNumber()
|
|
{
|
|
$hasBigNumber = new \ReflectionMethod(
|
|
'Rhumsaa\Uuid\Uuid', 'hasBigNumber'
|
|
);
|
|
$hasBigNumber->setAccessible(true);
|
|
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
|
|
$this->assertTrue($hasBigNumber->invoke($uuid));
|
|
|
|
Uuid::$forceNoBigNumber = true;
|
|
$this->assertFalse($hasBigNumber->invoke($uuid));
|
|
}
|
|
|
|
/**
|
|
* @covers Rhumsaa\Uuid\Uuid::is64BitSystem
|
|
*/
|
|
public function testIs64BitSystem()
|
|
{
|
|
$is64BitSystem = new \ReflectionMethod(
|
|
'Rhumsaa\Uuid\Uuid', 'is64BitSystem'
|
|
);
|
|
$is64BitSystem->setAccessible(true);
|
|
|
|
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
|
|
|
if (PHP_INT_SIZE == 8) {
|
|
$this->assertTrue($is64BitSystem->invoke($uuid));
|
|
} else {
|
|
$this->assertFalse($is64BitSystem->invoke($uuid));
|
|
}
|
|
|
|
Uuid::$force32Bit = true;
|
|
$this->assertFalse($is64BitSystem->invoke($uuid));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Overriding the default php_uname() for the purpose of this test so we can
|
|
* have full code coverage on all the lines.
|
|
*
|
|
* This should ensure that we have full code coverage whether the tests are run
|
|
* on Windows or a *NIX environment. This does not mean that the code has been
|
|
* tested for both environments if it is only tested in one environment, so
|
|
* the tests must still be run in both environments to ensure they pass.
|
|
*/
|
|
function php_uname($v)
|
|
{
|
|
static $run = 0;
|
|
|
|
if ($run == 0 && strtoupper(substr(\php_uname('a'), 0, 3)) != 'WIN') {
|
|
// If this is the first run and the SUT is not Windows, return "Windows"
|
|
$ret = 'Windows';
|
|
} elseif ($run == 0 && strtoupper(substr(\php_uname('a'), 0, 3)) == 'WIN') {
|
|
// If this is the first run and the SUT is Windows, return "Darwin"
|
|
$ret = 'Darwin';
|
|
} else {
|
|
// If this isn't the first run, then use the system php_uname()
|
|
$ret = \php_uname($v);
|
|
}
|
|
|
|
$run++;
|
|
|
|
return $ret;
|
|
}
|
|
|
|
|