UUIDTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Unit tests for UUID
  4. *
  5. * @group uuid
  6. * @package Unittest
  7. *
  8. * @author shadowhand <woody.gilk@kohanaframework.org>
  9. * @copyright (c) 2007-2016 Kohana Team
  10. * @copyright (c) since 2016 Koseven Team
  11. * @license https://koseven.dev/LICENSE
  12. */
  13. class KO7_UUIDTest extends KO7_Unittest_TestCase {
  14. /**
  15. * Provides test data for test_valid()
  16. *
  17. * @return array
  18. */
  19. public function provider_valid() : array
  20. {
  21. return [
  22. ['00000000-0000-0000-0000-000000000000', TRUE],
  23. ['26c8a352-60a5-342f-ae9b-0b06cde477fb', TRUE],
  24. ['3afb8d83-6af4-48f3-91f2-febbdc46e7a2', TRUE],
  25. ['f74bd7e3-ec3c-5c8a-aaeb-1b3c0dc16ea3', TRUE],
  26. ['6ba7b810-9dad-11d1-80b4-00c04fd430c8', TRUE],
  27. ['6ba7b810-9dad-11d1-80b4-zzzzzzzzzzzz', FALSE],
  28. ['00000000-0000-0000-000000000000', FALSE],
  29. ['6ba7b810-9dad-11d1-80b4-00c04fd430x0', FALSE]
  30. ];
  31. }
  32. /**
  33. * Tests UUID::valid()
  34. *
  35. * @dataProvider provider_valid
  36. * @covers UUID::valid
  37. *
  38. * @param string $uuid UUID to test validity
  39. * @param bool $expected Expect UUID to be valid?
  40. */
  41. public function test_valid(string $uuid, bool $expected)
  42. {
  43. self::assertEquals($expected, UUID::valid($uuid));
  44. }
  45. /**
  46. * Tests UUID::v4()
  47. *
  48. * @covers UUID::v4
  49. * @throws Exception
  50. */
  51. public function test_v4_random()
  52. {
  53. self::assertNotEquals('6ba7b810-9dad-11d1-80b4-00c04fd430c8', UUID::v4());
  54. }
  55. /**
  56. * Provides test data for test_v3_md5()
  57. *
  58. * @return array
  59. */
  60. public function provider_v3_md5() : array
  61. {
  62. return [
  63. ['kohana', 'ffa14b9e-3afc-3989-95b7-cd49a421ee8a'],
  64. ['shadowhand', '819df73f-f819-3f53-946b-fd2e1c9f25a2'],
  65. ['zombor', 'fbb96ab5-e716-3920-91da-620a977db2cc'],
  66. ['team', 'db7ec69b-eb29-37ef-a76d-2e2ef553e92e']
  67. ];
  68. }
  69. /**
  70. * Tests UUID::v3()
  71. *
  72. * @dataProvider provider_v3_md5
  73. * @covers UUID::v3
  74. *
  75. * @param string $value Value to generate UUID from
  76. * @param string $expected UUID
  77. *
  78. * @throws KO7_Exception
  79. */
  80. public function test_v3_md5(string $value, string $expected)
  81. {
  82. self::assertEquals($expected, UUID::v3(UUID::NIL, $value));
  83. }
  84. /**
  85. * Provides test data for test_v3_md5()
  86. *
  87. * @return array
  88. */
  89. public function provider_v5_sha1() : array
  90. {
  91. return [
  92. ['kohana', '476f3195-2016-5eb4-8422-1505cb2c6066'],
  93. ['shadowhand', '93617e5a-9632-5d84-9512-16f76aa39015'],
  94. ['zombor', '996be374-db7b-5976-b822-f6ba1fae7337'],
  95. ['team', 'd221f29a-4332-5f0d-b323-c5206a2e86ce']
  96. ];
  97. }
  98. /**
  99. * Tests UUID::v5()
  100. *
  101. * @dataProvider provider_v5_sha1
  102. * @covers UUID::v5
  103. *
  104. * @param string $value Value to generate UUID from
  105. * @param string $expected UUID
  106. *
  107. * @throws KO7_Exception
  108. */
  109. public function test_v5_sha1(string $value, string $expected)
  110. {
  111. self::assertEquals($expected, UUID::v5(UUID::NIL, $value));
  112. }
  113. /**
  114. * Provides test data for str() and bin()
  115. *
  116. * @return array
  117. */
  118. public function provider_bin_str() : array
  119. {
  120. return [
  121. ['3afb8d83-6af4-48f3-91f2-febbdc46e7a2', '3afb8d836af448f391f2febbdc46e7a2'],
  122. ['f74bd7e3-ec3c-5c8a-aaeb-1b3c0dc16ea3', 'f74bd7e3ec3c5c8aaaeb1b3c0dc16ea3'],
  123. ['6ba7b810-9dad-11d1-80b4-00c04fd430c8', '6ba7b8109dad11d180b400c04fd430c8']
  124. ];
  125. }
  126. /**
  127. * Tests UUID::str and UUID:bin
  128. *
  129. * @dataProvider provider_bin_str
  130. * @covers UUID::str
  131. * @covers UUID::bin
  132. *
  133. * @param string $value Value to convert to binary and back to string
  134. * @param mixed $expected Expected binary result in hex
  135. *
  136. * @throws KO7_Exception
  137. */
  138. public function test_bin_str(string $value, $expected)
  139. {
  140. $bin = UUID::bin($value);
  141. self::assertEquals($expected, bin2hex($bin));
  142. if ($expected !== FALSE)
  143. {
  144. self::assertEquals($value, UUID::str($bin));
  145. }
  146. }
  147. /**
  148. * Tests passing invalid namespace v3
  149. */
  150. public function test_invalid_namespace_v3() {
  151. $this->expectException(KO7_Exception::class);
  152. UUID::v3('invalid', 'koseven');
  153. }
  154. /**
  155. * Tests passing invalid namespace v5
  156. */
  157. public function test_invalid_namespace_v5() {
  158. $this->expectException(KO7_Exception::class);
  159. UUID::v5('invalid', 'koseven');
  160. }
  161. /**
  162. * Tests passing invalid uuid bin
  163. */
  164. public function test_invalid_uuid_bin() {
  165. $this->expectException(KO7_Exception::class);
  166. UUID::bin('invalid');
  167. }
  168. }