UuidTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Drupal\Tests\Component\Uuid;
  3. use Drupal\Component\Uuid\Uuid;
  4. use Drupal\Component\Uuid\UuidInterface;
  5. use Drupal\Component\Uuid\Com;
  6. use Drupal\Component\Uuid\Pecl;
  7. use Drupal\Component\Uuid\Php;
  8. use PHPUnit\Framework\TestCase;
  9. /**
  10. * Tests the handling of Universally Unique Identifiers (UUIDs).
  11. *
  12. * @group Uuid
  13. */
  14. class UuidTest extends TestCase {
  15. /**
  16. * Tests generating valid UUIDs.
  17. *
  18. * @dataProvider providerUuidInstances
  19. */
  20. public function testGenerateUuid(UuidInterface $instance) {
  21. $this->assertTrue(Uuid::isValid($instance->generate()), sprintf('UUID generation for %s works.', get_class($instance)));
  22. }
  23. /**
  24. * Tests that generated UUIDs are unique.
  25. *
  26. * @dataProvider providerUuidInstances
  27. */
  28. public function testUuidIsUnique(UuidInterface $instance) {
  29. $this->assertNotEquals($instance->generate(), $instance->generate(), sprintf('Same UUID was not generated twice with %s.', get_class($instance)));
  30. }
  31. /**
  32. * Dataprovider for UUID instance tests.
  33. *
  34. * @return array
  35. */
  36. public function providerUuidInstances() {
  37. $instances = [];
  38. $instances[][] = new Php();
  39. // If valid PECL extensions exists add to list.
  40. if (function_exists('uuid_create') && !function_exists('uuid_make')) {
  41. $instances[][] = new Pecl();
  42. }
  43. // If we are on Windows add the com implementation as well.
  44. if (function_exists('com_create_guid')) {
  45. $instances[][] = new Com();
  46. }
  47. return $instances;
  48. }
  49. /**
  50. * Tests UUID validation.
  51. *
  52. * @param string $uuid
  53. * The uuid to check against.
  54. * @param bool $is_valid
  55. * Whether the uuid is valid or not.
  56. * @param string $message
  57. * The message to display on failure.
  58. *
  59. * @dataProvider providerTestValidation
  60. */
  61. public function testValidation($uuid, $is_valid, $message) {
  62. $this->assertSame($is_valid, Uuid::isValid($uuid), $message);
  63. }
  64. /**
  65. * Dataprovider for UUID instance tests.
  66. *
  67. * @return array
  68. * An array of arrays containing
  69. * - The Uuid to check against.
  70. * - (bool) Whether or not the Uuid is valid.
  71. * - Failure message.
  72. */
  73. public function providerTestValidation() {
  74. return [
  75. // These valid UUIDs.
  76. ['6ba7b810-9dad-11d1-80b4-00c04fd430c8', TRUE, 'Basic FQDN UUID did not validate'],
  77. ['00000000-0000-0000-0000-000000000000', TRUE, 'Minimum UUID did not validate'],
  78. ['ffffffff-ffff-ffff-ffff-ffffffffffff', TRUE, 'Maximum UUID did not validate'],
  79. // These are invalid UUIDs.
  80. ['0ab26e6b-f074-4e44-9da-601205fa0e976', FALSE, 'Invalid format was validated'],
  81. ['0ab26e6b-f074-4e44-9daf-1205fa0e9761f', FALSE, 'Invalid length was validated'],
  82. ];
  83. }
  84. }