Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['8.2', '8.3', '8.4', '8.5']
php-version: ['8.3', '8.4', '8.5']
os: [ubuntu-latest, windows-latest]

steps:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"php-parallel-lint/php-parallel-lint": "^1.4",
"phpbench/phpbench": "^1.4",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^10.5",
"phpunit/phpunit": "^12.5.22",
"ramsey/conventional-commits": "^1.5",
"slevomat/coding-standard": "^8.25",
"squizlabs/php_codesniffer": "^4.0"
Expand Down
2 changes: 2 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ private static function convertLargeBaseToBase36(array $digits, int $base): stri
*/
public static function hexToBase36(string $hexValue): string
{
$hexValue = preg_replace('/[^0-9a-fA-F]/', '', $hexValue) ?? '';

if ($hexValue === '' || $hexValue === '0') {
return '0';
}
Expand Down
21 changes: 7 additions & 14 deletions tests/Cuid2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use OutOfRangeException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Visus\Cuid2\Cuid2;

Expand Down Expand Up @@ -119,9 +120,8 @@ public function testStringRepresentationsAreConsistent(): void

/**
* @throws Exception
*
* @dataProvider invalidLengthProvider
*/
#[DataProvider('invalidLengthProvider')]
public function testThrowsExceptionForInvalidLength(int $length): void
{
$this->expectException(OutOfRangeException::class);
Expand Down Expand Up @@ -200,9 +200,8 @@ public function testGeneratesUniqueValuesInLargeSample(): void

/**
* @throws Exception
*
* @dataProvider validLengthProvider
*/
#[DataProvider('validLengthProvider')]
public function testGeneratesCorrectLength(int $length): void
{
$cuid = new Cuid2($length);
Expand All @@ -226,9 +225,8 @@ public function testStaticGenerateCreatesDefaultLength(): void

/**
* @throws Exception
*
* @dataProvider validLengthProvider
*/
#[DataProvider('validLengthProvider')]
public function testStaticGenerateCreatesCustomLength(int $length): void
{
$cuid = Cuid2::generate($length);
Expand All @@ -240,9 +238,8 @@ public function testStaticGenerateCreatesCustomLength(int $length): void

/**
* @throws Exception
*
* @dataProvider invalidLengthProvider
*/
#[DataProvider('invalidLengthProvider')]
public function testStaticGenerateThrowsExceptionForInvalidLength(int $length): void
{
$this->expectException(OutOfRangeException::class);
Expand All @@ -262,17 +259,13 @@ public function testStaticGenerateProducesUniqueValues(): void
$this->assertNotEquals((string) $cuid1, (string) $cuid2);
}

/**
* @dataProvider validCuidProvider
*/
#[DataProvider('validCuidProvider')]
public function testIsValidAcceptsValidCuids(string $cuid, ?int $expectedLength = null): void
{
$this->assertTrue(Cuid2::isValid($cuid, $expectedLength));
}

/**
* @dataProvider invalidCuidProvider
*/
#[DataProvider('invalidCuidProvider')]
public function testIsValidRejectsInvalidCuids(string $cuid, ?int $expectedLength = null): void
{
$this->assertFalse(Cuid2::isValid($cuid, $expectedLength));
Expand Down
9 changes: 3 additions & 6 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Visus\Cuid2\Test;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Visus\Cuid2\Utils;

Expand Down Expand Up @@ -56,9 +57,7 @@ public static function invalidHexCharactersProvider(): array
];
}

/**
* @dataProvider hexToBase36Provider
*/
#[DataProvider('hexToBase36Provider')]
public function testHexToBase36Conversion(string $hex, string $expected): void
{
$result = Utils::hexToBase36($hex);
Expand Down Expand Up @@ -126,9 +125,7 @@ public function testHexToBase36ProducesUniqueOutputsForDifferentInputs(): void
);
}

/**
* @dataProvider invalidHexCharactersProvider
*/
#[DataProvider('invalidHexCharactersProvider')]
public function testHexToBase36HandlesInvalidCharactersGracefully(string $hex, string $_expected): void
{
// Invalid characters are treated as 0 based on the match default case
Expand Down
Loading