8000 minor #18604 [Cache] Test & tweak CacheItem::validateKey() (nicolas-g… · symfony/symfony@c4acbb4 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4acbb4

Browse files
minor #18604 [Cache] Test & tweak CacheItem::validateKey() (nicolas-grekas)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Cache] Test & tweak CacheItem::validateKey() | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Ping @javiereguiluz @stof Commits ------- 4256add [Cache] Test & tweak CacheItem::validateKey()
2 parents 357785a + 4256add commit c4acbb4

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ public function __destruct()
337337

338338
private function getId($key)
339339
{
340-
return $this->namespace.CacheItem::validateKey($key);
340+
CacheItem::validateKey($key);
341+
342+
return $this->namespace.$key;
341343
}
342344

343345
private function generateItems($items, &$keys)

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public function getItems(array $keys = array())
8484
*/
8585
public function hasItem($key)
8686
{
87-
return isset($this->expiries[CacheItem::validateKey($key)]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
87+
CacheItem::validateKey($key);
88+
89+
return isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
8890
}
8991

9092
/**
@@ -102,7 +104,9 @@ public function clear()
102104
*/
103105
public function deleteItem($key)
104106
{
105-
unset($this->values[CacheItem::validateKey($key)], $this->expiries[$key]);
107+
CacheItem::validateKey($key);
108+
109+
unset($this->values[$key], $this->expiries[$key]);
106110

107111
return true;
108112
}

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ public function getMisses()
193193

194194
private function getId($key)
195195
{
196-
return $this->namespace.CacheItem::validateKey($key);
196+
CacheItem::validateKey($key);
197+
198+
return $this->namespace.$key;
197199
}
198200
}

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ public function expiresAfter($time)
104104
*
105105
* @param string $key The key to validate.
106106
*
107-
* @return string $key if it is valid.
108-
*
109107
* @throws InvalidArgumentException When $key is not valid.
110108
*/
111109
public static function validateKey($key)
@@ -119,8 +117,6 @@ public static function validateKey($key)
119117
if (isset($key[strcspn($key, '{}()/\@:')])) {
120118
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
121119
}
122-
123-
return $key;
124120
}
125121

126122
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests;
13+
14+
use Symfony\Component\Cache\CacheItem;
15+
16+
class CacheItemTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testValidKey()
19+
{
20+
$this->assertNull(CacheItem::validateKey('foo'));
21+
}
22+
23+
/**
24+
* @dataProvider provideInvalidKey
25+
* @expectedException Symfony\Component\Cache\Exception\InvalidArgumentException
26+
* @expectedExceptionMessage Cache key
27+
*/
28+
public function testInvalidKey($key)
29+
{
30+
CacheItem::validateKey($key);
31+
}
32+
33+
public function provideInvalidKey()
34+
{
35+
return array(
36+
array(''),
37+
array('{'),
38+
array('}'),
39+
array('('),
40+
array(')'),
41+
array('/'),
42+
array('\\'),
43+
array('@'),
44+
array(':'),
45+
array(true),
46+
array(null),
47+
array(1),
48+
array(1.1),
49+
array(array()),
50+
array(new \Exception('foo')),
51+
);
52+
}
53+
}

0 commit comments

Comments
 (0)
0