View source
<?php
namespace Drupal\Tests\Component\Uuid;
use Drupal\Component\Uuid\Uuid;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Component\Uuid\Com;
use Drupal\Component\Uuid\Pecl;
use Drupal\Component\Uuid\Php;
use PHPUnit\Framework\TestCase;
class UuidTest extends TestCase {
public function testGenerateUuid(UuidInterface $instance) {
$this
->assertTrue(Uuid::isValid($instance
->generate()), sprintf('UUID generation for %s works.', get_class($instance)));
}
public function testUuidIsUnique(UuidInterface $instance) {
$this
->assertNotEquals($instance
->generate(), $instance
->generate(), sprintf('Same UUID was not generated twice with %s.', get_class($instance)));
}
public function providerUuidInstances() {
$instances = [];
$instances[][] = new Php();
if (function_exists('uuid_create') && !function_exists('uuid_make')) {
$instances[][] = new Pecl();
}
if (function_exists('com_create_guid')) {
$instances[][] = new Com();
}
return $instances;
}
public function testValidation($uuid, $is_valid, $message) {
$this
->assertSame($is_valid, Uuid::isValid($uuid), $message);
}
public function providerTestValidation() {
return [
[
'6ba7b810-9dad-11d1-80b4-00c04fd430c8',
TRUE,
'Basic FQDN UUID did not validate',
],
[
'00000000-0000-0000-0000-000000000000',
TRUE,
'Minimum UUID did not validate',
],
[
'ffffffff-ffff-ffff-ffff-ffffffffffff',
TRUE,
'Maximum UUID did not validate',
],
[
'0ab26e6b-f074-4e44-9da-601205fa0e976',
FALSE,
'Invalid format was validated',
],
[
'0ab26e6b-f074-4e44-9daf-1205fa0e9761f',
FALSE,
'Invalid length was validated',
],
];
}
}