diff --git a/.travis.yml b/.travis.yml index e04cbc0..dee5a35 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,15 +7,6 @@ services: jobs: fast_finish: true include: - - php: 5.4 - dist: trusty - - php: 5.5 - dist: trusty - - php: 5.6 - - php: 7.0 - - php: 7.1 - - php: 7.1 - arch: s390x - php: 7.2 - php: 7.2 arch: s390x diff --git a/composer.json b/composer.json index c17c804..1207b64 100644 --- a/composer.json +++ b/composer.json @@ -1,41 +1,30 @@ { "name": "ramsey/uuid", "type": "library", - "description": "Formerly rhumsaa/uuid. A PHP 5.4+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).", - "keywords": ["uuid", "identifier", "guid"], + "description": "A PHP 7 library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).", + "keywords": [ + "uuid", + "identifier", + "guid" + ], "homepage": "https://github.com/ramsey/uuid", "license": "MIT", - "authors": [ - { - "name": "Ben Ramsey", - "email": "ben@benramsey.com", - "homepage": "https://benramsey.com" - }, - { - "name": "Marijn Huizendveld", - "email": "marijn.huizendveld@gmail.com" - }, - { - "name": "Thibaud Fabre", - "email": "thibaud@aztech.io" - } - ], "require": { - "php": "^5.6 | ^7 | ^8", + "php": "^7.2 | ^8", "ext-json": "*", "paragonie/random_compat": "^1 | ^2 | 9.99.99", "symfony/polyfill-ctype": "^1.8" }, "require-dev": { - "codeception/aspect-mock": "^1 | ^2", - "doctrine/annotations": "^1.2", - "goaop/framework": "1.0.0-alpha.2 | ^1 | ^2.1", + "codeception/aspect-mock": "^3", + "doctrine/annotations": "^1.8", + "goaop/framework": "^2", "jakub-onderka/php-parallel-lint": "^1", - "mockery/mockery": "^0.9.11 | ^1", + "mockery/mockery": "^1.3", "moontoast/math": "^1.1", "paragonie/random-lib": "^2", - "php-mock/php-mock-phpunit": "^0.3 | ^1.1", - "phpunit/phpunit": "^5.4 | ^6.5", + "php-mock/php-mock-phpunit": "^2.5", + "phpunit/phpunit": "^8", "squizlabs/php_codesniffer": "^3.5" }, "suggest": { diff --git a/tests/Builder/DefaultUuidBuilderTest.php b/tests/Builder/DefaultUuidBuilderTest.php index e14c31e..75752a6 100644 --- a/tests/Builder/DefaultUuidBuilderTest.php +++ b/tests/Builder/DefaultUuidBuilderTest.php @@ -2,10 +2,10 @@ namespace Ramsey\Uuid\Test\Builder; -use PHPUnit_Framework_TestCase; use Ramsey\Uuid\Builder\DefaultUuidBuilder; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; +use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Uuid; /** @@ -13,7 +13,7 @@ use Ramsey\Uuid\Uuid; * @package Ramsey\Uuid\Test\Builder * @covers Ramsey\Uuid\Builder\DefaultUuidBuilder */ -class DefaultUuidBuilderTest extends PHPUnit_Framework_TestCase +class DefaultUuidBuilderTest extends TestCase { public function testBuildCreatesUuid() diff --git a/tests/Builder/DegradedUuidBuilderTest.php b/tests/Builder/DegradedUuidBuilderTest.php index 2298648..c9a6781 100644 --- a/tests/Builder/DegradedUuidBuilderTest.php +++ b/tests/Builder/DegradedUuidBuilderTest.php @@ -2,18 +2,18 @@ namespace Ramsey\Uuid\Test\Builder; -use PHPUnit_Framework_TestCase; use Ramsey\Uuid\Builder\DegradedUuidBuilder; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\DegradedUuid; +use Ramsey\Uuid\Test\TestCase; /** * Class DegradedUuidBuilderTest * @package Ramsey\Uuid\Test\Builder * @covers Ramsey\Uuid\Builder\DegradedUuidBuilder */ -class DegradedUuidBuilderTest extends PHPUnit_Framework_TestCase +class DegradedUuidBuilderTest extends TestCase { public function testBuildCreatesUuid() diff --git a/tests/Codec/GuidStringCodecTest.php b/tests/Codec/GuidStringCodecTest.php index de00cb3..0fdbe4a 100644 --- a/tests/Codec/GuidStringCodecTest.php +++ b/tests/Codec/GuidStringCodecTest.php @@ -22,7 +22,7 @@ class GuidStringCodecTest extends TestCase /** @var array */ private $fields; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock(); @@ -35,7 +35,7 @@ class GuidStringCodecTest extends TestCase 'node' => '1234abcd4321']; } - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); $this->builder = null; diff --git a/tests/Codec/OrderedTimeCodecTest.php b/tests/Codec/OrderedTimeCodecTest.php index 9cc4a43..c3e4d58 100644 --- a/tests/Codec/OrderedTimeCodecTest.php +++ b/tests/Codec/OrderedTimeCodecTest.php @@ -26,7 +26,7 @@ class OrderedTimeCodecTest extends TestCase /** @var string */ private $optimizedHex = '11d8eebc58e0a7d796690800200c9a66'; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock(); @@ -39,7 +39,7 @@ class OrderedTimeCodecTest extends TestCase 'node' => '0800200c9a66']; } - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); $this->builder = null; @@ -89,7 +89,9 @@ class OrderedTimeCodecTest extends TestCase $string = '61'; $bytes = pack('H*', $string); $codec = new OrderedTimeCodec($this->builder); - $this->setExpectedException('InvalidArgumentException', '$bytes string should contain 16 characters.'); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('$bytes string should contain 16 characters.'); $codec->decodeBytes($bytes); } diff --git a/tests/Codec/StringCodecTest.php b/tests/Codec/StringCodecTest.php index 63dd842..6af432d 100644 --- a/tests/Codec/StringCodecTest.php +++ b/tests/Codec/StringCodecTest.php @@ -24,7 +24,7 @@ class StringCodecTest extends TestCase /** @var string */ private $uuidString = '12345678-1234-abcd-abef-1234abcd4321'; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock(); @@ -37,7 +37,7 @@ class StringCodecTest extends TestCase 'node' => '1234abcd4321']; } - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); $this->builder = null; @@ -95,8 +95,9 @@ class StringCodecTest extends TestCase public function testDecodeThrowsExceptionOnInvalidUuid() { $string = 'invalid-uuid'; - $this->setExpectedException(\InvalidArgumentException::class); $codec = new StringCodec($this->builder); + + $this->expectException(\InvalidArgumentException::class); $codec->decode($string); } @@ -115,7 +116,9 @@ class StringCodecTest extends TestCase $string = '61'; $bytes = pack('H*', $string); $codec = new StringCodec($this->builder); - $this->setExpectedException('InvalidArgumentException', '$bytes string should contain 16 characters.'); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('$bytes string should contain 16 characters.'); $codec->decodeBytes($bytes); } diff --git a/tests/Converter/Number/DegradedNumberConverterTest.php b/tests/Converter/Number/DegradedNumberConverterTest.php index 02a9b2d..0bbcea1 100644 --- a/tests/Converter/Number/DegradedNumberConverterTest.php +++ b/tests/Converter/Number/DegradedNumberConverterTest.php @@ -2,8 +2,9 @@ namespace Ramsey\Uuid\Test\Converter\Number; -use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Converter\Number\DegradedNumberConverter; +use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; +use Ramsey\Uuid\Test\TestCase; /** * Class DegradedNumberConverterTest @@ -12,23 +13,21 @@ use Ramsey\Uuid\Converter\Number\DegradedNumberConverter; */ class DegradedNumberConverterTest extends TestCase { - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testConvertingFromHexThrowsException() { $converter = new DegradedNumberConverter(); + $this->expectException(UnsatisfiedDependencyException::class); + $converter->fromHex('ffff'); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testConvertingToHexThrowsException() { $converter = new DegradedNumberConverter(); + $this->expectException(UnsatisfiedDependencyException::class); + $converter->toHex(0); } } diff --git a/tests/Converter/Time/PhpTimeConverterTest.php b/tests/Converter/Time/PhpTimeConverterTest.php index 47a3a17..5574f34 100644 --- a/tests/Converter/Time/PhpTimeConverterTest.php +++ b/tests/Converter/Time/PhpTimeConverterTest.php @@ -2,15 +2,15 @@ namespace Ramsey\Uuid\Test\Converter; -use PHPUnit_Framework_TestCase; use Ramsey\Uuid\Converter\Time\PhpTimeConverter; +use Ramsey\Uuid\Test\TestCase; /** * Class PhpTimeConverterTest * @package Ramsey\Uuid\Test\Converter * @covers Ramsey\Uuid\Converter\Time\PhpTimeConverter */ -class PhpTimeConverterTest extends PHPUnit_Framework_TestCase +class PhpTimeConverterTest extends TestCase { public function testCalculateTimeReturnsArrayOfTimeSegments() diff --git a/tests/Encoder/TimestampFirstCombCodecTest.php b/tests/Encoder/TimestampFirstCombCodecTest.php index 7a263ae..d94054c 100644 --- a/tests/Encoder/TimestampFirstCombCodecTest.php +++ b/tests/Encoder/TimestampFirstCombCodecTest.php @@ -20,7 +20,7 @@ class TimestampFirstCombCodecTest extends TestCase */ private $builderMock; - protected function setUp() + protected function setUp(): void { $this->builderMock = $this->getMockBuilder(UuidBuilderInterface::class)->getMock(); $this->codec = new TimestampFirstCombCodec($this->builderMock); diff --git a/tests/Encoder/TimestampLastCombCodecTest.php b/tests/Encoder/TimestampLastCombCodecTest.php index 43fcd18..cf0a2bc 100644 --- a/tests/Encoder/TimestampLastCombCodecTest.php +++ b/tests/Encoder/TimestampLastCombCodecTest.php @@ -20,7 +20,7 @@ class TimestampLastCombCodecTest extends TestCase */ private $builderMock; - protected function setUp() + protected function setUp(): void { $this->builderMock = $this->getMockBuilder(UuidBuilderInterface::class)->getMock(); $this->codec = new TimestampLastCombCodec($this->builderMock); diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index e8aaf46..1fc59fd 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -10,7 +10,7 @@ class FunctionsTest extends TestCase { $v1 = \Ramsey\Uuid\v1(); - $this->assertInternalType('string', $v1); + $this->assertIsString($v1); $this->assertSame(Uuid::UUID_TYPE_TIME, Uuid::fromString($v1)->getVersion()); } @@ -19,7 +19,7 @@ class FunctionsTest extends TestCase $ns = Uuid::fromString(Uuid::NAMESPACE_URL); $v3 = \Ramsey\Uuid\v3($ns, 'https://example.com/foo'); - $this->assertInternalType('string', $v3); + $this->assertIsString($v3); $this->assertSame(Uuid::UUID_TYPE_HASH_MD5, Uuid::fromString($v3)->getVersion()); } @@ -27,7 +27,7 @@ class FunctionsTest extends TestCase { $v4 = \Ramsey\Uuid\v4(); - $this->assertInternalType('string', $v4); + $this->assertIsString($v4); $this->assertSame(Uuid::UUID_TYPE_RANDOM, Uuid::fromString($v4)->getVersion()); } @@ -36,7 +36,7 @@ class FunctionsTest extends TestCase $ns = Uuid::fromString(Uuid::NAMESPACE_URL); $v5 = \Ramsey\Uuid\v5($ns, 'https://example.com/foo'); - $this->assertInternalType('string', $v5); + $this->assertIsString($v5); $this->assertSame(Uuid::UUID_TYPE_HASH_SHA1, Uuid::fromString($v5)->getVersion()); } } diff --git a/tests/Generator/CombGeneratorTest.php b/tests/Generator/CombGeneratorTest.php index d356671..9006c46 100644 --- a/tests/Generator/CombGeneratorTest.php +++ b/tests/Generator/CombGeneratorTest.php @@ -2,6 +2,7 @@ namespace Ramsey\Uuid\Test\Generator; +use PHPUnit\Framework\Error\Error as PHPUnitError; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Generator\CombGenerator; use Ramsey\Uuid\Generator\RandomGeneratorInterface; @@ -61,7 +62,7 @@ class CombGeneratorTest extends TestCase $generator = new CombGenerator($randomGenerator, $converter); $returned = $generator->generate($length); - $this->assertInternalType('string', $returned); + $this->assertIsString($returned); $this->assertEquals($expected, $returned); } @@ -77,10 +78,11 @@ class CombGeneratorTest extends TestCase */ public function testGenerateWithLessThanTimestampBytesThrowsException($length) { - $this->setExpectedException('InvalidArgumentException'); $randomGenerator = $this->getMockBuilder(RandomGeneratorInterface::class)->getMock(); $converter = $this->getMockBuilder(NumberConverterInterface::class)->getMock(); $generator = new CombGenerator($randomGenerator, $converter); + + $this->expectException(\InvalidArgumentException::class); $generator->generate($length); } @@ -89,10 +91,11 @@ class CombGeneratorTest extends TestCase */ public function testGenerateWithOddNumberOverTimestampBytesCausesError() { - $this->setExpectedException('PHPUnit_Framework_Error'); $randomGenerator = $this->getMockBuilder(RandomGeneratorInterface::class)->getMock(); $converter = $this->getMockBuilder(NumberConverterInterface::class)->getMock(); $generator = new CombGenerator($randomGenerator, $converter); + + $this->expectException(PHPUnitError::class); $generator->generate(7); } } diff --git a/tests/Generator/DefaultTimeGeneratorTest.php b/tests/Generator/DefaultTimeGeneratorTest.php index 2aebce6..6b4433a 100644 --- a/tests/Generator/DefaultTimeGeneratorTest.php +++ b/tests/Generator/DefaultTimeGeneratorTest.php @@ -29,7 +29,7 @@ class DefaultTimeGeneratorTest extends TestCase private $clockSeq = 4066; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->timeProvider = $this->getMockBuilder(TimeProviderInterface::class)->getMock(); @@ -39,7 +39,7 @@ class DefaultTimeGeneratorTest extends TestCase $this->calculatedTime = ["low" => "83cb98e0", "mid" => "98e0", "hi" => "03cb"]; } - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); $this->timeProvider = null; @@ -107,6 +107,8 @@ class DefaultTimeGeneratorTest extends TestCase */ public function testGenerateAppliesVersionAndVariant() { + $expectedBytes = hex2bin('83cb98e098e003cb8fe2122f80ca9e06'); + $this->timeProvider->method('currentTime') ->willReturn($this->currentTime); $this->timeConverter->method('calculateTime') @@ -126,7 +128,8 @@ class DefaultTimeGeneratorTest extends TestCase $this->timeConverter, $this->timeProvider ); - $defaultTimeGenerator->generate($this->nodeId, $this->clockSeq); + + $this->assertSame($expectedBytes, $defaultTimeGenerator->generate($this->nodeId, $this->clockSeq)); } /** diff --git a/tests/Generator/OpenSslGeneratorTest.php b/tests/Generator/OpenSslGeneratorTest.php index 33f28ae..56558ea 100644 --- a/tests/Generator/OpenSslGeneratorTest.php +++ b/tests/Generator/OpenSslGeneratorTest.php @@ -34,8 +34,8 @@ class OpenSslGeneratorTest extends TestCase $bytes = hex2bin($hex); $openSsl = AspectMock::func('Ramsey\Uuid\Generator', 'openssl_random_pseudo_bytes', $bytes); $generator = new OpenSslGenerator(); - $generator->generate($length); + $this->assertSame($bytes, $generator->generate($length)); $openSsl->verifyInvokedOnce([$length]); } diff --git a/tests/Generator/PeclUuidRandomGeneratorTest.php b/tests/Generator/PeclUuidRandomGeneratorTest.php index 650a93c..fa28854 100644 --- a/tests/Generator/PeclUuidRandomGeneratorTest.php +++ b/tests/Generator/PeclUuidRandomGeneratorTest.php @@ -11,12 +11,11 @@ use AspectMock\Test as AspectMock; */ class PeclUuidRandomGeneratorTest extends PeclUuidTestCase { - private $length = 10; //Doesn't matter, it isn't used + private $length = 10; /** - * This test is just to check collaboration with the PECL UUID extension - not to check - * the correctness of the methods defined in that extension. - * So we are just checking that the UUID methods are called with the right parameters. + * @runInSeparateProcess + * @preserveGlobalState disabled */ public function testGenerateCreatesUuidUsingPeclUuidMethods() { @@ -24,8 +23,9 @@ class PeclUuidRandomGeneratorTest extends PeclUuidTestCase $parse = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); $generator = new PeclUuidRandomGenerator(); - $generator->generate($this->length); + $uuid = $generator->generate($this->length); + $this->assertEquals($this->uuidBinary, $uuid); $create->verifyInvoked([UUID_TYPE_RANDOM]); $parse->verifyInvoked([$this->uuidString]); } @@ -36,10 +36,13 @@ class PeclUuidRandomGeneratorTest extends PeclUuidTestCase */ public function testGenerateReturnsUuidString() { - AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->uuidString); - AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); + $create = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->uuidString); + $parse = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); $generator = new PeclUuidRandomGenerator; $uuid = $generator->generate($this->length); + $this->assertEquals($this->uuidBinary, $uuid); + $create->verifyInvoked([UUID_TYPE_RANDOM]); + $parse->verifyInvoked([$this->uuidString]); } } diff --git a/tests/Generator/PeclUuidTestCase.php b/tests/Generator/PeclUuidTestCase.php index 3a45e90..9cc2055 100644 --- a/tests/Generator/PeclUuidTestCase.php +++ b/tests/Generator/PeclUuidTestCase.php @@ -4,13 +4,6 @@ namespace Ramsey\Uuid\Test\Generator; use Ramsey\Uuid\Test\TestCase; -if (!defined('UUID_TYPE_TIME')) { - define('UUID_TYPE_TIME', 1); -} -if (!defined('UUID_TYPE_RANDOM')) { - define('UUID_TYPE_RANDOM', 4); -} - class PeclUuidTestCase extends TestCase { protected $uuidString = 'b08c6fff-7dc5-e111-9b21-0800200c9a66'; diff --git a/tests/Generator/PeclUuidTimeGeneratorTest.php b/tests/Generator/PeclUuidTimeGeneratorTest.php index a3ed54a..0694888 100644 --- a/tests/Generator/PeclUuidTimeGeneratorTest.php +++ b/tests/Generator/PeclUuidTimeGeneratorTest.php @@ -11,11 +11,9 @@ use AspectMock\Test as AspectMock; */ class PeclUuidTimeGeneratorTest extends PeclUuidTestCase { - /** - * This test is just to check collaboration with the PECL UUID extension - not to check - * the correctness of the methods defined in that extension. - * So we are just checking that the UUID methods are called with the right parameters. + * @runInSeparateProcess + * @preserveGlobalState disabled */ public function testGenerateCreatesUuidUsingPeclUuidMethods() { @@ -23,8 +21,9 @@ class PeclUuidTimeGeneratorTest extends PeclUuidTestCase $parse = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); $generator = new PeclUuidTimeGenerator; - $generator->generate(); + $uuid = $generator->generate(); + $this->assertEquals($this->uuidBinary, $uuid); $create->verifyInvoked([UUID_TYPE_TIME]); $parse->verifyInvoked([$this->uuidString]); } @@ -35,10 +34,13 @@ class PeclUuidTimeGeneratorTest extends PeclUuidTestCase */ public function testGenerateReturnsUuidString() { - AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->uuidString); - AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); + $create = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->uuidString); + $parse = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); $generator = new PeclUuidTimeGenerator; $uuid = $generator->generate(); + $this->assertEquals($this->uuidBinary, $uuid); + $create->verifyInvoked([UUID_TYPE_TIME]); + $parse->verifyInvoked([$this->uuidString]); } } diff --git a/tests/Generator/RandomBytesGeneratorTest.php b/tests/Generator/RandomBytesGeneratorTest.php index cd1092c..b72376d 100644 --- a/tests/Generator/RandomBytesGeneratorTest.php +++ b/tests/Generator/RandomBytesGeneratorTest.php @@ -35,8 +35,8 @@ class RandomBytesGeneratorTest extends TestCase $bytes = hex2bin($hex); $openSsl = AspectMock::func('Ramsey\Uuid\Generator', 'random_bytes', $bytes); $generator = new RandomBytesGenerator(); - $generator->generate($length); + $this->assertSame($bytes, $generator->generate($length)); $openSsl->verifyInvokedOnce([$length]); } diff --git a/tests/Generator/RandomLibAdapterTest.php b/tests/Generator/RandomLibAdapterTest.php index 2ddac40..b146360 100644 --- a/tests/Generator/RandomLibAdapterTest.php +++ b/tests/Generator/RandomLibAdapterTest.php @@ -28,7 +28,8 @@ class RandomLibAdapterTest extends TestCase $generator = $this->getMockBuilder(Generator::class) ->disableOriginalConstructor() ->getMock(); - new RandomLibAdapter($generator); + + $this->assertInstanceOf(RandomLibAdapter::class, new RandomLibAdapter($generator)); } /** @@ -42,7 +43,7 @@ class RandomLibAdapterTest extends TestCase ->once() ->getMock(); - new RandomLibAdapter(); + $this->assertInstanceOf(RandomLibAdapter::class, new RandomLibAdapter()); } public function testGenerateUsesGenerator() diff --git a/tests/Generator/SodiumRandomGeneratorTest.php b/tests/Generator/SodiumRandomGeneratorTest.php index 830d18c..43b111e 100644 --- a/tests/Generator/SodiumRandomGeneratorTest.php +++ b/tests/Generator/SodiumRandomGeneratorTest.php @@ -28,7 +28,7 @@ class SodiumRandomGeneratorTest extends TestCase $bytes = $generator->generate(16); - $this->assertInternalType('string', $bytes); + $this->assertIsString('string', $bytes); $this->assertEquals(16, strlen($bytes)); } diff --git a/tests/Provider/Node/RandomNodeProviderTest.php b/tests/Provider/Node/RandomNodeProviderTest.php index 647c93a..fea2fe5 100644 --- a/tests/Provider/Node/RandomNodeProviderTest.php +++ b/tests/Provider/Node/RandomNodeProviderTest.php @@ -8,7 +8,7 @@ use AspectMock\Test as AspectMock; class RandomNodeProviderTest extends TestCase { - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); AspectMock::clean(); @@ -21,10 +21,13 @@ class RandomNodeProviderTest extends TestCase public function testGetNodeUsesRandomBytes() { $bytes = hex2bin('38a675685d50'); + $expectedNode = '39a675685d50'; $randomBytes = AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); $provider = new RandomNodeProvider(); - $provider->getNode(); + $node = $provider->getNode(); + + $this->assertSame($expectedNode, $node); $randomBytes->verifyInvoked([6]); } @@ -39,10 +42,11 @@ class RandomNodeProviderTest extends TestCase // Expected node has the multicast bit set, and it wasn't set in the bytes. $expectedNode = '39a675685d50'; - AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); + $randomBytes = AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); $provider = new RandomNodeProvider(); $this->assertSame($expectedNode, $provider->getNode()); + $randomBytes->verifyInvoked([6]); } /** @@ -57,10 +61,11 @@ class RandomNodeProviderTest extends TestCase // We expect the same hex value for the node. $expectedNode = $bytesHex; - AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); + $randomBytes = AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); $provider = new RandomNodeProvider(); $this->assertSame($expectedNode, $provider->getNode()); + $randomBytes->verifyInvoked([6]); } /** @@ -72,10 +77,11 @@ class RandomNodeProviderTest extends TestCase $bytes = hex2bin('100000000001'); $expectedNode = '110000000001'; - AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); + $randomBytes = AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); $provider = new RandomNodeProvider(); $this->assertSame($expectedNode, $provider->getNode()); + $randomBytes->verifyInvoked([6]); } public function testGetNodeAlwaysSetsMulticastBit() diff --git a/tests/Provider/Node/SystemNodeProviderTest.php b/tests/Provider/Node/SystemNodeProviderTest.php index 0cafa72..7569d43 100644 --- a/tests/Provider/Node/SystemNodeProviderTest.php +++ b/tests/Provider/Node/SystemNodeProviderTest.php @@ -253,10 +253,12 @@ class SystemNodeProviderTest extends TestCase /*/ Act /*/ $provider = new SystemNodeProvider(); $provider->getNode(); - $provider->getNode(); + $node = $provider->getNode(); /*/ Assert /*/ $this->assertMockFunctions(null, null, ['netstat -ie 2>&1'], ['PHP_OS'], ['disable_functions']); + + $this->assertFalse($node); } /** @@ -282,7 +284,7 @@ class SystemNodeProviderTest extends TestCase /*/ Act /*/ $provider = new SystemNodeProvider(); - $provider->getNode(); + $node = $provider->getNode(); /*/ Assert /*/ $globBodyAssert = null; @@ -301,6 +303,8 @@ class SystemNodeProviderTest extends TestCase ['disable_functions'], $isReadableAssert ); + + $this->assertFalse($node); } /** diff --git a/tests/Provider/Time/FixedTimeProviderTest.php b/tests/Provider/Time/FixedTimeProviderTest.php index 2f13943..35b38d5 100644 --- a/tests/Provider/Time/FixedTimeProviderTest.php +++ b/tests/Provider/Time/FixedTimeProviderTest.php @@ -10,7 +10,7 @@ class FixedTimeProviderTest extends TestCase public function testConstructorRequiresSecAndUsec() { - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $provider = new FixedTimeProvider([]); } diff --git a/tests/Provider/Time/SystemTimeProviderTest.php b/tests/Provider/Time/SystemTimeProviderTest.php index 8210efd..ef3ff76 100644 --- a/tests/Provider/Time/SystemTimeProviderTest.php +++ b/tests/Provider/Time/SystemTimeProviderTest.php @@ -26,7 +26,8 @@ class SystemTimeProviderTest extends TestCase $timestamp = ['sec' => 1458844556, 'usec' => 200997]; $func = AspectMock::func('Ramsey\Uuid\Provider\Time', 'gettimeofday', $timestamp); $provider = new SystemTimeProvider(); - $provider->currentTime(); + + $this->assertSame($timestamp, $provider->currentTime()); $func->verifyInvokedOnce(); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 5a1cd05..cd674cb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase as PhpUnitTestCase; class TestCase extends PhpUnitTestCase { - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); AspectMock::clean(); diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 5304b78..2335a20 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -2,10 +2,14 @@ namespace Ramsey\Uuid\Test; +use InvalidArgumentException; use Ramsey\Uuid\Codec\TimestampFirstCombCodec; use Ramsey\Uuid\Codec\TimestampLastCombCodec; use Ramsey\Uuid\Converter\Number\DegradedNumberConverter; use Ramsey\Uuid\DegradedUuid; +use Ramsey\Uuid\Exception\InvalidUuidStringException; +use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; +use Ramsey\Uuid\Exception\UnsupportedOperationException; use Ramsey\Uuid\FeatureSet; use Ramsey\Uuid\Provider\Time\FixedTimeProvider; use Ramsey\Uuid\Generator\CombGenerator; @@ -17,7 +21,7 @@ use stdClass; class UuidTest extends TestCase { - protected function setUp() + protected function setUp(): void { Uuid::setFactory(new UuidFactory()); } @@ -74,8 +78,6 @@ class UuidTest extends TestCase $this->assertEquals(bin2hex($uuid->getBytes()), bin2hex($guid->getBytes())); } - /** - */ public function testFromStringWithCurlyBraces() { $uuid = Uuid::fromString('{ff6f8cb0-c57d-11e1-9b21-0800200c9a66}'); @@ -83,21 +85,19 @@ class UuidTest extends TestCase $this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString()); } - /** - * @expectedException Ramsey\Uuid\Exception\InvalidUuidStringException - * @expectedExceptionMessage Invalid UUID string: - */ public function testFromStringWithInvalidUuidString() { + $this->expectException(InvalidUuidStringException::class); + $this->expectExceptionMessage('Invalid UUID string:'); + $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21'); } - /** - * @expectedException Ramsey\Uuid\Exception\InvalidUuidStringException - * @expectedExceptionMessage Invalid UUID string: - */ public function testFromStringWithTrailingNewLine() { + $this->expectException(InvalidUuidStringException::class); + $this->expectExceptionMessage('Invalid UUID string:'); + Uuid::fromString("d0d5f586-21d1-470c-8088-55c8857728dc\n"); } @@ -220,9 +220,6 @@ class UuidTest extends TestCase $this->assertEquals('1582-10-15T00:00:00+00:00', $uuid->getDateTime()->format('c')); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testGetDateTimeThrownException() { Uuid::setFactory(new UuidFactory(new FeatureSet(false, true, true))); @@ -232,17 +229,19 @@ class UuidTest extends TestCase $this->assertInstanceOf(DegradedUuid::class, $uuid); $this->assertInstanceOf(DegradedNumberConverter::class, $uuid->getNumberConverter()); + $this->expectException(UnsatisfiedDependencyException::class); + $date = $uuid->getDateTime(); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsupportedOperationException - * @expectedExceptionMessage Not a time-based UUID - */ public function testGetDateTimeFromNonVersion1Uuid() { // Using a version 4 UUID to test $uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de'); + + $this->expectException(UnsupportedOperationException::class); + $this->expectExceptionMessage('Not a time-based UUID'); + $date = $uuid->getDateTime(); } @@ -266,14 +265,14 @@ class UuidTest extends TestCase $this->assertEquals($fields, $uuid->getFields()); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testGetFields32Bit() { Uuid::setFactory(new UuidFactory(new FeatureSet(false, true))); $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); + + $this->expectException(UnsatisfiedDependencyException::class); + $fields = $uuid->getFields(); } @@ -306,14 +305,14 @@ class UuidTest extends TestCase $this->assertEquals('11178224546741000806', $uuid->getLeastSignificantBits()->getValue()); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testGetLeastSignificantBitsException() { Uuid::setFactory(new UuidFactory(new FeatureSet(false, false, true))); $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); + + $this->expectException(UnsatisfiedDependencyException::class); + $bn = $uuid->getLeastSignificantBits(); } @@ -336,14 +335,14 @@ class UuidTest extends TestCase $this->assertEquals('18406084892941947361', $uuid->getMostSignificantBits()->getValue()); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testGetMostSignificantBitsException() { Uuid::setFactory(new UuidFactory(new FeatureSet(false, false, true))); $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); + + $this->expectException(UnsatisfiedDependencyException::class); + $bn = $uuid->getMostSignificantBits(); } @@ -365,14 +364,14 @@ class UuidTest extends TestCase $this->assertEquals(8796630719078, $uuid->getNode()); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testGetNode32Bit() { Uuid::setFactory(new UuidFactory(new FeatureSet(false, true))); $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); + + $this->expectException(UnsatisfiedDependencyException::class); + $node = $uuid->getNode(); } @@ -410,14 +409,14 @@ class UuidTest extends TestCase $this->assertEquals(4285500592, $uuid->getTimeLow()); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testGetTimeLow32Bit() { Uuid::setFactory(new UuidFactory(new FeatureSet(false, true))); $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); + + $this->expectException(UnsatisfiedDependencyException::class); + $timeLow = $uuid->getTimeLow(); } @@ -473,36 +472,36 @@ class UuidTest extends TestCase $this->assertEquals('00001540901e600', $uuid->getTimestampHex()); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsupportedOperationException - * @expectedExceptionMessage Not a time-based UUID - */ public function testGetTimestampFromNonVersion1Uuid() { // Using a version 4 UUID to test $uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de'); + + $this->expectException(UnsupportedOperationException::class); + $this->expectExceptionMessage('Not a time-based UUID'); + $ts = $uuid->getTimestamp(); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsupportedOperationException - * @expectedExceptionMessage Not a time-based UUID - */ public function testGetTimestampHexFromNonVersion1Uuid() { // Using a version 4 UUID to test $uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de'); + + $this->expectException(UnsupportedOperationException::class); + $this->expectExceptionMessage('Not a time-based UUID'); + $ts = $uuid->getTimestampHex(); } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testGetTimestamp32Bit() { Uuid::setFactory(new UuidFactory(new FeatureSet(false, true))); $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); + + $this->expectException(UnsatisfiedDependencyException::class); + $ts = $uuid->getTimestamp(); } @@ -716,30 +715,27 @@ class UuidTest extends TestCase } } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Invalid node value - */ public function testUuid1WithOutOfBoundsNode() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid node value'); + $uuid = Uuid::uuid1(9223372036854775808); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Invalid node value - */ public function testUuid1WithNonHexadecimalNode() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid node value'); + $uuid = Uuid::uuid1('db77e160355g'); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Invalid node value - */ public function testUuid1WithNon48bitNumber() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid node value'); + $uuid = Uuid::uuid1('db77e160355ef'); } @@ -1345,13 +1341,12 @@ class UuidTest extends TestCase } } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testCalculateUuidTimeThrownException() { Uuid::setFactory(new UuidFactory(new FeatureSet(false, true, true))); + $this->expectException(UnsatisfiedDependencyException::class); + $uuid = Uuid::uuid1(0x00007ffffffe, 0x1669); } @@ -1511,19 +1506,17 @@ class UuidTest extends TestCase $this->assertEquals($guid->toString(), $parsedGuid->toString()); } - /** - * @expectedException InvalidArgumentException - */ public function testFromBytesArgumentTooShort() { + $this->expectException(InvalidArgumentException::class); + Uuid::fromBytes('thisisveryshort'); } - /** - * @expectedException InvalidArgumentException - */ public function testFromBytesArgumentTooLong() { + $this->expectException(InvalidArgumentException::class); + Uuid::fromBytes('thisisabittoolong'); } @@ -1570,12 +1563,12 @@ class UuidTest extends TestCase 'bytes' => 'AAAAAAAAAAAAAAAAAAAAAA==', 'int' => '0', 'fields' => [ - 'time_low' => '0', - 'time_mid' => '0', - 'time_hi_and_version' => '0', - 'clock_seq_hi_and_reserved' => '0', - 'clock_seq_low' => '0', - 'node' => '0', + 'time_low' => '00000000', + 'time_mid' => '0000', + 'time_hi_and_version' => '0000', + 'clock_seq_hi_and_reserved' => '00', + 'clock_seq_low' => '00', + 'node' => '000000000000', ], 'urn' => 'urn:uuid:00000000-0000-0000-0000-000000000000', 'time' => '0', @@ -1590,11 +1583,11 @@ class UuidTest extends TestCase 'bytes' => 'AAECAwQFBgcICQoLDA0ODw==', 'int' => '5233100606242806050955395731361295', 'fields' => [ - 'time_low' => '10203', - 'time_mid' => '405', - 'time_hi_and_version' => '607', - 'clock_seq_hi_and_reserved' => '8', - 'clock_seq_low' => '9', + 'time_low' => '00010203', + 'time_mid' => '0405', + 'time_hi_and_version' => '0607', + 'clock_seq_hi_and_reserved' => '08', + 'clock_seq_low' => '09', 'node' => '0a0b0c0d0e0f', ], 'urn' => 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', @@ -1879,14 +1872,14 @@ class UuidTest extends TestCase } } - /** - * @expectedException Ramsey\Uuid\Exception\UnsatisfiedDependencyException - */ public function testGetInteger() { Uuid::setFactory(new UuidFactory(new FeatureSet(false, false, true))); - $uuid = Uuid::uuid1(); + $uuid = Uuid::uuid4(); + + $this->expectException(UnsatisfiedDependencyException::class); + $uuid->getInteger(); } @@ -1908,12 +1901,11 @@ class UuidTest extends TestCase $this->assertTrue($uuid->equals($unserializedUuid)); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Invalid UUID string: - */ public function testUuid3WithEmptyNamespace() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid UUID string:'); + $uuid = Uuid::uuid3('', ''); } @@ -1945,12 +1937,11 @@ class UuidTest extends TestCase $this->assertEquals('19826852-5007-3022-a72a-212f66e9fac3', $uuid->toString()); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Invalid UUID string: - */ public function testUuid5WithEmptyNamespace() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid UUID string:'); + $uuid = Uuid::uuid5('', ''); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 22f23fa..9f95a84 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,6 +8,30 @@ use AspectMock\Kernel; require_once __DIR__ . '/../vendor/autoload.php'; // composer autoload require_once __DIR__ . '/phpstan-bootstrap.php'; +if (!function_exists('uuid_create')) { + // Create stub of method so AspectMock can mock this function + // if it doesn't exist in PHP. + function uuid_create() + { + } +} + +if (!function_exists('uuid_parse')) { + // Create stub of method so AspectMock can mock this function + // if it doesn't exist in PHP. + function uuid_parse() + { + } +} + +if (!defined('UUID_TYPE_TIME')) { + define('UUID_TYPE_TIME', 1); +} + +if (!defined('UUID_TYPE_RANDOM')) { + define('UUID_TYPE_RANDOM', 4); +} + $kernel = Kernel::getInstance(); $kernel->init([ 'debug' => true,