├── .gitignore ├── .travis.yml ├── composer.json ├── phpunit.xml ├── readme.md ├── src └── Webpatser │ └── Uuid │ ├── Uuid.php │ └── UuidServiceProvider.php └── tests ├── Webpatser └── Uuid │ └── UuidTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | clover.xml 6 | .idea 7 | .phpunit.result.cache 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | matrix: 4 | fast_finish: true 5 | include: 6 | - php: 7.3 7 | - php: 7.4 8 | - php: 8.0 9 | 10 | sudo: false 11 | 12 | before_install: 13 | - travis_retry composer self-update 14 | 15 | install: 16 | - travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest 17 | 18 | script: vendor/bin/phpunit --coverage-text 19 | 20 | after_success: 21 | - bash <(curl -s https://codecov.io/bash) 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "webpatser/laravel-uuid", 3 | "description" : "Laravel package to generate and to validate a universally unique identifier (UUID) according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUIDs are built-in.", 4 | "authors" : [{ 5 | "name" : "Christoph Kempen", 6 | "email" : "christoph@downsized.nl" 7 | } 8 | ], 9 | "keywords" : [ 10 | "UUID RFC4122" 11 | ], 12 | "homepage" : "https://github.com/webpatser/laravel-uuid", 13 | "license" : [ 14 | "MIT" 15 | ], 16 | "require" : { 17 | "php" : "^7.0|^8.0" 18 | }, 19 | "require-dev" : { 20 | "fakerphp/faker": "^1.9.1", 21 | "phpunit/phpunit": "^9.3.3" 22 | }, 23 | "autoload" : { 24 | "psr-4" : { 25 | "Webpatser\\Uuid\\": "src/Webpatser/Uuid/" 26 | } 27 | }, 28 | "autoload-dev": { 29 | "psr-4": { 30 | "Tests\\": "tests/" 31 | } 32 | }, 33 | "support" : { 34 | "source" : "https://github.com/webpatser/laravel-uuid", 35 | "issues" : "https://github.com/webpatser/laravel-uuid/issues" 36 | }, 37 | "extra": { 38 | "laravel": { 39 | "providers": [ 40 | "Webpatser\\Uuid\\UuidServiceProvider" 41 | ], 42 | "aliases": { 43 | "Uuid": "Webpatser\\Uuid\\Uuid" 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./src 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | ./tests/ 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Laravel Uuid 2 | 3 | [![Total Downloads](https://poser.pugx.org/webpatser/laravel-uuid/downloads.svg)](https://packagist.org/packages/webpatser/laravel-uuid) 4 | [![Build Status](https://secure.travis-ci.org/webpatser/laravel-uuid.png?branch=master)](http://travis-ci.org/webpatser/laravel-uuid) 5 | [![codecov.io](http://codecov.io/github/webpatser/laravel-uuid/coverage.svg?branch=master)](http://codecov.io/github/webpatser/laravel-uuid?branch=master) 6 | [![Latest Stable Version](https://poser.pugx.org/webpatser/laravel-uuid/v/stable.svg)](https://packagist.org/packages/webpatser/laravel-uuid) 7 | [![Licence](https://poser.pugx.org/webpatser/laravel-uuid/license.svg)](https://packagist.org/packages/webpatser/laravel-uuid) 8 | 9 | Laravel package to generate and to validate a universally unique identifier (UUID) according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUIDs are built-in. 10 | 11 | ## What's new in 4.* 12 | Laravel-uuid is now ready for Laravel 8. It has the same requirements so that means PHP 7.3 or PHP 8. Laravel package auto-discovery is enabled, and you can now use the UUID validation. Validation examples are below and in the tests. 13 | 14 | For older Laravel or PHP versions use older versions; see below... 15 | 16 | ## What's new in 3.* 17 | Laravel-uuid is now refactored for Laravel 5.5. It has the same requirements so that means PHP 7. Laravel package auto-discovery is enabled, and you can now use the UUID validation. Validation examples are below and in the tests. 18 | 19 | Laravel 5.0, 5.1, 5.2, 5.3 and 5.4? use [version 2](https://github.com/webpatser/laravel-uuid/tree/2.1.1) 20 | 21 | Laravel 4.*? use [version 1](https://github.com/webpatser/laravel-uuid/tree/1.5) 22 | 23 | ## Installation 24 | 25 | In Laravel 5.5 laravel-uuid will install via the new package discovery feature so you only need to add the package to your composer.json file 26 | 27 | ```shell 28 | composer require "webpatser/laravel-uuid:^3.0" 29 | ``` 30 | 31 | after installation you should see 32 | 33 | ```shell 34 | Discovered Package: webpatser/laravel-uuid 35 | ``` 36 | 37 | and you are ready to go 38 | 39 | ## Basic Usage 40 | 41 | To quickly generate a UUID just do 42 | 43 | ```php 44 | Uuid::generate() 45 | ``` 46 | 47 | This will generate a version 1 Uuid `object` with a random generated MAC address. 48 | 49 | To echo out the generated UUID, cast it to a string 50 | 51 | ```php 52 | (string) Uuid::generate() 53 | ``` 54 | 55 | or 56 | 57 | ```php 58 | Uuid::generate()->string 59 | ``` 60 | 61 | ## Advanced Usage 62 | 63 | ### UUID creation 64 | 65 | Generate a version 1, time-based, UUID. You can set the optional node to the MAC address. If not supplied it will generate a random MAC address. 66 | 67 | ```php 68 | Uuid::generate(1,'00:11:22:33:44:55'); 69 | ``` 70 | 71 | Generate a version 3, name-based using MD5 hashing, UUID 72 | 73 | ```php 74 | Uuid::generate(3,'test', Uuid::NS_DNS); 75 | ``` 76 | 77 | Generate a version 4, truly random, UUID 78 | 79 | ```php 80 | Uuid::generate(4); 81 | ``` 82 | 83 | Generate a version 5, name-based using SHA-1 hashing, UUID 84 | 85 | ```php 86 | Uuid::generate(5,'test', Uuid::NS_DNS); 87 | ``` 88 | 89 | ### Some magic features 90 | 91 | To import a UUID 92 | 93 | ```php 94 | $uuid = Uuid::import('d3d29d70-1d25-11e3-8591-034165a3a613'); 95 | ``` 96 | 97 | Extract the time for a time-based UUID (version 1) 98 | 99 | ```php 100 | $uuid = Uuid::generate(1); 101 | dd($uuid->time); 102 | ``` 103 | 104 | Extract the version of an UUID 105 | 106 | ```php 107 | $uuid = Uuid::generate(4); 108 | dd($uuid->version); 109 | ``` 110 | 111 | ## Eloquent UUID generation 112 | 113 | If you want an UUID magically be generated in your Laravel models, just add this boot method to your Model. 114 | 115 | ```php 116 | /** 117 | * Setup model event hooks 118 | */ 119 | public static function boot() 120 | { 121 | parent::boot(); 122 | self::creating(function ($model) { 123 | $model->uuid = (string) Uuid::generate(4); 124 | }); 125 | } 126 | ``` 127 | This will generate a version 4 UUID when creating a new record. 128 | 129 | ## Model binding to UUID instead of primary key 130 | 131 | If you want to use the UUID in URLs instead of the primary key, you can add this to your model (where 'uuid' is the column name to store the UUID) 132 | 133 | ```php 134 | /** 135 | * Get the route key for the model. 136 | * 137 | * @return string 138 | */ 139 | public function getRouteKeyName() 140 | { 141 | return 'uuid'; 142 | } 143 | ``` 144 | 145 | When you inject the model on your resource controller methods you get the correct record 146 | 147 | ```php 148 | public function edit(Model $model) 149 | { 150 | return view('someview.edit')->with([ 151 | 'model' => $model, 152 | ]); 153 | } 154 | ``` 155 | 156 | ## Validation 157 | 158 | Just use like any other Laravel validator. 159 | 160 | ``'uuid-field' => 'uuid'`` 161 | 162 | Or create a validator from scratch. In the example an Uuid object in validated. You can also validate strings `$uuid->string`, the URN `$uuid->urn` or the binary value `$uuid->bytes` 163 | 164 | ```php 165 | $uuid = Uuid::generate(); 166 | $validator = Validator::make(['uuid' => $uuid], ['uuid' => 'uuid']); 167 | dd($validator->passes()); 168 | ``` 169 | 170 | ## Notes 171 | 172 | Full details on the UUID specification can be found on [http://tools.ietf.org/html/rfc4122](http://tools.ietf.org/html/rfc4122). 173 | -------------------------------------------------------------------------------- /src/Webpatser/Uuid/Uuid.php: -------------------------------------------------------------------------------- 1 | bytes = $uuid; 132 | 133 | // Optimize the most common use 134 | $this->string = bin2hex(substr($uuid, 0, 4)) . "-" . 135 | bin2hex(substr($uuid, 4, 2)) . "-" . 136 | bin2hex(substr($uuid, 6, 2)) . "-" . 137 | bin2hex(substr($uuid, 8, 2)) . "-" . 138 | bin2hex(substr($uuid, 10, 6)); 139 | 140 | // Store UUID in an optimized way 141 | $this->uuid_ordered = bin2hex(substr($uuid, 6, 2)) . 142 | bin2hex(substr($uuid, 4, 2)) . 143 | bin2hex(substr($uuid, 0, 4)); 144 | } 145 | 146 | 147 | /** 148 | * @param int $ver 149 | * @param string $node 150 | * @param string $ns 151 | * @return Uuid 152 | * @throws Exception 153 | */ 154 | public static function generate($ver = 1, $node = null, $ns = null) 155 | { 156 | /* Create a new UUID based on provided data. */ 157 | switch ((int)$ver) { 158 | case 1: 159 | return new static(static::mintTime($node)); 160 | case 2: 161 | // Version 2 is not supported 162 | throw new Exception('Version 2 is unsupported.'); 163 | case 3: 164 | return new static(static::mintName(static::MD5, $node, $ns)); 165 | case 4: 166 | return new static(static::mintRand()); 167 | case 5: 168 | return new static(static::mintName(static::SHA1, $node, $ns)); 169 | default: 170 | throw new Exception('Selected version is invalid or unsupported.'); 171 | } 172 | } 173 | 174 | /** 175 | * Generates a Version 1 UUID. 176 | * These are derived from the time at which they were generated. 177 | * 178 | * @param string $node 179 | * @return string 180 | */ 181 | protected static function mintTime($node = null) 182 | { 183 | 184 | /** Get time since Gregorian calendar reform in 100ns intervals 185 | * This is exceedingly difficult because of PHP's (and pack()'s) 186 | * integer size limits. 187 | * Note that this will never be more accurate than to the microsecond. 188 | */ 189 | $time = microtime(1) * 10000000 + static::INTERVAL; 190 | 191 | // Convert to a string representation 192 | $time = sprintf("%F", $time); 193 | 194 | //strip decimal point 195 | preg_match("/^\d+/", $time, $time); 196 | 197 | // And now to a 64-bit binary representation 198 | $time = base_convert($time[0], 10, 16); 199 | $time = pack("H*", str_pad($time, 16, "0", STR_PAD_LEFT)); 200 | 201 | // Reorder bytes to their proper locations in the UUID 202 | $uuid = $time[4] . $time[5] . $time[6] . $time[7] . $time[2] . $time[3] . $time[0] . $time[1]; 203 | 204 | // Generate a random clock sequence 205 | $uuid .= static::randomBytes(2); 206 | 207 | // set variant 208 | $uuid[8] = chr(ord($uuid[8]) & static::CLEAR_VAR | static::VAR_RFC); 209 | 210 | // set version 211 | $uuid[6] = chr(ord($uuid[6]) & static::CLEAR_VER | static::VERSION_1); 212 | 213 | // Set the final 'node' parameter, a MAC address 214 | if (!is_null($node)) { 215 | $node = static::makeBin($node, 6); 216 | } 217 | 218 | // If no node was provided or if the node was invalid, 219 | // generate a random MAC address and set the multicast bit 220 | if (is_null($node)) { 221 | $node = static::randomBytes(6); 222 | $node[0] = pack("C", ord($node[0]) | 1); 223 | } 224 | 225 | $uuid .= $node; 226 | 227 | return $uuid; 228 | } 229 | 230 | /** 231 | * Randomness is returned as a string of bytes 232 | * 233 | * @param $bytes 234 | * @return string 235 | */ 236 | public static function randomBytes($bytes) 237 | { 238 | return random_bytes($bytes); 239 | } 240 | 241 | /** 242 | * Insure that an input string is either binary or hexadecimal. 243 | * Returns binary representation, or false on failure. 244 | * 245 | * @param string $str 246 | * @param integer $len 247 | * @return string|null 248 | */ 249 | protected static function makeBin($str, $len) 250 | { 251 | if ($str instanceof self) { 252 | return $str->bytes; 253 | } 254 | if (strlen($str) === $len) { 255 | return $str; 256 | } else { 257 | // strip URN scheme and namespace 258 | $str = preg_replace('/^urn:uuid:/is', '', $str); 259 | } 260 | // strip non-hex characters 261 | $str = preg_replace('/[^a-f0-9]/is', '', $str); 262 | if (strlen($str) !== ($len * 2)) { 263 | return null; 264 | } else { 265 | return pack("H*", $str); 266 | } 267 | } 268 | 269 | /** 270 | * Generates a Version 3 or Version 5 UUID. 271 | * These are derived from a hash of a name and its namespace, in binary form. 272 | * 273 | * @param string $ver 274 | * @param string $node 275 | * @param string $ns 276 | * @return string 277 | * @throws Exception 278 | */ 279 | protected static function mintName($ver, $node, $ns) 280 | { 281 | if (empty($node)) { 282 | throw new Exception('A name-string is required for Version 3 or 5 UUIDs.'); 283 | } 284 | 285 | // if the namespace UUID isn't binary, make it so 286 | $ns = static::makeBin($ns, 16); 287 | if (is_null($ns)) { 288 | throw new Exception('A binary namespace is required for Version 3 or 5 UUIDs.'); 289 | } 290 | 291 | $version = null; 292 | $uuid = null; 293 | 294 | switch ($ver) { 295 | case static::MD5: 296 | $version = static::VERSION_3; 297 | $uuid = md5($ns . $node, 1); 298 | break; 299 | case static::SHA1: 300 | $version = static::VERSION_5; 301 | $uuid = substr(sha1($ns . $node, 1), 0, 16); 302 | break; 303 | default: 304 | // no default really required here 305 | } 306 | 307 | // set variant 308 | $uuid[8] = chr(ord($uuid[8]) & static::CLEAR_VAR | static::VAR_RFC); 309 | 310 | // set version 311 | $uuid[6] = chr(ord($uuid[6]) & static::CLEAR_VER | $version); 312 | 313 | return ($uuid); 314 | } 315 | 316 | /** 317 | * Generate a Version 4 UUID. 318 | * These are derived solely from random numbers. 319 | * generate random fields 320 | * 321 | * @return string 322 | */ 323 | protected static function mintRand() 324 | { 325 | $uuid = static::randomBytes(16); 326 | // set variant 327 | $uuid[8] = chr(ord($uuid[8]) & static::CLEAR_VAR | static::VAR_RFC); 328 | // set version 329 | $uuid[6] = chr(ord($uuid[6]) & static::CLEAR_VER | static::VERSION_4); 330 | 331 | return $uuid; 332 | } 333 | 334 | /** 335 | * Import an existing UUID 336 | * 337 | * @param string $uuid 338 | * @return Uuid 339 | */ 340 | public static function import($uuid) 341 | { 342 | return new static(static::makeBin($uuid, 16)); 343 | } 344 | 345 | /** 346 | * Compares the binary representations of two UUIDs. 347 | * The comparison will return true if they are bit-exact, 348 | * or if neither is valid. 349 | * 350 | * @param string $a 351 | * @param string $b 352 | * @return string|string 353 | */ 354 | public static function compare($a, $b) 355 | { 356 | if (static::makeBin($a, 16) == static::makeBin($b, 16)) { 357 | return true; 358 | } else { 359 | return false; 360 | } 361 | } 362 | 363 | /** 364 | * @param string $var 365 | * @return string|string|number|number|number|number|number|NULL|number|NULL|NULL 366 | */ 367 | public function __get($var) 368 | { 369 | switch ($var) { 370 | case "bytes": 371 | return $this->bytes; 372 | break; 373 | case "hex": 374 | return bin2hex($this->bytes); 375 | break; 376 | case "node": 377 | if (ord($this->bytes[6]) >> 4 == 1) { 378 | return bin2hex(substr($this->bytes, 10)); 379 | } else { 380 | return null; 381 | } 382 | break; 383 | case "string": 384 | return $this->__toString(); 385 | break; 386 | case "uuid_ordered": 387 | return $this->__toUuidOrdered(); 388 | break; 389 | case "time": 390 | if (ord($this->bytes[6]) >> 4 == 1) { 391 | // Restore contiguous big-endian byte order 392 | $time = bin2hex($this->bytes[6] . $this->bytes[7] . $this->bytes[4] . $this->bytes[5] . 393 | $this->bytes[0] . $this->bytes[1] . $this->bytes[2] . $this->bytes[3]); 394 | // Clear version flag 395 | $time[0] = "0"; 396 | 397 | // Do some reverse arithmetic to get a Unix timestamp 398 | return (hexdec($time) - static::INTERVAL) / 10000000; 399 | } else { 400 | return null; 401 | } 402 | break; 403 | case "urn": 404 | return "urn:uuid:" . $this->__toString(); 405 | break; 406 | case "variant": 407 | $byte = ord($this->bytes[8]); 408 | if ($byte >= static::VAR_RES) { 409 | return 3; 410 | } elseif ($byte >= static::VAR_MS) { 411 | return 2; 412 | } elseif ($byte >= static::VAR_RFC) { 413 | return 1; 414 | } else { 415 | return 0; 416 | } 417 | break; 418 | case "version": 419 | return ord($this->bytes[6]) >> 4; 420 | break; 421 | default: 422 | return null; 423 | break; 424 | } 425 | } 426 | 427 | /** 428 | * Return the UUID 429 | * 430 | * @return string 431 | */ 432 | public function __toString() 433 | { 434 | return $this->string; 435 | } 436 | 437 | /** 438 | * Return the UUID ORDERED 439 | * 440 | * @return uuid ordered 441 | */ 442 | public function __toUuidOrdered() 443 | { 444 | return $this->uuid_ordered; 445 | } 446 | 447 | /** 448 | * Import and validate an UUID 449 | * 450 | * @param Uuid|string $uuid 451 | * 452 | * @return boolean 453 | */ 454 | public static function validate($uuid) 455 | { 456 | return (boolean) preg_match('~' . static::VALID_UUID_REGEX . '~', static::import($uuid)->string); 457 | } 458 | } 459 | -------------------------------------------------------------------------------- /src/Webpatser/Uuid/UuidServiceProvider.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('Webpatser\Uuid\Uuid', $uuid); 12 | 13 | $uuid = Uuid::generate(3, 'example.com', Uuid::NS_DNS); 14 | $this->assertInstanceOf('Webpatser\Uuid\Uuid', $uuid); 15 | 16 | $uuid = Uuid::generate(4); 17 | $this->assertInstanceOf('Webpatser\Uuid\Uuid', $uuid); 18 | 19 | $uuid = Uuid::generate(5, 'example.com', Uuid::NS_DNS); 20 | $this->assertInstanceOf('Webpatser\Uuid\Uuid', $uuid); 21 | } 22 | 23 | public function testImportAllZeroUuid() 24 | { 25 | $uuid = Uuid::import('00000000-0000-0000-0000-000000000000'); 26 | $this->assertInstanceOf('Webpatser\Uuid\Uuid', $uuid); 27 | $this->assertEquals('00000000-0000-0000-0000-000000000000', (string) $uuid); 28 | } 29 | 30 | public function testGenerationOfValidUuidViaRegex() 31 | { 32 | $uuid = Uuid::generate(1); 33 | $this->assertMatchesRegularExpression('~' . Uuid::VALID_UUID_REGEX . '~', (string)$uuid); 34 | 35 | $uuid = Uuid::generate(3, 'example.com', Uuid::NS_DNS); 36 | $this->assertMatchesRegularExpression('~' . Uuid::VALID_UUID_REGEX . '~', (string)$uuid); 37 | 38 | $uuid = Uuid::generate(4); 39 | $this->assertMatchesRegularExpression('~' . Uuid::VALID_UUID_REGEX . '~', (string)$uuid); 40 | 41 | $uuid = Uuid::generate(5, 'example.com', Uuid::NS_DNS); 42 | $this->assertMatchesRegularExpression('~' . Uuid::VALID_UUID_REGEX . '~', (string)$uuid); 43 | } 44 | 45 | public function testGenerationOfValidUuidViaValidator() 46 | { 47 | $uuid = Uuid::generate(1); 48 | $this->assertTrue(Uuid::validate($uuid->string)); 49 | 50 | $uuid = Uuid::generate(3, 'example.com', Uuid::NS_DNS); 51 | $this->assertTrue(Uuid::validate($uuid->string)); 52 | 53 | $uuid = Uuid::generate(4); 54 | $this->assertTrue(Uuid::validate($uuid->string)); 55 | 56 | $uuid = Uuid::generate(5, 'example.com', Uuid::NS_DNS); 57 | $this->assertTrue(Uuid::validate($uuid->string)); 58 | 59 | $uuid = Uuid::generate(1); 60 | $this->assertTrue(Uuid::validate($uuid->bytes)); 61 | 62 | $uuid = Uuid::generate(3, 'example.com', Uuid::NS_DNS); 63 | $this->assertTrue(Uuid::validate($uuid->bytes)); 64 | 65 | $uuid = Uuid::generate(4); 66 | $this->assertTrue(Uuid::validate($uuid->bytes)); 67 | 68 | $uuid = Uuid::generate(5, 'example.com', Uuid::NS_DNS); 69 | $this->assertTrue(Uuid::validate($uuid->bytes)); 70 | 71 | $uuid = Uuid::generate(1); 72 | $this->assertTrue(Uuid::validate($uuid->urn)); 73 | 74 | $uuid = Uuid::generate(3, 'example.com', Uuid::NS_DNS); 75 | $this->assertTrue(Uuid::validate($uuid->urn)); 76 | 77 | $uuid = Uuid::generate(4); 78 | $this->assertTrue(Uuid::validate($uuid->urn)); 79 | 80 | $uuid = Uuid::generate(5, 'example.com', Uuid::NS_DNS); 81 | $this->assertTrue(Uuid::validate($uuid->urn)); 82 | 83 | $this->assertTrue(Uuid::validate(Uuid::generate(1))); 84 | 85 | $this->assertTrue(Uuid::validate(Uuid::generate(3, 'example.com', Uuid::NS_DNS))); 86 | 87 | $this->assertTrue(Uuid::validate(Uuid::generate(4))); 88 | 89 | $this->assertTrue(Uuid::validate(Uuid::generate(5, 'example.com', Uuid::NS_DNS))); 90 | } 91 | 92 | public function testCorrectVersionUuid() 93 | { 94 | $uuidOne = Uuid::generate(1); 95 | $this->assertEquals(1, $uuidOne->version); 96 | 97 | $uuidThree = Uuid::generate(3, 'example.com', Uuid::NS_DNS); 98 | $this->assertEquals(3, $uuidThree->version); 99 | 100 | $uuidFour = Uuid::generate(4); 101 | $this->assertEquals(4, $uuidFour->version); 102 | 103 | $uuidFive = Uuid::generate(5, 'example.com', Uuid::NS_DNS); 104 | $this->assertEquals(5, $uuidFive->version); 105 | } 106 | 107 | public function testCorrectVariantUuid() 108 | { 109 | $uuidOne = Uuid::generate(1); 110 | $this->assertEquals(1, $uuidOne->variant); 111 | 112 | $uuidThree = Uuid::generate(3, 'example.com', Uuid::NS_DNS); 113 | $this->assertEquals(1, $uuidThree->variant); 114 | 115 | $uuidFour = Uuid::generate(4); 116 | $this->assertEquals(1, $uuidFour->variant); 117 | 118 | $uuidFive = Uuid::generate(5, 'example.com', Uuid::NS_DNS); 119 | $this->assertEquals(1, $uuidFive->variant); 120 | } 121 | 122 | public function testCorrectVersionOfImportedUuid() 123 | { 124 | $uuidOne = Uuid::generate(1); 125 | $importedOne = Uuid::import((string)$uuidOne); 126 | $this->assertEquals($uuidOne->version, $importedOne->version); 127 | 128 | $uuidThree = Uuid::generate(3, 'example.com', Uuid::NS_DNS); 129 | $importedThree = Uuid::import((string)$uuidThree); 130 | $this->assertEquals($uuidThree->version, $importedThree->version); 131 | 132 | $uuidFour = Uuid::generate(4); 133 | $importedFour = Uuid::import((string)$uuidFour); 134 | $this->assertEquals($uuidFour->version, $importedFour->version); 135 | 136 | $uuidFive = Uuid::generate(5, 'example.com', Uuid::NS_DNS); 137 | $importedFive = Uuid::import((string)$uuidFive); 138 | $this->assertEquals($uuidFive->version, $importedFive->version); 139 | } 140 | 141 | public function testCorrectNodeOfGeneratedUuid() 142 | { 143 | $macAdress = Faker\Provider\Internet::macAddress(); 144 | $uuidThree = Uuid::generate(1, $macAdress); 145 | $this->assertEquals(strtolower(str_replace(':', '', $macAdress)), $uuidThree->node); 146 | 147 | $uuidThree = Uuid::generate(3, $macAdress, Uuid::NS_DNS); 148 | $this->assertNull($uuidThree->node); 149 | 150 | $uuidThree = Uuid::generate(4, $macAdress); 151 | $this->assertNull($uuidThree->node); 152 | 153 | $uuidThree = Uuid::generate(5, $macAdress, Uuid::NS_DNS); 154 | $this->assertNull($uuidThree->node); 155 | } 156 | 157 | public function testCorrectTimeOfImportedUuid() 158 | { 159 | $uuidOne = Uuid::generate(1); 160 | $importedOne = Uuid::import((string)$uuidOne); 161 | $this->assertEquals($uuidOne->time, $importedOne->time); 162 | 163 | $uuidThree = Uuid::generate(3, 'example.com', Uuid::NS_DNS); 164 | $importedThree = Uuid::import((string)$uuidThree); 165 | $this->assertEmpty($importedThree->time); 166 | 167 | $uuidFour = Uuid::generate(4); 168 | $importedFour = Uuid::import((string)$uuidFour); 169 | $this->assertEmpty($importedFour->time); 170 | 171 | $uuidFive = Uuid::generate(5, 'example.com', Uuid::NS_DNS); 172 | $importedFive = Uuid::import((string)$uuidFive); 173 | $this->assertEmpty($importedFive->time); 174 | } 175 | 176 | public function testUuidCompare() 177 | { 178 | $uuid1 = (string)Uuid::generate(1); 179 | $uuid2 = (string)Uuid::generate(1); 180 | 181 | $this->assertTrue(Uuid::compare($uuid1, $uuid1)); 182 | $this->assertFalse(Uuid::compare($uuid1, $uuid2)); 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |