-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathCachePoolClearCommandTest.php
153 lines (126 loc) · 6.59 KB
/
CachePoolClearCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
use Symfony\Bundle\FrameworkBundle\Command\CachePoolClearCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\Finder\SplFileInfo;
/**
* @group functional
*/
class CachePoolClearCommandTest extends AbstractWebTestCase
{
protected function setUp(): void
{
static::bootKernel(['test_case' => 'CachePoolClear', 'root_config' => 'config.yml']);
}
public function testClearPrivatePool()
{
$tester = $this->createCommandTester();
$tester->execute(['pools' => ['cache.private_pool']], ['decorated' => false]);
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
$this->assertStringContainsString('Clearing cache pool: cache.private_pool', $tester->getDisplay());
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
}
public function testClearPublicPool()
{
$tester = $this->createCommandTester();
$tester->execute(['pools' => ['cache.public_pool']], ['decorated' => false]);
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
$this->assertStringContainsString('Clearing cache pool: cache.public_pool', $tester->getDisplay());
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
}
public function testClearPoolWithCustomClearer()
{
$tester = $this->createCommandTester();
$tester->execute(['pools' => ['cache.pool_with_clearer']], ['decorated' => false]);
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
$this->assertStringContainsString('Clearing cache pool: cache.pool_with_clearer', $tester->getDisplay());
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
}
public function testCallClearer()
{
$tester = $this->createCommandTester();
$tester->execute(['pools' => ['cache.app_clearer']], ['decorated' => false]);
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
$this->assertStringContainsString('Calling cache clearer: cache.app_clearer', $tester->getDisplay());
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
}
public function testClearUnexistingPool()
{
$this->expectException(ServiceNotFoundException::class);
$this->expectExceptionMessage('You have requested a non-existent service "unknown_pool"');
$this->createCommandTester()
->execute(['pools' => ['unknown_pool']], ['decorated' => false]);
}
public function testClearAll()
{
$tester = $this->createCommandTester(['cache.app_clearer']);
$tester->execute(['--all' => true], ['decorated' => false]);
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
$this->assertStringContainsString('Clearing all cache pools...', $tester->getDisplay());
$this->assertStringContainsString('Calling cache clearer: cache.app_clearer', $tester->getDisplay());
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
}
public function testClearWithoutPoolNames()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Could not clear all cache pools, try specifying a specific pool or cache clearer.');
$this->createCommandTester()->execute(['--all' => true], ['decorated' => false]);
}
public function testClearNoOptions()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Either specify at least one pool name, or provide the --all option to clear all pools.');
$this->createCommandTester()->execute([], ['decorated' => false]);
}
public function testClearFailed()
{
$tester = $this->createCommandTester();
/** @var FilesystemAdapter $pool */
$pool = static::getContainer()->get('cache.public_pool');
$item = $pool->getItem('foo');
$item->set('baz');
$pool->save($item);
$r = new \ReflectionObject($pool);
$p = $r->getProperty('directory');
$poolDir = $p->getValue($pool);
/** @var SplFileInfo $entry */
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($poolDir)) as $entry) {
// converts files into dir to make adapter fail
if ($entry->isFile()) {
unlink($entry->getPathname());
mkdir($entry->getPathname());
}
}
$tester->execute(['pools' => ['cache.public_pool']]);
$this->assertSame(1, $tester->getStatusCode(), 'cache:pool:clear exits with 1 in case of error');
$this->assertStringNotContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
$this->assertStringContainsString('[WARNING] Cache pool "cache.public_pool" could not be cleared.', $tester->getDisplay());
}
public function testExcludedPool()
{
$tester = $this->createCommandTester(['cache.app_clearer']);
$tester->execute(['--all' => true, '--exclude' => ['cache.app_clearer']], ['decorated' => false]);
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
$this->assertStringNotContainsString('Clearing all cache pools...', $tester->getDisplay());
$this->assertStringNotContainsString('Calling cache clearer: cache.app_clearer', $tester->getDisplay());
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
}
private function createCommandTester(?array $poolNames = null)
{
$application = new Application(static::$kernel);
$application->add(new CachePoolClearCommand(static::getContainer()->get('cache.global_clearer'), $poolNames));
return new CommandTester($application->find('cache:pool:clear'));
}
}