GenericValidatorTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Validator;
  4. use Ramsey\Uuid\Test\TestCase;
  5. use Ramsey\Uuid\Validator\GenericValidator;
  6. use function array_merge;
  7. use function strtoupper;
  8. class GenericValidatorTest extends TestCase
  9. {
  10. /**
  11. * @dataProvider provideValuesForValidation
  12. */
  13. public function testValidate(string $value, bool $expected): void
  14. {
  15. $variations = [];
  16. $variations[] = $value;
  17. $variations[] = 'urn:uuid:' . $value;
  18. $variations[] = '{' . $value . '}';
  19. foreach ($variations as $variation) {
  20. $variations[] = strtoupper($variation);
  21. }
  22. $validator = new GenericValidator();
  23. foreach ($variations as $variation) {
  24. $this->assertSame($expected, $validator->validate($variation));
  25. }
  26. }
  27. /**
  28. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  29. */
  30. public function provideValuesForValidation(): array
  31. {
  32. $hexMutations = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f'];
  33. $testValues = [];
  34. foreach ($hexMutations as $version) {
  35. foreach ($hexMutations as $variant) {
  36. $testValues[] = [
  37. 'value' => "ff6f8cb0-c57d-{$version}1e1-{$variant}b21-0800200c9a66",
  38. 'expected' => true,
  39. ];
  40. }
  41. }
  42. return array_merge($testValues, [
  43. [
  44. 'value' => 'zf6f8cb0-c57d-11e1-9b21-0800200c9a66',
  45. 'expected' => false,
  46. ],
  47. [
  48. 'value' => '3f6f8cb0-c57d-11e1-9b21-0800200c9a6',
  49. 'expected' => false,
  50. ],
  51. [
  52. 'value' => 'af6f8cb-c57d-11e1-9b21-0800200c9a66',
  53. 'expected' => false,
  54. ],
  55. [
  56. 'value' => 'af6f8cb0c57d11e19b210800200c9a66',
  57. 'expected' => false,
  58. ],
  59. [
  60. 'value' => 'ff6f8cb0-c57da-51e1-9b21-0800200c9a66',
  61. 'expected' => false,
  62. ],
  63. [
  64. 'value' => "ff6f8cb0-c57d-11e1-1b21-0800200c9a66\n",
  65. 'expected' => false,
  66. ],
  67. [
  68. 'value' => "\nff6f8cb0-c57d-11e1-1b21-0800200c9a66",
  69. 'expected' => false,
  70. ],
  71. [
  72. 'value' => "\nff6f8cb0-c57d-11e1-1b21-0800200c9a66\n",
  73. 'expected' => false,
  74. ],
  75. ]);
  76. }
  77. public function testGetPattern(): void
  78. {
  79. $expectedPattern = '\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\z';
  80. $validator = new GenericValidator();
  81. $this->assertSame($expectedPattern, $validator->getPattern());
  82. }
  83. }