From b941aa1a9a2ca9e76971c56f9277269aea4caa1d Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Thu, 29 Oct 2020 18:24:14 -0500 Subject: [PATCH] Prepare test suite for PHP 8 --- composer.json | 10 ++-- src/Generator/DceSecurityGenerator.php | 4 +- src/Guid/Fields.php | 4 ++ .../Dce/SystemDceSecurityProvider.php | 6 +-- src/Rfc4122/Fields.php | 1 + src/Rfc4122/VariantTrait.php | 1 + src/UuidFactory.php | 8 ++- tests/Generator/DefaultTimeGeneratorTest.php | 15 +++--- tests/Generator/PeclUuidNameGeneratorTest.php | 8 ++- .../Generator/PeclUuidRandomGeneratorTest.php | 29 ++++------ tests/Generator/PeclUuidTimeGeneratorTest.php | 29 ++++------ tests/Generator/RandomBytesGeneratorTest.php | 32 ++++------- .../Provider/Node/RandomNodeProviderTest.php | 53 +++++++------------ .../Provider/Node/SystemNodeProviderTest.php | 52 ++++++++++++------ tests/TestCase.php | 7 +-- tests/bootstrap.php | 17 ------ 16 files changed, 123 insertions(+), 153 deletions(-) diff --git a/composer.json b/composer.json index 73402a74..b3084719 100644 --- a/composer.json +++ b/composer.json @@ -17,17 +17,15 @@ "symfony/polyfill-ctype": "^1.8" }, "require-dev": { - "codeception/aspect-mock": "^3", - "dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7.0", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", "doctrine/annotations": "^1.8", - "goaop/framework": "^2", "mockery/mockery": "^1.3", "moontoast/math": "^1.1", "paragonie/random-lib": "^2", + "php-mock/php-mock": "^2.2", "php-mock/php-mock-mockery": "^1.3", - "php-mock/php-mock-phpunit": "^2.5", "php-parallel-lint/php-parallel-lint": "^1.1", - "phpbench/phpbench": "^0.17.1", + "phpbench/phpbench": "1.0.0-alpha2", "phpstan/extension-installer": "^1.0", "phpstan/phpstan": "^0.12", "phpstan/phpstan-mockery": "^0.12", @@ -38,6 +36,8 @@ "squizlabs/php_codesniffer": "^3.5", "vimeo/psalm": "^3.18" }, + "minimum-stability": "dev", + "prefer-stable": true, "suggest": { "ext-bcmath": "Enables faster math with arbitrary-precision integers using BCMath.", "ext-ctype": "Enables faster processing of character classification using ctype functions.", diff --git a/src/Generator/DceSecurityGenerator.php b/src/Generator/DceSecurityGenerator.php index a3f07f2f..79f05665 100644 --- a/src/Generator/DceSecurityGenerator.php +++ b/src/Generator/DceSecurityGenerator.php @@ -137,8 +137,8 @@ public function generate( ); } - $domainByte = pack('n', $localDomain)[1]; - $identifierBytes = hex2bin(str_pad($identifierHex, 8, '0', STR_PAD_LEFT)); + $domainByte = (string) pack('n', $localDomain)[1]; + $identifierBytes = (string) hex2bin(str_pad($identifierHex, 8, '0', STR_PAD_LEFT)); if ($node instanceof Hexadecimal) { $node = $node->toString(); diff --git a/src/Guid/Fields.php b/src/Guid/Fields.php index 49db4ed2..d8a1a2b1 100644 --- a/src/Guid/Fields.php +++ b/src/Guid/Fields.php @@ -94,6 +94,7 @@ public function getBytes(): string public function getTimeLow(): Hexadecimal { // Swap the bytes from little endian to network byte order. + /** @var array $hex */ $hex = unpack( 'H*', pack( @@ -109,6 +110,7 @@ public function getTimeLow(): Hexadecimal public function getTimeMid(): Hexadecimal { // Swap the bytes from little endian to network byte order. + /** @var array $hex */ $hex = unpack( 'H*', pack( @@ -123,6 +125,7 @@ public function getTimeMid(): Hexadecimal public function getTimeHiAndVersion(): Hexadecimal { // Swap the bytes from little endian to network byte order. + /** @var array $hex */ $hex = unpack( 'H*', pack( @@ -172,6 +175,7 @@ public function getVersion(): ?int return null; } + /** @var array $parts */ $parts = unpack('n*', $this->bytes); return ((int) $parts[4] >> 4) & 0x00f; diff --git a/src/Provider/Dce/SystemDceSecurityProvider.php b/src/Provider/Dce/SystemDceSecurityProvider.php index 1a1f4cf2..67b58ebc 100644 --- a/src/Provider/Dce/SystemDceSecurityProvider.php +++ b/src/Provider/Dce/SystemDceSecurityProvider.php @@ -178,7 +178,7 @@ private function getWindowsUid(): string } /** @var string $sid */ - $sid = str_getcsv(trim($response))[1] ?? ''; + $sid = str_getcsv(trim((string) $response))[1] ?? ''; if (($lastHyphen = strrpos($sid, '-')) === false) { return ''; @@ -207,7 +207,7 @@ private function getWindowsGid(): string } /** @var string[] $userGroups */ - $userGroups = preg_split('/\s{2,}/', $response, -1, PREG_SPLIT_NO_EMPTY); + $userGroups = preg_split('/\s{2,}/', (string) $response, -1, PREG_SPLIT_NO_EMPTY); $firstGroup = trim($userGroups[1] ?? '', "* \t\n\r\0\x0B"); @@ -222,7 +222,7 @@ private function getWindowsGid(): string } /** @var string[] $userGroup */ - $userGroup = preg_split('/\s{2,}/', $response, -1, PREG_SPLIT_NO_EMPTY); + $userGroup = preg_split('/\s{2,}/', (string) $response, -1, PREG_SPLIT_NO_EMPTY); $sid = $userGroup[1] ?? ''; diff --git a/src/Rfc4122/Fields.php b/src/Rfc4122/Fields.php index 0989d842..2ccc20bb 100644 --- a/src/Rfc4122/Fields.php +++ b/src/Rfc4122/Fields.php @@ -177,6 +177,7 @@ public function getVersion(): ?int return null; } + /** @var array $parts */ $parts = unpack('n*', $this->bytes); return (int) $parts[4] >> 12; diff --git a/src/Rfc4122/VariantTrait.php b/src/Rfc4122/VariantTrait.php index c32a8ce8..4c981658 100644 --- a/src/Rfc4122/VariantTrait.php +++ b/src/Rfc4122/VariantTrait.php @@ -58,6 +58,7 @@ public function getVariant(): int throw new InvalidBytesException('Invalid number of bytes'); } + /** @var array $parts */ $parts = unpack('n*', $this->getBytes()); // $parts[5] is a 16-bit, unsigned integer containing the variant bits diff --git a/src/UuidFactory.php b/src/UuidFactory.php index feddef88..6f2cea06 100644 --- a/src/UuidFactory.php +++ b/src/UuidFactory.php @@ -471,10 +471,14 @@ private function uuidFromNsAndName($ns, string $name, int $version, string $hash */ private function uuidFromBytesAndVersion(string $bytes, int $version): UuidInterface { - $timeHi = (int) unpack('n*', substr($bytes, 6, 2))[1]; + /** @var array $unpackedTime */ + $unpackedTime = unpack('n*', substr($bytes, 6, 2)); + $timeHi = (int) $unpackedTime[1]; $timeHiAndVersion = pack('n*', BinaryUtils::applyVersion($timeHi, $version)); - $clockSeqHi = (int) unpack('n*', substr($bytes, 8, 2))[1]; + /** @var array $unpackedClockSeq */ + $unpackedClockSeq = unpack('n*', substr($bytes, 8, 2)); + $clockSeqHi = (int) $unpackedClockSeq[1]; $clockSeqHiAndReserved = pack('n*', BinaryUtils::applyVariant($clockSeqHi)); $bytes = substr_replace($bytes, $timeHiAndVersion, 6, 2); diff --git a/tests/Generator/DefaultTimeGeneratorTest.php b/tests/Generator/DefaultTimeGeneratorTest.php index acf98bda..f12a93d0 100644 --- a/tests/Generator/DefaultTimeGeneratorTest.php +++ b/tests/Generator/DefaultTimeGeneratorTest.php @@ -4,7 +4,6 @@ namespace Ramsey\Uuid\Test\Generator; -use AspectMock\Test as AspectMock; use Exception; use Mockery; use Mockery\MockInterface; @@ -21,6 +20,7 @@ use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Time; +use phpmock\mockery\PHPMockery; use function hex2bin; @@ -80,7 +80,6 @@ protected function tearDown(): void parent::tearDown(); unset($this->timeProvider, $this->nodeProvider, $this->timeConverter); Mockery::close(); - AspectMock::clean(); } public function testGenerateUsesNodeProviderWhenNodeIsNull(): void @@ -159,7 +158,10 @@ public function testGenerateDoesNotApplyVersionAndVariant(): void */ public function testGenerateUsesRandomSequenceWhenClockSeqNull(): void { - $randomInt = AspectMock::func('Ramsey\Uuid\Generator', 'random_int', 9622); + PHPMockery::mock('Ramsey\Uuid\Generator', 'random_int') + ->once() + ->with(0, 0x3fff) + ->andReturn(9622); $this->timeConverter->expects($this->once()) ->method('calculateTime') ->with($this->currentTime['sec'], $this->currentTime['usec']) @@ -170,7 +172,6 @@ public function testGenerateUsesRandomSequenceWhenClockSeqNull(): void $this->timeProvider ); $defaultTimeGenerator->generate($this->nodeId); - $randomInt->verifyInvokedOnce([0, 0x3fff]); } /** @@ -179,9 +180,9 @@ public function testGenerateUsesRandomSequenceWhenClockSeqNull(): void */ public function testGenerateThrowsExceptionWhenExceptionThrownByRandomint(): void { - AspectMock::func('Ramsey\Uuid\Generator', 'random_int', function (): void { - throw new Exception('Could not gather sufficient random data'); - }); + PHPMockery::mock('Ramsey\Uuid\Generator', 'random_int') + ->once() + ->andThrow(new Exception('Could not gather sufficient random data')); $defaultTimeGenerator = new DefaultTimeGenerator( $this->nodeProvider, diff --git a/tests/Generator/PeclUuidNameGeneratorTest.php b/tests/Generator/PeclUuidNameGeneratorTest.php index 65b9cb33..1c946bb8 100644 --- a/tests/Generator/PeclUuidNameGeneratorTest.php +++ b/tests/Generator/PeclUuidNameGeneratorTest.php @@ -29,10 +29,14 @@ public function testPeclUuidNameGeneratorHashesName(string $ns, string $name, st // Need to add the version and variant, since ext-uuid already includes // these in the values returned. - $timeHi = (int) unpack('n*', substr($expectedBytes, 6, 2))[1]; + /** @var array $unpackedTime */ + $unpackedTime = unpack('n*', substr($expectedBytes, 6, 2)); + $timeHi = (int) $unpackedTime[1]; $timeHiAndVersion = pack('n*', BinaryUtils::applyVersion($timeHi, $version)); - $clockSeqHi = (int) unpack('n*', substr($expectedBytes, 8, 2))[1]; + /** @var array $unpackedClockSeq */ + $unpackedClockSeq = unpack('n*', substr($expectedBytes, 8, 2)); + $clockSeqHi = (int) $unpackedClockSeq[1]; $clockSeqHiAndReserved = pack('n*', BinaryUtils::applyVariant($clockSeqHi)); $expectedBytes = substr_replace($expectedBytes, $timeHiAndVersion, 6, 2); diff --git a/tests/Generator/PeclUuidRandomGeneratorTest.php b/tests/Generator/PeclUuidRandomGeneratorTest.php index aaa5d2ea..acacf610 100644 --- a/tests/Generator/PeclUuidRandomGeneratorTest.php +++ b/tests/Generator/PeclUuidRandomGeneratorTest.php @@ -4,8 +4,8 @@ namespace Ramsey\Uuid\Test\Generator; -use AspectMock\Test as AspectMock; use Ramsey\Uuid\Generator\PeclUuidRandomGenerator; +use phpmock\mockery\PHPMockery; use const UUID_TYPE_RANDOM; @@ -22,30 +22,19 @@ class PeclUuidRandomGeneratorTest extends PeclUuidTestCase */ public function testGenerateCreatesUuidUsingPeclUuidMethods(): void { - $create = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->uuidString); - $parse = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); + PHPMockery::mock('Ramsey\Uuid\Generator', 'uuid_create') + ->once() + ->with(UUID_TYPE_RANDOM) + ->andReturn($this->uuidString); - $generator = new PeclUuidRandomGenerator(); - $uuid = $generator->generate($this->length); - - $this->assertSame($this->uuidBinary, $uuid); - $create->verifyInvoked([UUID_TYPE_RANDOM]); - $parse->verifyInvoked([$this->uuidString]); - } + PHPMockery::mock('Ramsey\Uuid\Generator', 'uuid_parse') + ->once() + ->with($this->uuidString) + ->andReturn($this->uuidBinary); - /** - * This test is for the return type of the generate method - * It ensures that the generate method returns whatever value uuid_parse returns. - */ - public function testGenerateReturnsUuidString(): void - { - $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->assertSame($this->uuidBinary, $uuid); - $create->verifyInvoked([UUID_TYPE_RANDOM]); - $parse->verifyInvoked([$this->uuidString]); } } diff --git a/tests/Generator/PeclUuidTimeGeneratorTest.php b/tests/Generator/PeclUuidTimeGeneratorTest.php index b404e5f4..e4c0e201 100644 --- a/tests/Generator/PeclUuidTimeGeneratorTest.php +++ b/tests/Generator/PeclUuidTimeGeneratorTest.php @@ -4,8 +4,8 @@ namespace Ramsey\Uuid\Test\Generator; -use AspectMock\Test as AspectMock; use Ramsey\Uuid\Generator\PeclUuidTimeGenerator; +use phpmock\mockery\PHPMockery; use const UUID_TYPE_TIME; @@ -17,30 +17,19 @@ class PeclUuidTimeGeneratorTest extends PeclUuidTestCase */ public function testGenerateCreatesUuidUsingPeclUuidMethods(): void { - $create = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->uuidString); - $parse = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); + PHPMockery::mock('Ramsey\Uuid\Generator', 'uuid_create') + ->once() + ->with(UUID_TYPE_TIME) + ->andReturn($this->uuidString); - $generator = new PeclUuidTimeGenerator(); - $uuid = $generator->generate(); - - $this->assertSame($this->uuidBinary, $uuid); - $create->verifyInvoked([UUID_TYPE_TIME]); - $parse->verifyInvoked([$this->uuidString]); - } + PHPMockery::mock('Ramsey\Uuid\Generator', 'uuid_parse') + ->once() + ->with($this->uuidString) + ->andReturn($this->uuidBinary); - /** - * This test is for the return type of the generate method - * It ensures that the generate method returns whatever value uuid_parse returns. - */ - public function testGenerateReturnsUuidString(): void - { - $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->assertSame($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 dca6618a..1b09b076 100644 --- a/tests/Generator/RandomBytesGeneratorTest.php +++ b/tests/Generator/RandomBytesGeneratorTest.php @@ -4,11 +4,11 @@ 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; +use phpmock\mockery\PHPMockery; use function hex2bin; @@ -33,28 +33,17 @@ public function lengthAndHexDataProvider(): array * @runInSeparateProcess * @preserveGlobalState disabled */ - public function testGenerateUsesOpenSsl(int $length, string $hex): void + public function testGenerateReturnsRandomBytes(int $length, string $hex): void { $bytes = hex2bin($hex); - $openSsl = AspectMock::func('Ramsey\Uuid\Generator', 'random_bytes', $bytes); - $generator = new RandomBytesGenerator(); - $this->assertSame($bytes, $generator->generate($length)); - $openSsl->verifyInvokedOnce([$length]); - } + PHPMockery::mock('Ramsey\Uuid\Generator', 'random_bytes') + ->once() + ->with($length) + ->andReturn($bytes); - /** - * @throws Exception - * - * @dataProvider lengthAndHexDataProvider - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testGenerateReturnsRandomBytes(int $length, string $hex): void - { - $bytes = hex2bin($hex); - AspectMock::func('Ramsey\Uuid\Generator', 'random_bytes', $bytes); $generator = new RandomBytesGenerator(); + $this->assertSame($bytes, $generator->generate($length)); } @@ -64,9 +53,10 @@ public function testGenerateReturnsRandomBytes(int $length, string $hex): void */ public function testGenerateThrowsExceptionWhenExceptionThrownByRandombytes(): void { - AspectMock::func('Ramsey\Uuid\Generator', 'random_bytes', function (): void { - throw new Exception('Could not gather sufficient random data'); - }); + PHPMockery::mock('Ramsey\Uuid\Generator', 'random_bytes') + ->once() + ->with(16) + ->andThrow(new Exception('Could not gather sufficient random data')); $generator = new RandomBytesGenerator(); diff --git a/tests/Provider/Node/RandomNodeProviderTest.php b/tests/Provider/Node/RandomNodeProviderTest.php index bf818153..2af39228 100644 --- a/tests/Provider/Node/RandomNodeProviderTest.php +++ b/tests/Provider/Node/RandomNodeProviderTest.php @@ -4,11 +4,11 @@ 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; +use phpmock\mockery\PHPMockery; use function bin2hex; use function hex2bin; @@ -18,12 +18,6 @@ class RandomNodeProviderTest extends TestCase { - protected function tearDown(): void - { - parent::tearDown(); - AspectMock::clean(); - } - /** * @runInSeparateProcess * @preserveGlobalState disabled @@ -33,30 +27,15 @@ public function testGetNodeUsesRandomBytes(): void $bytes = hex2bin('38a675685d50'); $expectedNode = '39a675685d50'; - $randomBytes = AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); + PHPMockery::mock('Ramsey\Uuid\Provider\Node', 'random_bytes') + ->once() + ->with(6) + ->andReturn($bytes); + $provider = new RandomNodeProvider(); $node = $provider->getNode(); $this->assertSame($expectedNode, $node->toString()); - $randomBytes->verifyInvoked([6]); - } - - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testGetNodeSetsMulticastBit(): void - { - $bytes = hex2bin('38a675685d50'); - - // Expected node has the multicast bit set, and it wasn't set in the bytes. - $expectedNode = '39a675685d50'; - - $randomBytes = AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); - $provider = new RandomNodeProvider(); - - $this->assertSame($expectedNode, $provider->getNode()->toString()); - $randomBytes->verifyInvoked([6]); } /** @@ -71,11 +50,14 @@ public function testGetNodeAlreadyHasMulticastBit(): void // We expect the same hex value for the node. $expectedNode = $bytesHex; - $randomBytes = AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); + PHPMockery::mock('Ramsey\Uuid\Provider\Node', 'random_bytes') + ->once() + ->with(6) + ->andReturn($bytes); + $provider = new RandomNodeProvider(); $this->assertSame($expectedNode, $provider->getNode()->toString()); - $randomBytes->verifyInvoked([6]); } /** @@ -87,11 +69,14 @@ public function testGetNodeSetsMulticastBitForLowNodeValue(): void $bytes = hex2bin('100000000001'); $expectedNode = '110000000001'; - $randomBytes = AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes); + PHPMockery::mock('Ramsey\Uuid\Provider\Node', 'random_bytes') + ->once() + ->with(6) + ->andReturn($bytes); + $provider = new RandomNodeProvider(); $this->assertSame($expectedNode, $provider->getNode()->toString()); - $randomBytes->verifyInvoked([6]); } public function testGetNodeAlwaysSetsMulticastBit(): void @@ -125,9 +110,9 @@ public function testGetNodeAlwaysSetsMulticastBit(): void */ public function testGetNodeThrowsExceptionWhenExceptionThrownByRandombytes(): void { - AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', function (): void { - throw new Exception('Could not gather sufficient random data'); - }); + PHPMockery::mock('Ramsey\Uuid\Provider\Node', 'random_bytes') + ->once() + ->andThrow(new Exception('Could not gather sufficient random data')); $provider = new RandomNodeProvider(); diff --git a/tests/Provider/Node/SystemNodeProviderTest.php b/tests/Provider/Node/SystemNodeProviderTest.php index 4e98e786..8bf85c2d 100644 --- a/tests/Provider/Node/SystemNodeProviderTest.php +++ b/tests/Provider/Node/SystemNodeProviderTest.php @@ -4,12 +4,11 @@ namespace Ramsey\Uuid\Test\Provider\Node; -use AspectMock\Proxy\FuncProxy; -use AspectMock\Test as AspectMock; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Exception\NodeException; use Ramsey\Uuid\Provider\Node\SystemNodeProvider; use Ramsey\Uuid\Test\TestCase; +use phpmock\spy\Spy; use function array_shift; use function array_walk; @@ -18,13 +17,15 @@ use function strlen; use function vsprintf; +use const GLOB_NOSORT; + /** * Tests for the SystemNodeProvider class * * The class under test make use of various native functions who's output is * dictated by which environment PHP runs on. Instead of having to run these - * tests on each of these environments, the related functions are mocked (using - * AspectMock). The following functions are concerned: + * tests on each of these environments, the related functions are mocked. The + * following functions are concerned: * * - glob * - constant @@ -55,7 +56,7 @@ class SystemNodeProviderTest extends TestCase private const PROVIDER_NAMESPACE = 'Ramsey\\Uuid\\Provider\\Node'; /** - * @var FuncProxy[] + * @var Spy[] */ private $functionProxies = []; @@ -318,7 +319,7 @@ public function testGetNodeGetsNetworkInterfaceConfig(string $os, string $comman $fileGetContentsAssert = null; $isReadableAssert = null; if ($os === 'Linux') { - $globBodyAssert = ['/sys/class/net/*/address']; + $globBodyAssert = [['/sys/class/net/*/address', GLOB_NOSORT]]; $fileGetContentsAssert = ['mock address path']; $isReadableAssert = $fileGetContentsAssert; } @@ -427,7 +428,7 @@ function (): void { if ($os === 'Linux') { $fileGetContentsAssert = [['mock address path 1'], ['mock address path 2']]; - $globBodyAssert = ['/sys/class/net/*/address']; + $globBodyAssert = [['/sys/class/net/*/address', GLOB_NOSORT]]; $passthruBodyAssert = null; $constantBodyAssert = ['PHP_OS']; $iniGetDisableFunctionsAssert = null; @@ -469,7 +470,7 @@ function (): void { /* Assert */ $this->assertMockFunctions( null, - ['/sys/class/net/*/address'], + [['/sys/class/net/*/address', GLOB_NOSORT]], ['netstat -ie 2>&1'], ['PHP_OS'], ['disable_functions'] @@ -502,7 +503,7 @@ function (): void { /* Assert */ $this->assertMockFunctions( null, - ['/sys/class/net/*/address'], + [['/sys/class/net/*/address', GLOB_NOSORT]], ['netstat -ie 2>&1'], ['PHP_OS'], ['disable_functions'] @@ -536,7 +537,7 @@ function (): void { /* Assert */ $this->assertMockFunctions( null, - ['/sys/class/net/*/address'], + [['/sys/class/net/*/address', GLOB_NOSORT]], ['netstat -ie 2>&1'], ['PHP_OS'], ['disable_functions'], @@ -611,7 +612,16 @@ private function arrangeMockFunctions( ]; array_walk($mockFunction, function ($body, $key): void { - $this->functionProxies[$key] = AspectMock::func(self::PROVIDER_NAMESPACE, $key, $body); + if (!is_callable($body)) { + $body = function () use ($body) { + return $body; + }; + } + + $spy = new Spy(self::PROVIDER_NAMESPACE, $key, $body); + $spy->enable(); + + $this->functionProxies[$key] = $spy; }); } @@ -621,7 +631,7 @@ private function arrangeMockFunctions( * Provide a NULL to assert a function is never called. * * @param array|array>|null $fileGetContentsAssert - * @param array|array>|null $globBodyAssert + * @param array>|null $globBodyAssert * @param array|array>|null $passthruBodyAssert * @param array|array>|null $constantBodyAssert * @param array|array>|null $iniGetDisableFunctionsAssert @@ -646,13 +656,21 @@ private function assertMockFunctions( array_walk($mockFunctionAsserts, function ($asserts, $key): void { if ($asserts === null) { - $this->functionProxies[$key]->verifyNeverInvoked(); + // Assert the function was never invoked. + $this->assertEmpty($this->functionProxies[$key]->getInvocations()); } elseif (is_array($asserts)) { + // Assert there was at least one invocation for this function. + $this->assertNotEmpty($this->functionProxies[$key]->getInvocations()); + + $invokedArgs = []; + foreach ($this->functionProxies[$key]->getInvocations() as $invocation) { + $invokedArgs[] = $invocation->getArguments(); + } + foreach ($asserts as $assert) { - if (!is_array($assert)) { - $assert = [$assert]; - } - $this->functionProxies[$key]->verifyInvoked($assert); + // Assert these args were used to invoke the function. + $assert = is_array($assert) ? $assert : [$assert]; + $this->assertContains($assert, $invokedArgs); } } else { $error = vsprintf( diff --git a/tests/TestCase.php b/tests/TestCase.php index 84228fe2..ed22f93a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,7 +4,6 @@ namespace Ramsey\Uuid\Test; -use AspectMock\Test as AspectMock; use Mockery; use PHPUnit\Framework\TestCase as PhpUnitTestCase; @@ -17,12 +16,14 @@ class TestCase extends PhpUnitTestCase protected function tearDown(): void { parent::tearDown(); - AspectMock::clean(); Mockery::close(); } public static function isLittleEndianSystem(): bool { - return current(unpack('v', pack('S', 0x00FF))) === 0x00FF; + /** @var array $unpacked */ + $unpacked = unpack('v', pack('S', 0x00FF)); + + return current($unpacked) === 0x00FF; } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 40eef3fa..c0393bc8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,22 +11,5 @@ // Ensure floating-point precision is set to 14 (the default) for tests. ini_set('precision', '14'); -use AspectMock\Kernel; - require_once __DIR__ . '/../vendor/autoload.php'; // composer autoload require_once __DIR__ . '/phpstan-bootstrap.php'; - -$cacheDir = __DIR__ . '/../build/cache/goaop'; -if (!is_dir($cacheDir)) { - if (mkdir($cacheDir, 0775, true) === false) { - echo "\n[ERROR] Unable to create cache directory at {$cacheDir}\n\n"; - exit(1); - } -} - -$kernel = Kernel::getInstance(); -$kernel->init([ - 'debug' => true, - 'cacheDir' => $cacheDir, - 'includePaths' => [__DIR__ . '/../src'] -]);