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
5 changes: 2 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ jobs:
strategy:
matrix:
php:
- "8.2"
- "8.3"
- "8.4"
# dependencies:
# - "lowest"
# - "highest"
include:
- php-version: "8.3"
- php-version: "8.4"
composer-options: "--ignore-platform-reqs"
steps:
- name: Checkout
Expand Down
5 changes: 0 additions & 5 deletions .run/PHPUnit.run.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="PHPUnit" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
<CommandLine>
<PhpTestInterpreterSettings>
<option name="interpreterName" value="PHP 8.3" />
</PhpTestInterpreterSettings>
</CommandLine>
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml" coverage_engine="PCov" scope="XML" use_alternative_configuration_file="true" />
<method v="2" />
</configuration>
Expand Down
2 changes: 1 addition & 1 deletion benchmarking/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,4 @@
fclose($fp);
}

echo 'Total time: ' . round($totalTime) . 'ms' . PHP_EOL;
echo 'Total time: ' . round($totalTime) . 'ms' . PHP_EOL;
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"description": "Multi-probe consistent hashing implementation for PHP",
"type": "library",
"require-dev": {
"phpunit/phpunit": "^11.2",
"phpstan/phpstan": "^1.11"
"phpunit/phpunit": "^11.5",
"phpstan/phpstan": "^2.1"
},
"license": "GPL-3.0-only",
"authors": [
Expand All @@ -14,7 +14,8 @@
],
"autoload": {
"psr-4": {
"Jspeedz\\PhpConsistentHashing\\": "src/"
"Jspeedz\\PhpConsistentHashing\\": "src/",
"Jspeedz\\PhpConsistentHashing\\Tests\\": "tests/"
}
},
"scripts": {
Expand All @@ -30,7 +31,7 @@
"phpstanpro": "Runs PHPStan in PRO mode!"
},
"require": {
"php": ">=8.2"
"php": ">=8.4"
},
"config": {
"process-timeout": 0
Expand Down
16 changes: 8 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
parameters:
level: 9
paths:
- src
- tests
ignoreErrors:
-
message: '#Variable \$oneHundredAndTwentyNodes in isset\(\) always exists and is always null#'
path: tests/MultiProbeConsistentHashTest.php
level: 9
paths:
- src
- tests
ignoreErrors:
-
message: '#Variable \$oneHundredAndTwentyNodes in isset\(\) always exists and is always null#'
path: tests/MultiProbeConsistentHashTest.php
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<coverage includeUncoveredFiles="true"
pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="true">
disableCodeCoverageIgnore="false">
<report>
<cobertura outputFile="cobertura.xml"/>
<clover outputFile="clover.xml"/>
Expand Down
10 changes: 8 additions & 2 deletions src/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ public function fetchKeys(): array {
$keys,
);
shuffle($keys);

return array_map(function(mixed $key): string {
if(is_scalar($key) || (is_object($key) && method_exists($key, '__toString'))) {
return (string) $key;
}

return $keys;
throw new Exception('Key cannot be cast to string');
}, $keys);
}

/**
Expand Down Expand Up @@ -199,4 +205,4 @@ public function printResults(array $results): string {

return $string;
}
}
}
2 changes: 1 addition & 1 deletion src/HashFunctions/Accurate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ function(string $key): float|int {
},
];
}
}
}
2 changes: 1 addition & 1 deletion src/HashFunctions/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ function(string $key): int|float {
},
];
}
}
}
2 changes: 1 addition & 1 deletion src/MultiProbeConsistentHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ public function getNode(string $key): ?string {

return $targetNode;
}
}
}
27 changes: 18 additions & 9 deletions tests/BenchmarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,39 @@

use Jspeedz\PhpConsistentHashing\Benchmark;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

#[CoversClass(Benchmark::class)]
class BenchmarkTest extends TestCase {
public function testGetAvailableHashCallbacks(): void {
#[Test]
public function getAvailableHashCallbacks(): void {
$benchmark = new Benchmark();
$results = $benchmark->getAvailableHashCallbacks();

$this->assertIsArray($results);
$this->assertNotEmpty($results);
foreach($results as $result) {
// Ignore this one, as there is no guarantee that the element is actually callable.
// @phpstan-ignore method.alreadyNarrowedType
$this->assertIsCallable($result);
}

$results = $benchmark->getAvailableHashCallbacks([
'someHashAlgorithm',
'md5',
]);

$this->assertIsArray($results);
$this->assertCount(1, $results);
foreach($results as $result) {
// Ignore this one, as there is no guarantee that the element is actually callable.
// @phpstan-ignore method.alreadyNarrowedType
$this->assertIsCallable($result);
$this->assertSame(3895525021, $result('someValue'));
}
}

public function testGetCombinations(): void {
#[Test]
public function getCombinations(): void {
$benchmark = new Benchmark();

// Test case 1: Normal case
Expand Down Expand Up @@ -74,7 +80,8 @@ public function testGetCombinations(): void {
$this->assertEquals($expected, $benchmark->getCombinations($array, $length));
}

public function testSortByTwoColumns(): void {
#[Test]
public function sortByTwoColumns(): void {
$benchmark = new Benchmark();

// Test case 1: Normal case with ascending sort
Expand Down Expand Up @@ -132,7 +139,8 @@ public function testSortByTwoColumns(): void {
$this->assertEquals($expected, $array);
}

public function testFormatNumber(): void {
#[Test]
public function formatNumber(): void {
$class = new ReflectionClass(Benchmark::class);
$method = $class->getMethod('formatNumber');
$method->setAccessible(true);
Expand Down Expand Up @@ -230,7 +238,8 @@ public function testFormatNumber(): void {
);
}

public function testPrintResults(): void {
#[Test]
public function printResults(): void {
$mock = $this->getMockBuilder(Benchmark::class)
->onlyMethods(['formatNumber'])
->getMock();
Expand Down Expand Up @@ -294,4 +303,4 @@ function(int|float $number): string {

$this->assertEquals($expected, $mock->printResults($results));
}
}
}
12 changes: 4 additions & 8 deletions tests/HashFunctions/AccurateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@

use Jspeedz\PhpConsistentHashing\HashFunctions\Accurate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(Accurate::class)]
class AccurateTest extends TestCase {
public function testStandardCallbacks(): void {
#[Test]
public function standardCallbacks(): void {
$callbacks = (new Accurate())();

$this->assertCount(5, $callbacks);

$this->assertIsCallable($callbacks[0]);
$this->assertIsCallable($callbacks[1]);
$this->assertIsCallable($callbacks[2]);
$this->assertIsCallable($callbacks[3]);
$this->assertIsCallable($callbacks[4]);

$this->assertSame(
crc32('test'),
$callbacks[0]('test'),
Expand All @@ -40,4 +36,4 @@ public function testStandardCallbacks(): void {
$callbacks[4]('test'),
);
}
}
}
10 changes: 4 additions & 6 deletions tests/HashFunctions/StandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

use Jspeedz\PhpConsistentHashing\HashFunctions\Standard;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(Standard::class)]
class StandardTest extends TestCase {
public function testStandardCallbacks(): void {
#[Test]
public function standardCallbacks(): void {
$callbacks = (new Standard())();

$this->assertCount(3, $callbacks);

$this->assertIsCallable($callbacks[0]);
$this->assertIsCallable($callbacks[1]);
$this->assertIsCallable($callbacks[2]);

$this->assertSame(
crc32('test'),
$callbacks[0]('test'),
Expand All @@ -30,4 +28,4 @@ public function testStandardCallbacks(): void {
$callbacks[2]('test'),
);
}
}
}
Loading