Skip to content

Commit

Permalink
Use PHPUnit 10.5.40 to match Composer
Browse files Browse the repository at this point in the history
  • Loading branch information
onethumb committed Dec 27, 2024
1 parent 07e4daa commit aa430ad
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.3.12'
tools: cs2pr:1.8.5, composer:2.8.4, phpunit:11.5.2
tools: cs2pr:1.8.5, composer:2.8.4, phpunit:10.5.40

- name: Output PHP info
run: php -i
Expand Down
53 changes: 37 additions & 16 deletions src/Ffi.php → src/Crc64/Ffi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Awesomized\Checksums\Crc64;
namespace Awesomized\Checksums;

use FFI\Exception;
use FFI\ParserException;
Expand All @@ -16,7 +16,10 @@ final class Ffi
{
private const string SCOPE_DEFAULT = 'CRC64NVME';

private static ?\FFI $ffi = null;
/**
* @var array<string, \FFI>
*/
private static array $ffis = [];

/**
* Creates a new FFI instance from the given C declarations and library name.
Expand All @@ -31,20 +34,26 @@ public static function fromCode(
string $code,
?string $library = null,
): \FFI {
if (null !== self::$ffi) {
return self::$ffi;
$id = $code . $library;

if (isset(self::$ffis[$id])) {
return self::$ffis[$id];
}

$ffi = \FFI::cdef(
code: $code,
lib: $library,
);

/** @psalm-suppress UndefinedMethod - FFI method, can't tell if it's defined or not */
/**
* Verify that the FFI instance is valid by calling digest_new() method before caching.
*
* @psalm-suppress UndefinedMethod - FFI method, can't tell if it's defined or not
*/
// @phpstan-ignore-next-line
$ffi->digest_new();

return self::$ffi = $ffi;
return self::$ffis[$id] = $ffi;
}

/**
Expand All @@ -58,19 +67,25 @@ public static function fromCode(
public static function fromPreloadScope(
string $ffiScopeName = self::SCOPE_DEFAULT,
): \FFI {
if (null !== self::$ffi) {
return self::$ffi;
$id = $ffiScopeName;

if (isset(self::$ffis[$id])) {
return self::$ffis[$id];
}

$ffi = \FFI::scope(
name: $ffiScopeName,
);

/** @psalm-suppress UndefinedMethod - FFI method, can't tell if it's defined or not */
/**
* Verify that the FFI instance is valid by calling digest_new() method before caching.
*
* @psalm-suppress UndefinedMethod - FFI method, can't tell if it's defined or not
*/
// @phpstan-ignore-next-line
$ffi->digest_new();

return self::$ffi = $ffi;
return self::$ffis[$id] = $ffi;
}

/**
Expand All @@ -83,14 +98,16 @@ public static function fromPreloadScope(
public static function fromHeaderFile(
string $headerFile = '',
): \FFI {
if (null !== self::$ffi) {
return self::$ffi;
}

if ('' === $headerFile) {
$headerFile = self::whichHeaderFile();
}

$id = $headerFile;

if (isset(self::$ffis[$id])) {
return self::$ffis[$id];
}

$ffi = \FFI::load(
filename: $headerFile,
);
Expand All @@ -101,11 +118,15 @@ public static function fromHeaderFile(
);
}

/** @psalm-suppress UndefinedMethod - FFI method, can't tell if it's defined or not */
/**
* Verify that the FFI instance is valid by calling digest_new() method before caching.
*
* @psalm-suppress UndefinedMethod - FFI method, can't tell if it's defined or not
*/
// @phpstan-ignore-next-line
$ffi->digest_new();

return self::$ffi = $ffi;
return self::$ffis[$id] = $ffi;
}

/**
Expand Down
File renamed without changes.
12 changes: 10 additions & 2 deletions tests/unit/FfiTest.php → tests/unit/Crc64/FfiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

declare(strict_types=1);

namespace Awesomized\Checksums\Crc64\tests\unit;
namespace Awesomized\Checksums\tests\unit;

use Awesomized\Checksums\Crc64;
use Awesomized\Checksums\tests\unit\Crc64\NvmeTest;
use FFI\Exception;
use PHPUnit\Framework\TestCase;

Expand All @@ -20,7 +21,7 @@ public function testFfiFromCodeInvalidCodeShouldFail(): void
{
$this->expectException(Exception::class);

$ffi = Crc64\Ffi::fromCode(
$ffi = Ffi::fromCode(
code: '',
library: __DIR__ . '/../../build/target/release/' . Crc64\Ffi::whichLibrary(),
);
Expand Down Expand Up @@ -95,6 +96,8 @@ public function testFfiFromCodeValidInputShouldSucceed(): void
}

/**
* @depends testFfiFromCodeValidInputShouldSucceed
*
* @throws Exception
* @throws \InvalidArgumentException
*/
Expand All @@ -106,6 +109,8 @@ public function testFfiFromHeaderValidHeaderShouldSucceed(): void
}

/**
* @depends testFfiFromHeaderValidHeaderShouldSucceed
*
* @throws Exception
*/
public function testFfiFromPreloadScopeValidScopeShouldSucceed(): void
Expand All @@ -119,6 +124,9 @@ public function testFfiFromPreloadScopeValidScopeShouldSucceed(): void
$this->testFfiCalculateCrc64ShouldSucceed($ffi);
}

/**
* @depends testFfiFromCodeValidInputShouldSucceed
*/
private function testFfiCalculateCrc64ShouldSucceed(
\FFI $ffi,
): void {
Expand Down
42 changes: 41 additions & 1 deletion tests/unit/NvmeTest.php → tests/unit/Crc64/NvmeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Awesomized\Checksums\Crc64\tests\unit;
namespace Awesomized\Checksums\tests\unit;

use Awesomized\Checksums\Crc64;
use FFI;
Expand Down Expand Up @@ -42,10 +42,25 @@ public function testConstructorInvalidLibraryShouldFail(): void
new Crc64\Nvme(
crc64Nvme: $ffi,
);
}

/**
* @depends testConstructorInvalidLibraryShouldFail
*
* @throws \InvalidArgumentException
*/
public function testConstructorValidLibraryShouldSucceed(): void
{
$this->expectNotToPerformAssertions();

$crc64Nvme = new Crc64\Nvme(
crc64Nvme: $this->ffi,
);
}

/**
* @depends testConstructorValidLibraryShouldSucceed
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
Expand All @@ -63,6 +78,8 @@ public function testCalculateHelloWorldShouldSucceed(): void
}

/**
* @depends testConstructorValidLibraryShouldSucceed
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
Expand All @@ -83,6 +100,8 @@ public function testCalculateFileHelloWorldShouldSucceed(): void
* Ensure that binary data is calculated properly, especially null bytes (0x00), which has been problematic in the
* past.
*
* @depends testConstructorValidLibraryShouldSucceed
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @throws RandomException
Expand All @@ -96,4 +115,25 @@ public function testCalculateBinaryDataShouldSucceed(): void

self::assertNotSame('0000000000000000', $crc64);
}

/**
* @depends testConstructorValidLibraryShouldSucceed
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function testCalculateChunkedDataShouldSucceed(): void
{
$crc64Nvme = new Crc64\Nvme(
crc64Nvme: $this->ffi,
);

$crc64Nvme->write('hello, ');
$crc64Nvme->write('world!');

self::assertSame(
self::HELLO_WORLD_CRC64,
$crc64Nvme->sum(),
);
}
}

0 comments on commit aa430ad

Please sign in to comment.