Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getMultiple returns invalid values when use a numeric key #39

Open
arima-ryunosuke opened this issue Mar 17, 2021 · 0 comments
Open

getMultiple returns invalid values when use a numeric key #39

arima-ryunosuke opened this issue Mar 17, 2021 · 0 comments

Comments

@arima-ryunosuke
Copy link

Numeric keys (like '1') are valid keys in PSR-16.

However getMultiple returns invalid values because it uses array_merge (array_merge renumbers the numeric index of an array).
It should use array_replace here.

test code:

public function testGetMultipleWithIntegerKeys()
{
    $values = [
        '1' => uniqid('value1', true),
        '2' => uniqid('value2', true),
    ];
    $keys = array_map('strval', array_keys($values));

    $psrCache = new SimpleCacheAdapter(new ArrayCache());
    foreach ($values as $k => $v) {
        $psrCache->set((string) $k, $v);
    }

    $default = uniqid('default', true);
    $invalid_key = uniqid('key3', true);
    $keys[] = $invalid_key;
    $values[$invalid_key] = $default;

    self::assertSame($values, $psrCache->getMultiple($keys, $default));
}

results:

1) RoaveTest\DoctrineSimpleCache\SimpleCacheAdapterTest::testGetMultipleWithIntegerKeys
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
 Array &0 (
-    1 => 'value160519bac1ea400.91716645'
-    2 => 'value260519bac1ea486.88148617'
+    0 => 'default60519bac1ea755.33267710'
+    1 => 'default60519bac1ea755.33267710'
     'key360519bac1ea776.08717164' => 'default60519bac1ea755.33267710'
+    2 => 'value160519bac1ea400.91716645'
+    3 => 'value260519bac1ea486.88148617'
 )

Fix as follows.

public function getMultiple($keys, $default = null) : array
{
    $keys = $this->filterValidateMultipleKeys($keys);
    return array_replace(array_fill_keys($keys, $default), $this->doctrineCache->fetchMultiple($keys));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant