From ed8ec33eb1f4e6b08fbbcee912e757ab0cb9b82d Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Fri, 4 Jan 2013 21:58:47 -0600 Subject: [PATCH] Adding hasBigNumber() and is64BitSystem() helper methods --- composer.json | 6 ++-- src/Rhumsaa/Uuid/Uuid.php | 35 +++++++++++++++++++++++ tests/Rhumsaa/Uuid/UuidTest.php | 49 ++++++++++++++++++++++++++++----- 3 files changed, 81 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 59ea75d..e02f57b 100644 --- a/composer.json +++ b/composer.json @@ -23,10 +23,12 @@ "php": ">=5.3.3" }, "require-dev": { - "doctrine/dbal": ">=2.3" + "doctrine/dbal": ">=2.3", + "moontoast/math": "dev-master" }, "suggest": { - "doctrine/dbal": "Allow the use of a UUID as doctrine field type." + "doctrine/dbal": "Allow the use of a UUID as doctrine field type.", + "moontoast/math": "Provides support for large integers on 32-bit platforms." }, "autoload": { "psr-0": {"Rhumsaa\\Uuid": "src/"} diff --git a/src/Rhumsaa/Uuid/Uuid.php b/src/Rhumsaa/Uuid/Uuid.php index 36c9f20..50e98f4 100644 --- a/src/Rhumsaa/Uuid/Uuid.php +++ b/src/Rhumsaa/Uuid/Uuid.php @@ -73,6 +73,21 @@ class Uuid */ const RESERVED_FUTURE = 7; + /** + * 64-bit system override; if true, treat the system as 32-bit (for testing) + * + * @var bool + */ + public static $force32Bit = false; + + /** + * Moontoast\Math\BigNumber override; if true, treat as if BigNumber is + * not available (for testing) + * + * @var bool + */ + public static $forceNoBigNumber = false; + /** * System override to ignore generating node from hardware (for testing) * @@ -761,6 +776,26 @@ class Uuid return $node; } + /** + * Returns true if the system has Moontoast\Math\BigNumber + * + * @return bool + */ + protected static function hasBigNumber() + { + return (class_exists('Moontoast\Math\BigNumber') && !self::$forceNoBigNumber); + } + + /** + * Returns true if the system is 64-bit, false otherwise + * + * @return bool + */ + protected static function is64BitSystem() + { + return (PHP_INT_SIZE == 8 && !self::$force32Bit); + } + /** * Returns a version 3 or 5 UUID based on the hash (md5 or sha1) of a * namespace identifier (which is a UUID) and a name (which is a string) diff --git a/tests/Rhumsaa/Uuid/UuidTest.php b/tests/Rhumsaa/Uuid/UuidTest.php index 788dcb3..d027edc 100644 --- a/tests/Rhumsaa/Uuid/UuidTest.php +++ b/tests/Rhumsaa/Uuid/UuidTest.php @@ -7,13 +7,8 @@ class UuidTest extends TestCase { protected function setUp() { - // Skip these tests if run on a 32-bit build of PHP - if (PHP_INT_SIZE == 4) { - $this->markTestSkipped( - 'Running tests on a 32-bit build of PHP; 64-bit build required.' - ); - } - + Uuid::$force32Bit = false; + Uuid::$forceNoBigNumber = false; Uuid::$ignoreSystemNode = false; } @@ -702,6 +697,46 @@ class UuidTest extends TestCase $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)); + } } /**