comparison core/tests/Drupal/Tests/Component/Uuid/UuidTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\Tests\Component\Uuid;
4
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;
11
12 /**
13 * Tests the handling of Universally Unique Identifiers (UUIDs).
14 *
15 * @group Uuid
16 */
17 class UuidTest extends TestCase {
18
19 /**
20 * Tests generating valid UUIDs.
21 *
22 * @dataProvider providerUuidInstances
23 */
24 public function testGenerateUuid(UuidInterface $instance) {
25 $this->assertTrue(Uuid::isValid($instance->generate()), sprintf('UUID generation for %s works.', get_class($instance)));
26 }
27
28 /**
29 * Tests that generated UUIDs are unique.
30 *
31 * @dataProvider providerUuidInstances
32 */
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)));
35 }
36
37 /**
38 * Dataprovider for UUID instance tests.
39 *
40 * @return array
41 */
42 public function providerUuidInstances() {
43
44 $instances = [];
45 $instances[][] = new Php();
46
47 // If valid PECL extensions exists add to list.
48 if (function_exists('uuid_create') && !function_exists('uuid_make')) {
49 $instances[][] = new Pecl();
50 }
51
52 // If we are on Windows add the com implementation as well.
53 if (function_exists('com_create_guid')) {
54 $instances[][] = new Com();
55 }
56
57 return $instances;
58 }
59
60 /**
61 * Tests UUID validation.
62 *
63 * @param string $uuid
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.
69 *
70 * @dataProvider providerTestValidation
71 */
72 public function testValidation($uuid, $is_valid, $message) {
73 $this->assertSame($is_valid, Uuid::isValid($uuid), $message);
74 }
75
76 /**
77 * Dataprovider for UUID instance tests.
78 *
79 * @return array
80 * An array of arrays containing
81 * - The Uuid to check against.
82 * - (bool) Whether or not the Uuid is valid.
83 * - Failure message.
84 */
85 public function providerTestValidation() {
86 return [
87 // These valid UUIDs.
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'],
94 ];
95 }
96
97 }