Skip to content

Commit 01e72a5

Browse files
committed
[FrameworkBundle] Allow clearing private cache pools
1 parent c355df9 commit 01e72a5

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

CacheClearer/Psr6CacheClearer.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,32 @@ class Psr6CacheClearer implements CacheClearerInterface
2020
{
2121
private $pools = array();
2222

23+
public function __construct(array $pools = array())
24+
{
25+
$this->pools = $pools;
26+
}
27+
2328
public function addPool(CacheItemPoolInterface $pool)
2429
{
30+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Pass an array of pools indexed by name to the constructor instead.', __METHOD__), E_USER_DEPRECATED);
31+
2532
$this->pools[] = $pool;
2633
}
2734

35+
public function hasPool($name)
36+
{
37+
return isset($this->pools[$name]);
38+
}
39+
40+
public function clearPool($name)
41+
{
42+
if (!isset($this->pools[$name])) {
43+
throw new \InvalidArgumentException(sprintf('Cache pool not found: %s.', $name));
44+
}
45+
46+
return $this->pools[$name]->clear();
47+
}
48+
2849
/**
2950
* {@inheritdoc}
3051
*/
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\HttpKernel\Tests\CacheClearer;
13+
14+
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
15+
use Psr\Cache\CacheItemPoolInterface;
16+
17+
class Psr6CacheClearerTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testClearPoolsInjectedInConstructor()
20+
{
21+
$pool = $this->getMock(CacheItemPoolInterface::class);
22+
$pool
23+
->expects($this->once())
24+
->method('clear');
25+
26+
(new Psr6CacheClearer(array('pool' => $pool)))->clear('');
27+
}
28+
29+
public function testClearPool()
30+
{
31+
$pool = $this->getMock(CacheItemPoolInterface::class);
32+
$pool
33+
->expects($this->once())
34+
->method('clear');
35+
36+
(new Psr6CacheClearer(array('pool' => $pool)))->clearPool('pool');
37+
}
38+
39+
/**
40+
* @expectedException \InvalidArgumentException
41+
* @expectedExceptionMessage Cache pool not found: unknown
42+
*/
43+
public function testClearPoolThrowsExceptionOnUnreferencedPool()
44+
{
45+
(new Psr6CacheClearer())->clearPool('unknown');
46+
}
47+
48+
/**
49+
* @group legacy
50+
* @expectedDeprecation The Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer::addPool() method is deprecated since version 3.3 and will be removed in 4.0. Pass an array of pools indexed by name to the constructor instead.
51+
*/
52+
public function testClearPoolsInjectedByAdder()
53+
{
54+
$pool1 = $this->getMock(CacheItemPoolInterface::class);
55+
$pool1
56+
->expects($this->once())
57+
->method('clear');
58+
59+
$pool2 = $this->getMock(CacheItemPoolInterface::class);
60+
$pool2
61+
->expects($this->once())
62+
->method('clear');
63+
64+
$clearer = new Psr6CacheClearer(array('pool1' => $pool1));
65+
$clearer->addPool($pool2);
66+
$clearer->clear('');
67+
}
68+
}

0 commit comments

Comments
 (0)