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 VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.40
0.0.41
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "celarius/spin-framework",
"description": "A super lightweight PHP UI/REST Framework",
"version": "0.0.40",
"version": "0.0.41",
"keywords": [
"php8",
"php-framework",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "spin-framework",
"title": "Spin Framework",
"version": "0.0.40",
"version": "0.0.41",
"homepage": "https://github.com/Celarius/spin-framework",
"description": "A super lightweight PHP UI/REST Framework",
"author": {
Expand Down
24 changes: 18 additions & 6 deletions src/Cache/Adapters/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,25 @@ protected function disconnect(): bool
public function get($key, mixed $default = null): mixed
{
$result = $this->redisClient->get($key);
if ($result) {
if (\is_int($result)) {
return $result;
}
return unserialize($result);

# Missing key — Predis returns null for a non-existent key
if ($result === null) {
return $default;
}

# Raw integer — stored un-serialized by set() and by native inc()/dec()
$asInt = \filter_var($result, \FILTER_VALIDATE_INT);
if ($asInt !== false) {
return $asInt;
}
return $default;

# Everything else is serialized; serialize(false) is a valid stored value
$value = @\unserialize($result);
if ($value === false && $result !== \serialize(false)) {
return $default;
}

return $value;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/Cache/Adapters/ApcuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spin\tests\Core;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\SimpleCache\InvalidArgumentException;
use Spin\Cache\Adapters\Apcu;
use Spin\Exceptions\CacheException;
Expand Down Expand Up @@ -374,6 +375,41 @@ public function testComplexDataTypes(): void
$this->assertFalse($this->cacheObj->get('bool_false'));
}

/**
* Regression for #78 — every supported value type must survive a
* set()/get() round-trip with its type and value intact.
*/
#[DataProvider('roundTripValueProvider')]
public function testValueRoundTrip(string $label, mixed $value): void
{
$key = 'roundtrip_' . $label;

$this->assertTrue($this->cacheObj->set($key, $value));
$this->assertSame($value, $this->cacheObj->get($key), "Round-trip failed for: {$label}");
}

/**
* @return array<string,array{0:string,1:mixed}>
*/
public static function roundTripValueProvider(): array
{
return [
'positive_int' => ['positive_int', 12345],
'zero_int' => ['zero_int', 0],
'negative_int' => ['negative_int', -7],
'large_int' => ['large_int', \time()],
'float' => ['float', 3.14],
'bool_true' => ['bool_true', true],
'bool_false' => ['bool_false', false],
'empty_string' => ['empty_string', ''],
'zero_string' => ['zero_string', '0'],
'numeric_string'=> ['numeric_string', '12345'],
'string' => ['string', 'hello'],
'null' => ['null', null],
'array' => ['array', ['a' => 1, 'b' => [2, 3]]],
];
}

public function testKeyValidation(): void
{
// Test with empty key
Expand Down
38 changes: 38 additions & 0 deletions tests/Cache/Adapters/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spin\tests\Core;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\SimpleCache\InvalidArgumentException;
use Spin\Cache\Adapters\Redis;
use Spin\Exceptions\CacheException;
Expand Down Expand Up @@ -344,6 +345,43 @@ public function testDriverName(): void
$this->assertEquals('Redis', $driver);
}

/**
* Regression for #78 — every supported value type must survive a
* set()/get() round-trip with its type and value intact.
*
* @throws InvalidArgumentException
*/
#[DataProvider('roundTripValueProvider')]
public function testValueRoundTrip(string $label, mixed $value): void
{
$key = 'roundtrip_' . $label;

$this->assertTrue($this->cacheObj->set($key, $value));
$this->assertSame($value, $this->cacheObj->get($key), "Round-trip failed for: {$label}");
}

/**
* @return array<string,array{0:string,1:mixed}>
*/
public static function roundTripValueProvider(): array
{
return [
'positive_int' => ['positive_int', 12345],
'zero_int' => ['zero_int', 0],
'negative_int' => ['negative_int', -7],
'large_int' => ['large_int', \time()],
'float' => ['float', 3.14],
'bool_true' => ['bool_true', true],
'bool_false' => ['bool_false', false],
'empty_string' => ['empty_string', ''],
'zero_string' => ['zero_string', '0'],
'numeric_string'=> ['numeric_string', '12345'],
'string' => ['string', 'hello'],
'null' => ['null', null],
'array' => ['array', ['a' => 1, 'b' => [2, 3]]],
];
}

/**
* Test that values persist across different instances
*
Expand Down