Skip to content

Commit

Permalink
Merge pull request #15350 from Izopi4a/fix-15125
Browse files Browse the repository at this point in the history
Storage adapters returning wrong data when data is emptry array Fixes #15125
  • Loading branch information
niden committed Mar 28, 2021
2 parents 01d24f9 + ee26911 commit 084e9c5
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 115 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
- Fixed `Phalcon\Mvc\Model::average()` to return `float` value when is `string` [#15287](https://github.com/phalcon/cphalcon/pull/15287)
- Fixed `Phalcon\Storage\Serializer\Igbinary` to store `is_numeric` and `bool` values properly [#15240](https://github.com/phalcon/cphalcon/pull/15240)
- Fixed `Phalcon\Validation\Validator\Confirmation` was failing to compare cases such as 000123 = 123 [#15347](https://github.com/phalcon/cphalcon/pull/15347)
- Fixed `Phalcon\Storage\Adapter` failing to retrieve empty like stored data (such as [], 0, false) [15125](https://github.com/phalcon/cphalcon/issues/15125)
- Fixed declarations for `function getEventsManager()` to allow null return [15010](https://github.com/phalcon/cphalcon/issues/15010)
- Removed underscore from method names (starting) to abide with PSR-12 [15345](https://github.com/phalcon/cphalcon/issues/15345)
5 changes: 1 addition & 4 deletions phalcon/Storage/Adapter/AbstractAdapter.zep
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,8 @@ abstract class AbstractAdapter implements AdapterInterface
/**
* Returns unserialized data
*/
protected function getUnserializedData(var content, var defaultValue = null) -> var
protected function getUnserializedData(var content) -> var
{
if !content {
return defaultValue;
}

if this->defaultSerializer !== "" {
this->serializer->unserialize(content);
Expand Down
6 changes: 5 additions & 1 deletion phalcon/Storage/Adapter/Apcu.zep
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ class Apcu extends AbstractAdapter
{
var content;

if this->has(key) == false {
return defaultValue;
}

let content = apcu_fetch(this->getPrefixedKey(key));

return this->getUnserializedData(content, defaultValue);
return this->getUnserializedData(content);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions phalcon/Storage/Adapter/Libmemcached.zep
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ class Libmemcached extends AbstractAdapter
*/
public function get(string! key, var defaultValue = null) -> var
{
return this->getUnserializedData(
this->getAdapter()->get(key),
defaultValue
);

if this->has(key) == false {
return defaultValue;
}

return this->getUnserializedData(this->getAdapter()->get(key));
}

/**
Expand Down
6 changes: 5 additions & 1 deletion phalcon/Storage/Adapter/Memory.zep
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,14 @@ class Memory extends AbstractAdapter
{
var content, prefixedKey;

if this->has(key) == false {
return defaultValue;
}

let prefixedKey = this->getPrefixedKey(key),
content = this->data->get(prefixedKey);

return this->getUnserializedData(content, defaultValue);
return this->getUnserializedData(content);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions phalcon/Storage/Adapter/Redis.zep
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ class Redis extends AbstractAdapter
*/
public function get(string! key, var defaultValue = null) -> var
{
return this->getUnserializedData(
this->getAdapter()->get(key),
defaultValue
);

if this->has(key) == false {
return defaultValue;
}

return this->getUnserializedData(this->getAdapter()->get(key));
}

/**
Expand Down
6 changes: 5 additions & 1 deletion phalcon/Storage/Adapter/Stream.zep
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ class Stream extends AbstractAdapter
{
var content, filepath, payload;

if this->has(key) == false {
return defaultValue;
}

let filepath = this->getFilepath(key);

if !file_exists(filepath) {
Expand All @@ -163,7 +167,7 @@ class Stream extends AbstractAdapter

let content = Arr::get(payload, "content", null);

return this->getUnserializedData(content, defaultValue);
return this->getUnserializedData(content);
}

/**
Expand Down
71 changes: 71 additions & 0 deletions tests/_data/fixtures/Cache/CacheFixtureData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Fixtures\Cache;

use stdClass;

/**
* Phalcon\Test\Fixtures\Cache
*
*/
class CacheFixtureData
{

public static function getExamples(): array
{

return [
[
'string',
'random string',
],
[
'integer',
123456,
],
[
'float',
123.456,
],
[
'boolean',
true,
],
[
'object',
new stdClass(),
],
[
'empty array',
[],
],
[
'boolean false',
false,
],
[
'integer 0',
0,
],
[
'string 0',
"0",
],
[
'null',
null,
],
];
}
}
21 changes: 21 additions & 0 deletions tests/integration/Cache/Adapter/Apcu/GetKeysCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,25 @@ public function cacheAdapterApcuGetKeys(IntegrationTester $I)
sort($actual);
$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Cache\Adapter\Apcu :: GetNoNExistingKeys()
*
* @author Phalcon Team <team@phalcon.io>
* @since 2021-03-28
*/
public function cacheAdapterApcuGetNoNExistingKeys(IntegrationTester $I)
{
$I->wantToTest('Cache\Adapter\Apcu - GetNoNExistingKeys()');

$serializer = new SerializerFactory();
$adapter = new Apcu($serializer);

$adapter->clear();
$key = 'random-non-existing-key';

$I->assertNull($adapter->get($key));
$I->assertEquals(123, $adapter->get($key, 123));
$I->assertFalse($adapter->get($key, false));
}
}
25 changes: 2 additions & 23 deletions tests/integration/Cache/Adapter/Apcu/GetSetCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use Codeception\Example;
use Phalcon\Cache\Adapter\Apcu;
use Phalcon\Storage\SerializerFactory;
use Phalcon\Test\Fixtures\Cache\CacheFixtureData;
use Phalcon\Test\Fixtures\Traits\ApcuTrait;
use stdClass;
use IntegrationTester;

class GetSetCest
Expand Down Expand Up @@ -53,27 +53,6 @@ public function cacheAdapterApcuGetSet(IntegrationTester $I, Example $example)

private function getExamples(): array
{
return [
[
'string',
'random string',
],
[
'integer',
123456,
],
[
'float',
123.456,
],
[
'boolean',
true,
],
[
'object',
new stdClass(),
],
];
return CacheFixtureData::getExamples();
}
}
24 changes: 24 additions & 0 deletions tests/integration/Cache/Adapter/Libmemcached/GetKeysCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,28 @@ public function cacheAdapterLibmemcachedGetKeys(IntegrationTester $I)
$actual
);
}

/**
* Tests Phalcon\Cache\Adapter\Libmemcached :: GetNoNExistingKeys()
*
* @author Phalcon Team <team@phalcon.io>
* @since 2021-03-28
*/
public function cacheAdapterLibmemcachedGetNoNExistingKeys(IntegrationTester $I)
{
$I->wantToTest('Cache\Adapter\Libmemcached - GetNoNExistingKeys()');

$serializer = new SerializerFactory();

$adapter = new Libmemcached(
$serializer,
getOptionsLibmemcached()
);

$key = 'random-non-existing-key';

$I->assertNull($adapter->get($key));
$I->assertEquals(123, $adapter->get($key, 123));
$I->assertFalse($adapter->get($key, false));
}
}
33 changes: 2 additions & 31 deletions tests/integration/Cache/Adapter/Libmemcached/GetSetCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Phalcon\Cache\Adapter\Libmemcached;
use Phalcon\Storage\Exception;
use Phalcon\Storage\SerializerFactory;
use Phalcon\Test\Fixtures\Cache\CacheFixtureData;
use Phalcon\Test\Fixtures\Traits\LibmemcachedTrait;
use stdClass;
use IntegrationTester;

use function getOptionsLibmemcached;
Expand Down Expand Up @@ -96,35 +96,6 @@ public function cacheAdapterLibmemcachedGetSetCustomSerializer(IntegrationTester

private function getExamples(): array
{
return [
[
'string',
'random string',
],
[
'integer',
123456,
],
[
'float',
123.456,
],
[
'boolean true',
true,
],
[
'boolean false',
false,
],
[
'null',
null,
],
[
'object',
new stdClass(),
],
];
return CacheFixtureData::getExamples();
}
}
21 changes: 21 additions & 0 deletions tests/integration/Cache/Adapter/Memory/GetKeysCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,25 @@ public function cacheAdapterMemoryGetKeys(IntegrationTester $I)
sort($actual);
$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Cache\Adapter\Memory :: GetNoNExistingKeys()
*
* @author Phalcon Team <team@phalcon.io>
* @since 2021-03-28
*/
public function cacheAdapterMemoryGetNoNExistingKeys(IntegrationTester $I)
{
$I->wantToTest('Cache\Adapter\Memory - GetNoNExistingKeys()');

$serializer = new SerializerFactory();
$adapter = new Memory($serializer);

$adapter->clear();
$key = 'random-non-existing-key';

$I->assertNull($adapter->get($key));
$I->assertEquals(123, $adapter->get($key, 123));
$I->assertFalse($adapter->get($key, false));
}
}
25 changes: 2 additions & 23 deletions tests/integration/Cache/Adapter/Memory/GetSetCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Codeception\Example;
use Phalcon\Cache\Adapter\Memory;
use Phalcon\Storage\SerializerFactory;
use stdClass;
use Phalcon\Test\Fixtures\Cache\CacheFixtureData;
use IntegrationTester;

class GetSetCest
Expand Down Expand Up @@ -50,27 +50,6 @@ public function cacheAdapterMemoryGetSet(IntegrationTester $I, Example $example)

private function getExamples(): array
{
return [
[
'string',
'random string',
],
[
'integer',
123456,
],
[
'float',
123.456,
],
[
'boolean',
true,
],
[
'object',
new stdClass(),
],
];
return CacheFixtureData::getExamples();
}
}
Loading

0 comments on commit 084e9c5

Please sign in to comment.