3 namespace Drupal\Tests\Component\Uuid;
5 use Drupal\Component\Uuid\Uuid;
6 use Drupal\Component\Uuid\UuidInterface;
7 use Drupal\Component\Uuid\Com;
8 use Drupal\Component\Uuid\Pecl;
9 use Drupal\Component\Uuid\Php;
10 use PHPUnit\Framework\TestCase;
13 * Tests the handling of Universally Unique Identifiers (UUIDs).
17 class UuidTest extends TestCase {
20 * Tests generating valid UUIDs.
22 * @dataProvider providerUuidInstances
24 public function testGenerateUuid(UuidInterface $instance) {
25 $this->assertTrue(Uuid::isValid($instance->generate()), sprintf('UUID generation for %s works.', get_class($instance)));
29 * Tests that generated UUIDs are unique.
31 * @dataProvider providerUuidInstances
33 public function testUuidIsUnique(UuidInterface $instance) {
34 $this->assertNotEquals($instance->generate(), $instance->generate(), sprintf('Same UUID was not generated twice with %s.', get_class($instance)));
38 * Dataprovider for UUID instance tests.
42 public function providerUuidInstances() {
45 $instances[][] = new Php();
47 // If valid PECL extensions exists add to list.
48 if (function_exists('uuid_create') && !function_exists('uuid_make')) {
49 $instances[][] = new Pecl();
52 // If we are on Windows add the com implementation as well.
53 if (function_exists('com_create_guid')) {
54 $instances[][] = new Com();
61 * Tests UUID validation.
64 * The uuid to check against.
65 * @param bool $is_valid
66 * Whether the uuid is valid or not.
67 * @param string $message
68 * The message to display on failure.
70 * @dataProvider providerTestValidation
72 public function testValidation($uuid, $is_valid, $message) {
73 $this->assertSame($is_valid, Uuid::isValid($uuid), $message);
77 * Dataprovider for UUID instance tests.
80 * An array of arrays containing
81 * - The Uuid to check against.
82 * - (bool) Whether or not the Uuid is valid.
85 public function providerTestValidation() {
88 ['6ba7b810-9dad-11d1-80b4-00c04fd430c8', TRUE, 'Basic FQDN UUID did not validate'],
89 ['00000000-0000-0000-0000-000000000000', TRUE, 'Minimum UUID did not validate'],
90 ['ffffffff-ffff-ffff-ffff-ffffffffffff', TRUE, 'Maximum UUID did not validate'],
91 // These are invalid UUIDs.
92 ['0ab26e6b-f074-4e44-9da-601205fa0e976', FALSE, 'Invalid format was validated'],
93 ['0ab26e6b-f074-4e44-9daf-1205fa0e9761f', FALSE, 'Invalid length was validated'],