StringCodecTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Codec;
  4. use InvalidArgumentException;
  5. use Mockery;
  6. use PHPUnit\Framework\MockObject\MockObject;
  7. use Ramsey\Uuid\Builder\UuidBuilderInterface;
  8. use Ramsey\Uuid\Codec\StringCodec;
  9. use Ramsey\Uuid\Rfc4122\Fields;
  10. use Ramsey\Uuid\Rfc4122\FieldsInterface;
  11. use Ramsey\Uuid\Test\TestCase;
  12. use Ramsey\Uuid\UuidInterface;
  13. use function hex2bin;
  14. use function implode;
  15. use function pack;
  16. class StringCodecTest extends TestCase
  17. {
  18. /**
  19. * @var UuidBuilderInterface & MockObject
  20. */
  21. private $builder;
  22. /**
  23. * @var UuidInterface & MockObject
  24. */
  25. private $uuid;
  26. /**
  27. * @var Fields
  28. */
  29. private $fields;
  30. /**
  31. * @var string
  32. */
  33. private $uuidString = '12345678-1234-4bcd-abef-1234abcd4321';
  34. protected function setUp(): void
  35. {
  36. parent::setUp();
  37. $this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock();
  38. $this->uuid = $this->getMockBuilder(UuidInterface::class)->getMock();
  39. $this->fields = new Fields((string) hex2bin('1234567812344bcdabef1234abcd4321'));
  40. }
  41. protected function tearDown(): void
  42. {
  43. parent::tearDown();
  44. unset($this->builder, $this->uuid, $this->fields);
  45. }
  46. public function testEncodeUsesFieldsArray(): void
  47. {
  48. $this->uuid->expects($this->once())
  49. ->method('getFields')
  50. ->willReturn($this->fields);
  51. $codec = new StringCodec($this->builder);
  52. $codec->encode($this->uuid);
  53. }
  54. public function testEncodeReturnsFormattedString(): void
  55. {
  56. $this->uuid->method('getFields')
  57. ->willReturn($this->fields);
  58. $codec = new StringCodec($this->builder);
  59. $result = $codec->encode($this->uuid);
  60. $this->assertSame($this->uuidString, $result);
  61. }
  62. public function testEncodeBinaryReturnsBinaryString(): void
  63. {
  64. $expected = hex2bin('123456781234abcdabef1234abcd4321');
  65. $fields = Mockery::mock(FieldsInterface::class, [
  66. 'getBytes' => hex2bin('123456781234abcdabef1234abcd4321'),
  67. ]);
  68. $this->uuid->method('getFields')->willReturn($fields);
  69. $codec = new StringCodec($this->builder);
  70. $result = $codec->encodeBinary($this->uuid);
  71. $this->assertSame($expected, $result);
  72. }
  73. public function testDecodeUsesBuilderOnFields(): void
  74. {
  75. $fields = [
  76. 'time_low' => $this->fields->getTimeLow()->toString(),
  77. 'time_mid' => $this->fields->getTimeMid()->toString(),
  78. 'time_hi_and_version' => $this->fields->getTimeHiAndVersion()->toString(),
  79. 'clock_seq_hi_and_reserved' => $this->fields->getClockSeqHiAndReserved()->toString(),
  80. 'clock_seq_low' => $this->fields->getClockSeqLow()->toString(),
  81. 'node' => $this->fields->getNode()->toString(),
  82. ];
  83. $bytes = hex2bin(implode('', $fields));
  84. $string = 'uuid:12345678-1234-4bcd-abef-1234abcd4321';
  85. $this->builder->expects($this->once())
  86. ->method('build')
  87. ->with($this->isInstanceOf(StringCodec::class), $bytes);
  88. $codec = new StringCodec($this->builder);
  89. $codec->decode($string);
  90. }
  91. public function testDecodeThrowsExceptionOnInvalidUuid(): void
  92. {
  93. $string = 'invalid-uuid';
  94. $codec = new StringCodec($this->builder);
  95. $this->expectException(InvalidArgumentException::class);
  96. $codec->decode($string);
  97. }
  98. public function testDecodeReturnsUuidFromBuilder(): void
  99. {
  100. $string = 'uuid:12345678-1234-abcd-abef-1234abcd4321';
  101. $this->builder->method('build')
  102. ->willReturn($this->uuid);
  103. $codec = new StringCodec($this->builder);
  104. $result = $codec->decode($string);
  105. $this->assertSame($this->uuid, $result);
  106. }
  107. public function testDecodeBytesThrowsExceptionWhenBytesStringNotSixteenCharacters(): void
  108. {
  109. $string = '61';
  110. $bytes = pack('H*', $string);
  111. $codec = new StringCodec($this->builder);
  112. $this->expectException(InvalidArgumentException::class);
  113. $this->expectExceptionMessage('$bytes string should contain 16 characters.');
  114. $codec->decodeBytes($bytes);
  115. }
  116. public function testDecodeBytesReturnsUuid(): void
  117. {
  118. $string = '123456781234abcdabef1234abcd4321';
  119. $bytes = pack('H*', $string);
  120. $codec = new StringCodec($this->builder);
  121. $this->builder->method('build')
  122. ->willReturn($this->uuid);
  123. $result = $codec->decodeBytes($bytes);
  124. $this->assertSame($this->uuid, $result);
  125. }
  126. }