Use internal exceptions

Fixes #254
This commit is contained in:
Ben Ramsey
2019-12-19 12:20:53 -06:00
parent 0d7b8c2b7a
commit 12896f06db
36 changed files with 336 additions and 40 deletions
+13
View File
@@ -18,6 +18,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Allow use of the [GMP extension](https://www.php.net/gmp) for number and time
conversion through the addition of `Converter\Number\GmpConverter` and
`Converter\Time\GmpTimeConverter`.
* Add an internal `InvalidArgumentException` that descends from the built-in
PHP `\InvalidArgumentException`. All places that used to throw
`\InvalidArgumentException` now throw `Ramsey\Uuid\Exception\InvalidArgumentException`.
This should not cause any BC breaks, however.
* Add an internal `DateTimeException` that descends from the built-in PHP
`\RuntimeException`. `Uuid::getDateTime()` and `DegradedUuid::getDateTime()`
may throw this exception if `\DateTimeImmutable` throws an error or exception.
* Add `RandomSourceException` that descends from the built-in PHP
`\RuntimeException`. DefaultTimeGenerator, RandomBytesGenerator, and
RandomNodeProvider may throw this exception if `random_bytes()` or
`random_int()` throw an error or exception.
### Changed
@@ -37,6 +48,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* `NumberConverterInterface::toHex(string $number): string`
* `TimeConverterInterface::calculateTime(string $seconds, string $microSeconds): array`
* `TimeConverterInterface::convertTime(string $timestamp): string`
* `UnsatisfiedDependencyException` and `UnsupportedOperationException` are now
descended from `\LogicException`. Previously, they descended from `\RuntimeException`.
### Deprecated
+1 -1
View File
@@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
use InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\UuidInterface;
+1 -1
View File
@@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
use InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\UuidInterface;
/**
+1 -1
View File
@@ -14,8 +14,8 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
use InvalidArgumentException;
use Ramsey\Uuid\Builder\UuidBuilderInterface;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
+1 -1
View File
@@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
use InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\UuidInterface;
+1 -1
View File
@@ -14,11 +14,11 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Converter\Number;
use InvalidArgumentException;
use Moontoast\Math\BigNumber;
use Ramsey\Uuid\Converter\DependencyCheckTrait;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\NumberStringTrait;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
/**
+1 -1
View File
@@ -14,10 +14,10 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Converter\Number;
use InvalidArgumentException;
use Ramsey\Uuid\Converter\DependencyCheckTrait;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\NumberStringTrait;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
/**
+1 -1
View File
@@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Converter;
use InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidArgumentException;
/**
* Provides shared functionality to check the values of string numbers for
@@ -14,11 +14,11 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Converter\Time;
use InvalidArgumentException;
use Moontoast\Math\BigNumber;
use Ramsey\Uuid\Converter\DependencyCheckTrait;
use Ramsey\Uuid\Converter\NumberStringTrait;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
/**
+1 -1
View File
@@ -14,10 +14,10 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Converter\Time;
use InvalidArgumentException;
use Ramsey\Uuid\Converter\DependencyCheckTrait;
use Ramsey\Uuid\Converter\NumberStringTrait;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
/**
+1 -1
View File
@@ -14,10 +14,10 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Converter\Time;
use InvalidArgumentException;
use Ramsey\Uuid\Converter\DependencyCheckTrait;
use Ramsey\Uuid\Converter\NumberStringTrait;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
/**
+16 -5
View File
@@ -16,6 +16,7 @@ namespace Ramsey\Uuid;
use DateTimeImmutable;
use DateTimeInterface;
use Ramsey\Uuid\Exception\DateTimeException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
@@ -31,6 +32,8 @@ class DegradedUuid extends Uuid
/**
* @return DateTimeImmutable An immutable instance of DateTimeInterface
*
* @throws DateTimeException if DateTime throws an exception/error
* @throws UnsatisfiedDependencyException if large integer support is not available
* @throws UnsupportedOperationException if UUID is not time-based
*/
public function getDateTime(): DateTimeInterface
@@ -42,11 +45,19 @@ class DegradedUuid extends Uuid
$time = $this->numberConverter->fromHex($this->getTimestampHex());
$unixTime = $this->timeConverter->convertTime($time);
return new DateTimeImmutable("@{$unixTime}");
try {
return new DateTimeImmutable("@{$unixTime}");
} catch (\Throwable $exception) {
throw new DateTimeException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}
}
/**
* @throws UnsatisfiedDependencyException if called on a 32-bit system
* @throws UnsatisfiedDependencyException if large integer support is not available
*
* @inheritDoc
*/
@@ -60,7 +71,7 @@ class DegradedUuid extends Uuid
}
/**
* @throws UnsatisfiedDependencyException if called on a 32-bit system
* @throws UnsatisfiedDependencyException if large integer support is not available
*/
public function getNode(): int
{
@@ -73,7 +84,7 @@ class DegradedUuid extends Uuid
}
/**
* @throws UnsatisfiedDependencyException if called on a 32-bit system
* @throws UnsatisfiedDependencyException if large integer support is not available
*/
public function getTimeLow(): int
{
@@ -86,7 +97,7 @@ class DegradedUuid extends Uuid
}
/**
* @throws UnsatisfiedDependencyException if called on a 32-bit system
* @throws UnsatisfiedDependencyException if large integer support is not available
* @throws UnsupportedOperationException if UUID is not time-based
*/
public function getTimestamp(): int
+24
View File
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Exception;
use RuntimeException as PhpRuntimeException;
/**
* Thrown to indicate that the PHP DateTime extension encountered an exception/error
*/
class DateTimeException extends PhpRuntimeException
{
}
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Exception;
use InvalidArgumentException as PhpInvalidArgumentException;
/**
* Thrown to indicate that the argument received is not valid
*/
class InvalidArgumentException extends PhpInvalidArgumentException
{
}
+4 -3
View File
@@ -14,10 +14,11 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Exception;
use InvalidArgumentException;
/**
* Thrown to indicate that the parsed UUID string is invalid.
* Thrown to indicate that the string received is not a valid UUID
*
* The InvalidArgumentException that this extends is the ramsey/uuid version
* of this exception. It exists in the same namespace as this class.
*/
class InvalidUuidStringException extends InvalidArgumentException
{
+27
View File
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Exception;
use RuntimeException as PhpRuntimeException;
/**
* Thrown to indicate that the source of random data encountered an error
*
* This exception is used mostly to indicate that random_bytes() or random_int()
* threw an exception. However, it may be used for other sources of random data.
*/
class RandomSourceException extends PhpRuntimeException
{
}
@@ -14,12 +14,12 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Exception;
use RuntimeException;
use LogicException as PhpLogicException;
/**
* Thrown to indicate that the requested operation has dependencies that have not
* been satisfied.
* been satisfied
*/
class UnsatisfiedDependencyException extends RuntimeException
class UnsatisfiedDependencyException extends PhpLogicException
{
}
@@ -14,11 +14,11 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Exception;
use RuntimeException;
use LogicException as PhpLogicException;
/**
* Thrown to indicate that the requested operation is not supported.
* Thrown to indicate that the requested operation is not supported
*/
class UnsupportedOperationException extends RuntimeException
class UnsupportedOperationException extends PhpLogicException
{
}
+1 -1
View File
@@ -14,8 +14,8 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Generator;
use InvalidArgumentException;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Exception\InvalidArgumentException;
/**
* CombGenerator generates COMBs (combined UUID/timestamp)
+13 -3
View File
@@ -14,9 +14,10 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Generator;
use InvalidArgumentException;
use Ramsey\Uuid\BinaryUtils;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\RandomSourceException;
use Ramsey\Uuid\Provider\NodeProviderInterface;
use Ramsey\Uuid\Provider\TimeProviderInterface;
@@ -53,6 +54,7 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
/**
* @throws InvalidArgumentException if the parameters contain invalid values
* @throws RandomSourceException if random_int() throws an exception/error
*
* @inheritDoc
*/
@@ -61,8 +63,16 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
$node = $this->getValidNode($node);
if ($clockSeq === null) {
// This does not use "stable storage"; see RFC 4122, Section 4.2.1.1.
$clockSeq = random_int(0, 0x3fff);
try {
// This does not use "stable storage"; see RFC 4122, Section 4.2.1.1.
$clockSeq = random_int(0, 0x3fff);
} catch (\Throwable $exception) {
throw new RandomSourceException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}
}
// Create a 60-bit time value as a count of 100-nanosecond intervals
+16 -1
View File
@@ -14,6 +14,8 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Generator;
use Ramsey\Uuid\Exception\RandomSourceException;
/**
* RandomBytesGenerator generates strings of random binary data using the
* built-in `random_bytes()` PHP function
@@ -22,8 +24,21 @@ namespace Ramsey\Uuid\Generator;
*/
class RandomBytesGenerator implements RandomGeneratorInterface
{
/**
* @throws RandomSourceException if random_bytes() throws an exception/error
*
* @inheritDoc
*/
public function generate(int $length): string
{
return random_bytes($length);
try {
return random_bytes($length);
} catch (\Throwable $exception) {
throw new RandomSourceException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}
}
}
+10 -1
View File
@@ -14,6 +14,7 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Provider\Node;
use Ramsey\Uuid\Exception\RandomSourceException;
use Ramsey\Uuid\Provider\NodeProviderInterface;
/**
@@ -28,7 +29,15 @@ class RandomNodeProvider implements NodeProviderInterface
*/
public function getNode()
{
$nodeBytes = random_bytes(6);
try {
$nodeBytes = random_bytes(6);
} catch (\Throwable $exception) {
throw new RandomSourceException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}
// Split the node bytes for math on 32-bit systems.
$nodeMsb = substr($nodeBytes, 0, 3);
+1 -1
View File
@@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Provider\Time;
use InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Provider\TimeProviderInterface;
/**
+16 -1
View File
@@ -19,6 +19,7 @@ use DateTimeInterface;
use Ramsey\Uuid\Codec\CodecInterface;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Exception\DateTimeException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
@@ -346,6 +347,7 @@ class Uuid implements UuidInterface
* @return DateTimeImmutable An immutable instance of DateTimeInterface
*
* @throws UnsupportedOperationException if UUID is not time-based
* @throws DateTimeException if DateTime throws an exception/error
*/
public function getDateTime(): DateTimeInterface
{
@@ -355,7 +357,15 @@ class Uuid implements UuidInterface
$unixTime = $this->timeConverter->convertTime((string) $this->getTimestamp());
return new DateTimeImmutable("@{$unixTime}");
try {
return new DateTimeImmutable("@{$unixTime}");
} catch (\Throwable $exception) {
throw new DateTimeException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}
}
/**
@@ -403,6 +413,8 @@ class Uuid implements UuidInterface
}
/**
* @throws UnsatisfiedDependencyException if large integer support is not available
*
* @inheritDoc
*/
public function getInteger()
@@ -478,6 +490,8 @@ class Uuid implements UuidInterface
* @link http://tools.ietf.org/html/rfc4122#section-4.1.6 RFC 4122, § 4.1.6: Node
*
* @return int Unsigned 48-bit integer value of node
*
* @throws UnsatisfiedDependencyException if large integer support is not available
*/
public function getNode(): int
{
@@ -541,6 +555,7 @@ class Uuid implements UuidInterface
*
* @link http://tools.ietf.org/html/rfc4122#section-4.1.4 RFC 4122, § 4.1.4: Timestamp
*
* @throws UnsatisfiedDependencyException if large integer support is not available
* @throws UnsupportedOperationException if UUID is not time-based
*/
public function getTimestamp(): int
@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Converter\Number;
use AspectMock\Test as AspectMock;
use InvalidArgumentException;
use Ramsey\Uuid\Converter\Number\BigNumberConverter;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Ramsey\Uuid\Test\TestCase;
+1 -1
View File
@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Converter\Number;
use AspectMock\Test as AspectMock;
use InvalidArgumentException;
use Ramsey\Uuid\Converter\Number\GmpConverter;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Ramsey\Uuid\Test\TestCase;
@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Converter\Time;
use AspectMock\Test as AspectMock;
use InvalidArgumentException;
use Ramsey\Uuid\Converter\Time\BigNumberTimeConverter;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Ramsey\Uuid\Test\TestCase;
@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Converter\Time;
use AspectMock\Test as AspectMock;
use InvalidArgumentException;
use Ramsey\Uuid\Converter\Time\GmpTimeConverter;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Ramsey\Uuid\Test\TestCase;
@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Converter\Time;
use InvalidArgumentException;
use Mockery;
use Ramsey\Uuid\Converter\Time\PhpTimeConverter;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Ramsey\Uuid\Test\TestCase;
+51
View File
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Ramsey\Uuid\Test;
use Mockery;
use Ramsey\Uuid\Builder\DegradedUuidBuilder;
use Ramsey\Uuid\Codec\StringCodec;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\DegradedUuid;
use Ramsey\Uuid\Exception\DateTimeException;
use Ramsey\Uuid\UuidFactory;
class DegradedUuidTest extends TestCase
{
public function testGetDateTime(): void
{
$numberConverter = Mockery::mock(NumberConverterInterface::class);
$numberConverter
->shouldReceive('fromHex')
->once()
->andReturn('aFromHexValue');
$timeConverter = Mockery::mock(TimeConverterInterface::class);
$timeConverter
->shouldReceive('convertTime')
->once()
->with('aFromHexValue')
->andReturn('foobar');
$builder = new DegradedUuidBuilder($numberConverter, $timeConverter);
$codec = new StringCodec($builder);
$factory = new UuidFactory();
$factory->setCodec($codec);
$uuid = $factory->fromString('b1484596-25dc-11ea-978f-2e728ce88125');
$this->assertInstanceOf(DegradedUuid::class, $uuid);
$this->expectException(DateTimeException::class);
$this->expectExceptionMessage(
'DateTimeImmutable::__construct(): Failed to parse time string '
. '(@foobar) at position 0 (@): Unexpected character'
);
$uuid->getDateTime();
}
}
+1 -1
View File
@@ -5,10 +5,10 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Generator;
use Exception;
use InvalidArgumentException;
use PHPUnit\Framework\Error\Error as PHPUnitError;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Generator\CombGenerator;
use Ramsey\Uuid\Generator\RandomGeneratorInterface;
use Ramsey\Uuid\Test\TestCase;
@@ -5,10 +5,12 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Generator;
use AspectMock\Test as AspectMock;
use Exception;
use Mockery;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\BinaryUtils;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Exception\RandomSourceException;
use Ramsey\Uuid\Generator\DefaultTimeGenerator;
use Ramsey\Uuid\Provider\NodeProviderInterface;
use Ramsey\Uuid\Provider\TimeProviderInterface;
@@ -217,4 +219,26 @@ class DefaultTimeGeneratorTest extends TestCase
$defaultTimeGenerator->generate($this->nodeId);
$randomInt->verifyInvokedOnce([0, 0x3fff]);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGenerateThrowsExceptionWhenExceptionThrownByRandomint(): void
{
AspectMock::func('Ramsey\Uuid\Generator', 'random_int', function (): void {
throw new Exception('Could not gather sufficient random data');
});
$defaultTimeGenerator = new DefaultTimeGenerator(
$this->nodeProvider,
$this->timeConverter,
$this->timeProvider
);
$this->expectException(RandomSourceException::class);
$this->expectExceptionMessage('Could not gather sufficient random data');
$defaultTimeGenerator->generate($this->nodeId);
}
}
@@ -6,6 +6,7 @@ namespace Ramsey\Uuid\Test\Generator;
use AspectMock\Test as AspectMock;
use Exception;
use Ramsey\Uuid\Exception\RandomSourceException;
use Ramsey\Uuid\Generator\RandomBytesGenerator;
use Ramsey\Uuid\Test\TestCase;
@@ -54,4 +55,22 @@ class RandomBytesGeneratorTest extends TestCase
$generator = new RandomBytesGenerator();
$this->assertEquals($bytes, $generator->generate($length));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGenerateThrowsExceptionWhenExceptionThrownByRandombytes(): void
{
AspectMock::func('Ramsey\Uuid\Generator', 'random_bytes', function (): void {
throw new Exception('Could not gather sufficient random data');
});
$generator = new RandomBytesGenerator();
$this->expectException(RandomSourceException::class);
$this->expectExceptionMessage('Could not gather sufficient random data');
$generator->generate(16);
}
}
@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Provider\Node;
use AspectMock\Test as AspectMock;
use Exception;
use Ramsey\Uuid\Exception\RandomSourceException;
use Ramsey\Uuid\Provider\Node\RandomNodeProvider;
use Ramsey\Uuid\Test\TestCase;
@@ -110,4 +112,22 @@ class RandomNodeProviderTest extends TestCase
$this->assertSame('010000000000', $node);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeThrowsExceptionWhenExceptionThrownByRandombytes(): void
{
AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', function (): void {
throw new Exception('Could not gather sufficient random data');
});
$provider = new RandomNodeProvider();
$this->expectException(RandomSourceException::class);
$this->expectExceptionMessage('Could not gather sufficient random data');
$provider->getNode();
}
}
@@ -6,7 +6,7 @@ namespace Ramsey\Uuid\Test\Provider\Node;
use AspectMock\Proxy\FuncProxy;
use AspectMock\Test as AspectMock;
use InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Provider\Node\SystemNodeProvider;
use Ramsey\Uuid\Test\TestCase;
+34 -1
View File
@@ -5,12 +5,18 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test;
use DateTimeInterface;
use InvalidArgumentException;
use Mockery;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\Builder\DefaultUuidBuilder;
use Ramsey\Uuid\Codec\StringCodec;
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
use Ramsey\Uuid\Codec\TimestampLastCombCodec;
use Ramsey\Uuid\Converter\Number\DegradedNumberConverter;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\DegradedUuid;
use Ramsey\Uuid\Exception\DateTimeException;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
@@ -2111,4 +2117,31 @@ class UuidTest extends TestCase
$uuid = Uuid::fromString('886313e1-3b8a-5372-9b90-0c9aee199e5d');
$this->assertEquals($uuid->getVersion(), Uuid::UUID_TYPE_HASH_SHA1);
}
public function testGetDateTimeThrowsExceptionWhenDateTimeCannotParseString(): void
{
$numberConverter = Mockery::mock(NumberConverterInterface::class);
$timeConverter = Mockery::mock(TimeConverterInterface::class);
$timeConverter
->shouldReceive('convertTime')
->once()
->andReturn('foobar');
$builder = new DefaultUuidBuilder($numberConverter, $timeConverter);
$codec = new StringCodec($builder);
$factory = new UuidFactory();
$factory->setCodec($codec);
$uuid = $factory->fromString('b1484596-25dc-11ea-978f-2e728ce88125');
$this->expectException(DateTimeException::class);
$this->expectExceptionMessage(
'DateTimeImmutable::__construct(): Failed to parse time string '
. '(@foobar) at position 0 (@): Unexpected character'
);
$uuid->getDateTime();
}
}