Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve coverage and unit tests for fields, GUIDs, and nonstandard UUIDs
- Loading branch information
Showing
13 changed files
with
476 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ramsey\Uuid\Test\Builder; | ||
|
||
use Mockery; | ||
use Ramsey\Uuid\Builder\FallbackBuilder; | ||
use Ramsey\Uuid\Builder\UuidBuilderInterface; | ||
use Ramsey\Uuid\Codec\CodecInterface; | ||
use Ramsey\Uuid\Exception\BuilderNotFoundException; | ||
use Ramsey\Uuid\Exception\InvalidArgumentException; | ||
use Ramsey\Uuid\Test\TestCase; | ||
|
||
class FallbackBuilderTest extends TestCase | ||
{ | ||
public function testBuildThrowsExceptionAfterAllConfiguredBuildersHaveErrored(): void | ||
{ | ||
$codec = Mockery::mock(CodecInterface::class); | ||
$fields = ['someFields']; | ||
|
||
$builder1 = Mockery::mock(UuidBuilderInterface::class); | ||
$builder1 | ||
->shouldReceive('build') | ||
->once() | ||
->with($codec, $fields) | ||
->andThrow(InvalidArgumentException::class); | ||
|
||
$builder2 = Mockery::mock(UuidBuilderInterface::class); | ||
$builder2 | ||
->shouldReceive('build') | ||
->once() | ||
->with($codec, $fields) | ||
->andThrow(InvalidArgumentException::class); | ||
|
||
$builder3 = Mockery::mock(UuidBuilderInterface::class); | ||
$builder3 | ||
->shouldReceive('build') | ||
->once() | ||
->with($codec, $fields) | ||
->andThrow(InvalidArgumentException::class); | ||
|
||
$fallbackBuilder = new FallbackBuilder([$builder1, $builder2, $builder3]); | ||
|
||
$this->expectException(BuilderNotFoundException::class); | ||
$this->expectExceptionMessage( | ||
'Could not find a suitable builder for the provided codec and fields' | ||
); | ||
|
||
$fallbackBuilder->build($codec, $fields); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ramsey\Uuid\Test; | ||
|
||
use Mockery; | ||
use Ramsey\Uuid\Builder\FallbackBuilder; | ||
use Ramsey\Uuid\FeatureSet; | ||
use Ramsey\Uuid\Guid\DegradedGuidBuilder; | ||
use Ramsey\Uuid\Validator\ValidatorInterface; | ||
|
||
class FeatureSetTest extends TestCase | ||
{ | ||
public function testDegradedGuidBuilderIsSelectedOn32BitSystem(): void | ||
{ | ||
$featureSet = new FeatureSet(true, true); | ||
|
||
$this->assertInstanceOf(DegradedGuidBuilder::class, $featureSet->getBuilder()); | ||
} | ||
|
||
public function testFallbackBuilderIsSelectedOn32BitSystem(): void | ||
{ | ||
$featureSet = new FeatureSet(false, true); | ||
|
||
$this->assertInstanceOf(FallbackBuilder::class, $featureSet->getBuilder()); | ||
} | ||
|
||
public function testSetValidatorSetsTheProvidedValidator(): void | ||
{ | ||
$validator = Mockery::mock(ValidatorInterface::class); | ||
|
||
$featureSet = new FeatureSet(); | ||
$featureSet->setValidator($validator); | ||
|
||
$this->assertSame($validator, $featureSet->getValidator()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ramsey\Uuid\Test\Guid; | ||
|
||
use Mockery; | ||
use Ramsey\Uuid\Codec\CodecInterface; | ||
use Ramsey\Uuid\Converter\NumberConverterInterface; | ||
use Ramsey\Uuid\Converter\TimeConverterInterface; | ||
use Ramsey\Uuid\Guid\DegradedGuid; | ||
use Ramsey\Uuid\Guid\DegradedGuidBuilder; | ||
use Ramsey\Uuid\Test\TestCase; | ||
|
||
class DegradedGuidBuilderTest extends TestCase | ||
{ | ||
public function testBuilderBuildsDegradedGuid(): void | ||
{ | ||
$numberConverter = Mockery::mock(NumberConverterInterface::class); | ||
$codec = Mockery::mock(CodecInterface::class); | ||
$timeConverter = Mockery::mock(TimeConverterInterface::class); | ||
|
||
$fields = [ | ||
'b1484596', | ||
'25dc', | ||
'11ea', | ||
'978f', | ||
'2e728ce88125', | ||
]; | ||
|
||
$expectedFields = $fields; | ||
|
||
// Bytes are expected to be swapped before being passed to DegradedGuidBuilder. | ||
$bytes = (string) hex2bin(implode('', $fields)); | ||
$fields[0] = bin2hex($bytes[3] . $bytes[2] . $bytes[1] . $bytes[0]); | ||
$fields[1] = bin2hex($bytes[5] . $bytes[4]); | ||
$fields[2] = bin2hex($bytes[7] . $bytes[6]); | ||
|
||
$builder = new DegradedGuidBuilder($numberConverter, $timeConverter); | ||
$degradedGuid = $builder->build($codec, $fields); | ||
|
||
$this->assertInstanceOf(DegradedGuid::class, $degradedGuid); | ||
$this->assertSame($expectedFields[0], $degradedGuid->getTimeLowHex()); | ||
$this->assertSame($expectedFields[1], $degradedGuid->getTimeMidHex()); | ||
$this->assertSame($expectedFields[2], $degradedGuid->getTimeHiAndVersionHex()); | ||
$this->assertSame( | ||
$expectedFields[3], | ||
$degradedGuid->getClockSeqHiAndReservedHex() . $degradedGuid->getClockSeqLowHex() | ||
); | ||
$this->assertSame($expectedFields[4], $degradedGuid->getNodeHex()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ramsey\Uuid\Test\Guid; | ||
|
||
use Mockery; | ||
use Ramsey\Uuid\Codec\CodecInterface; | ||
use Ramsey\Uuid\Converter\NumberConverterInterface; | ||
use Ramsey\Uuid\Converter\TimeConverterInterface; | ||
use Ramsey\Uuid\Guid\DegradedGuid; | ||
use Ramsey\Uuid\Test\TestCase; | ||
|
||
class DegradedGuidTest extends TestCase | ||
{ | ||
public function testConstructorConstructsDegradedGuid(): void | ||
{ | ||
$numberConverter = Mockery::mock(NumberConverterInterface::class); | ||
$codec = Mockery::mock(CodecInterface::class); | ||
$timeConverter = Mockery::mock(TimeConverterInterface::class); | ||
|
||
$fields = [ | ||
'b1484596', | ||
'25dc', | ||
'11ea', | ||
'978f', | ||
'2e728ce88125', | ||
]; | ||
|
||
$expectedFields = $fields; | ||
|
||
// Bytes are expected to be swapped before being passed to DegradedGuid. | ||
$bytes = (string) hex2bin(implode('', $fields)); | ||
$fields[0] = bin2hex($bytes[3] . $bytes[2] . $bytes[1] . $bytes[0]); | ||
$fields[1] = bin2hex($bytes[5] . $bytes[4]); | ||
$fields[2] = bin2hex($bytes[7] . $bytes[6]); | ||
|
||
$degradedGuid = new DegradedGuid($fields, $numberConverter, $codec, $timeConverter); | ||
|
||
$this->assertInstanceOf(DegradedGuid::class, $degradedGuid); | ||
$this->assertSame($expectedFields[0], $degradedGuid->getTimeLowHex()); | ||
$this->assertSame($expectedFields[1], $degradedGuid->getTimeMidHex()); | ||
$this->assertSame($expectedFields[2], $degradedGuid->getTimeHiAndVersionHex()); | ||
$this->assertSame( | ||
$expectedFields[3], | ||
$degradedGuid->getClockSeqHiAndReservedHex() . $degradedGuid->getClockSeqLowHex() | ||
); | ||
$this->assertSame($expectedFields[4], $degradedGuid->getNodeHex()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.