Skip to content

[Cache] Test & tweak CacheItem::validateKey() #18604

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

Merged
merged 1 commit into from
Apr 20, 2016
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
4 changes: 3 additions & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ public function __destruct()

private function getId($key)
{
return $this->namespace.CacheItem::validateKey($key);
CacheItem::validateKey($key);

return $this->namespace.$key;
}

private function generateItems($items, &$keys)
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public function getItems(array $keys = array())
*/
public function hasItem($key)
{
return isset($this->expiries[CacheItem::validateKey($key)]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
CacheItem::validateKey($key);

return isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
}

/**
Expand All @@ -102,7 +104,9 @@ public function clear()
*/
public function deleteItem($key)
{
unset($this->values[CacheItem::validateKey($key)], $this->expiries[$key]);
CacheItem::validateKey($key);

unset($this->values[$key], $this->expiries[$key]);

return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ public function getMisses()

private function getId($key)
{
return $this->namespace.CacheItem::validateKey($key);
CacheItem::validateKey($key);

return $this->namespace.$key;
}
}
4 changes: 0 additions & 4 deletions src/Symfony/Component/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ public function expiresAfter($time)
*
* @param string $key The key to validate.
*
* @return string $key if it is valid.
*
* @throws InvalidArgumentException When $key is not valid.
*/
public static function validateKey($key)
Expand All @@ -119,8 +117,6 @@ public static function validateKey($key)
if (isset($key[strcspn($key, '{}()/\@:')])) {
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
}

return $key;
}

/**
Expand Down
53 changes: 53 additions & 0 deletions src/Symfony/Component/Cache/Tests/CacheItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests;

use Symfony\Component\Cache\CacheItem;

class CacheItemTest extends \PHPUnit_Framework_TestCase
{
public function testValidKey()
{
$this->assertNull(CacheItem::validateKey('foo'));
}

/**
* @dataProvider provideInvalidKey
* @expectedException Symfony\Component\Cache\Exception\InvalidArgumentException
* @expectedExceptionMessage Cache key
*/
public function testInvalidKey($key)
{
CacheItem::validateKey($key);
}

public function provideInvalidKey()
{
return array(
array(''),
array('{'),
array('}'),
array('('),
array(')'),
array('/'),
array('\\'),
array('@'),
array(':'),
array(true),
array(null),
array(1),
array(1.1),
array(array()),
array(new \Exception('foo')),
);
}
}