Skip to content

Commit

Permalink
Prepare test suite for PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Oct 29, 2020
1 parent 2f92bdf commit b941aa1
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 153 deletions.
10 changes: 5 additions & 5 deletions composer.json
Expand Up @@ -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",
Expand All @@ -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.",
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/DceSecurityGenerator.php
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/Guid/Fields.php
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -172,6 +175,7 @@ public function getVersion(): ?int
return null;
}

/** @var array $parts */
$parts = unpack('n*', $this->bytes);

return ((int) $parts[4] >> 4) & 0x00f;
Expand Down
6 changes: 3 additions & 3 deletions src/Provider/Dce/SystemDceSecurityProvider.php
Expand Up @@ -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 '';
Expand Down Expand Up @@ -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");

Expand All @@ -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] ?? '';

Expand Down
1 change: 1 addition & 0 deletions src/Rfc4122/Fields.php
Expand Up @@ -177,6 +177,7 @@ public function getVersion(): ?int
return null;
}

/** @var array $parts */
$parts = unpack('n*', $this->bytes);

return (int) $parts[4] >> 12;
Expand Down
1 change: 1 addition & 0 deletions src/Rfc4122/VariantTrait.php
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/UuidFactory.php
Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions tests/Generator/DefaultTimeGeneratorTest.php
Expand Up @@ -4,7 +4,6 @@

namespace Ramsey\Uuid\Test\Generator;

use AspectMock\Test as AspectMock;
use Exception;
use Mockery;
use Mockery\MockInterface;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'])
Expand All @@ -170,7 +172,6 @@ public function testGenerateUsesRandomSequenceWhenClockSeqNull(): void
$this->timeProvider
);
$defaultTimeGenerator->generate($this->nodeId);
$randomInt->verifyInvokedOnce([0, 0x3fff]);
}

/**
Expand All @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions tests/Generator/PeclUuidNameGeneratorTest.php
Expand Up @@ -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);
Expand Down
29 changes: 9 additions & 20 deletions tests/Generator/PeclUuidRandomGeneratorTest.php
Expand Up @@ -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;

Expand All @@ -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]);
}
}
29 changes: 9 additions & 20 deletions tests/Generator/PeclUuidTimeGeneratorTest.php
Expand Up @@ -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;

Expand All @@ -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]);
}
}
32 changes: 11 additions & 21 deletions tests/Generator/RandomBytesGeneratorTest.php
Expand Up @@ -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;

Expand All @@ -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));
}

Expand All @@ -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();

Expand Down

0 comments on commit b941aa1

Please sign in to comment.