ValidatorTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Rfc4122;
  4. use Ramsey\Uuid\Rfc4122\Validator;
  5. use Ramsey\Uuid\Test\TestCase;
  6. use function array_merge;
  7. use function in_array;
  8. use function strtoupper;
  9. class ValidatorTest extends TestCase
  10. {
  11. /**
  12. * @dataProvider provideValuesForValidation
  13. */
  14. public function testValidate(string $value, bool $expected): void
  15. {
  16. $variations = [];
  17. $variations[] = $value;
  18. $variations[] = 'urn:uuid:' . $value;
  19. $variations[] = '{' . $value . '}';
  20. foreach ($variations as $variation) {
  21. $variations[] = strtoupper($variation);
  22. }
  23. $validator = new Validator();
  24. foreach ($variations as $variation) {
  25. $this->assertSame(
  26. $expected,
  27. $validator->validate($variation),
  28. sprintf(
  29. 'Expected "%s" to be %s',
  30. $variation,
  31. $expected ? 'valid' : 'not valid',
  32. ),
  33. );
  34. }
  35. }
  36. /**
  37. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  38. */
  39. public function provideValuesForValidation(): array
  40. {
  41. $hexMutations = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f'];
  42. $trueVersions = [1, 2, 3, 4, 5, 6, 7, 8];
  43. $trueVariants = [8, 9, 'a', 'b'];
  44. $testValues = [];
  45. foreach ($hexMutations as $version) {
  46. foreach ($hexMutations as $variant) {
  47. $testValues[] = [
  48. 'value' => "ff6f8cb0-c57d-{$version}1e1-{$variant}b21-0800200c9a66",
  49. 'expected' => in_array($variant, $trueVariants, true) && in_array($version, $trueVersions, true),
  50. ];
  51. }
  52. }
  53. return array_merge($testValues, [
  54. [
  55. 'value' => 'zf6f8cb0-c57d-11e1-9b21-0800200c9a66',
  56. 'expected' => false,
  57. ],
  58. [
  59. 'value' => '3f6f8cb0-c57d-11e1-9b21-0800200c9a6',
  60. 'expected' => false,
  61. ],
  62. [
  63. 'value' => 'af6f8cb-c57d-11e1-9b21-0800200c9a66',
  64. 'expected' => false,
  65. ],
  66. [
  67. 'value' => 'af6f8cb0c57d11e19b210800200c9a66',
  68. 'expected' => false,
  69. ],
  70. [
  71. 'value' => 'ff6f8cb0-c57da-51e1-9b21-0800200c9a66',
  72. 'expected' => false,
  73. ],
  74. [
  75. 'value' => "ff6f8cb0-c57d-11e1-1b21-0800200c9a66\n",
  76. 'expected' => false,
  77. ],
  78. [
  79. 'value' => "\nff6f8cb0-c57d-11e1-1b21-0800200c9a66",
  80. 'expected' => false,
  81. ],
  82. [
  83. 'value' => "\nff6f8cb0-c57d-11e1-1b21-0800200c9a66\n",
  84. 'expected' => false,
  85. ],
  86. [
  87. 'value' => '00000000-0000-0000-0000-000000000000',
  88. 'expected' => true,
  89. ],
  90. [
  91. 'value' => 'ffffffff-ffff-ffff-ffff-ffffffffffff',
  92. 'expected' => true,
  93. ],
  94. [
  95. 'value' => 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF',
  96. 'expected' => true,
  97. ],
  98. ]);
  99. }
  100. public function testGetPattern(): void
  101. {
  102. $expectedPattern = '\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-'
  103. . '[1-8][0-9A-Fa-f]{3}-[ABab89][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
  104. $validator = new Validator();
  105. $this->assertSame($expectedPattern, $validator->getPattern());
  106. }
  107. }