Skip to content

Commit f587a55

Browse files
committed
add command tests
1 parent 337e427 commit f587a55

File tree

1 file changed

+119
-3
lines changed

1 file changed

+119
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolInvalidateTagsCommandTest.php

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,128 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
1313

14+
use Symfony\Bundle\FrameworkBundle\Command\CachePoolInvalidateTagsCommand;
1415
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Tester\CommandCompletionTester;
18+
use Symfony\Component\Console\Tester\CommandTester;
19+
use Symfony\Component\DependencyInjection\ServiceLocator;
20+
use Symfony\Contracts\Cache\TagAwareCacheInterface;
1521

16-
final class CachePoolInvalidateTagsCommandTest extends TestCase
22+
class CachePoolInvalidateTagsCommandTest extends TestCase
1723
{
18-
public function testCommandWithPools()
24+
public function testComplete()
1925
{
20-
$this->markTestIncomplete('TODO');
26+
$tester = new CommandCompletionTester($this->createCommand(['foo' => null, 'bar' => null]));
27+
28+
$suggestions = $tester->complete(['--pool=']);
29+
30+
$this->assertSame(['foo', 'bar'], $suggestions);
31+
}
32+
33+
public function testInvalidatesTagsForAllPoolsByDefault()
34+
{
35+
$tagsToInvalidate = ['tag1', 'tag2'];
36+
37+
$foo = $this->createMock(TagAwareCacheInterface::class);
38+
$foo->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true);
39+
40+
$bar = $this->createMock(TagAwareCacheInterface::class);
41+
$bar->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true);
42+
43+
$tester = new CommandTester($this->createCommand([
44+
'foo' => $foo,
45+
'bar' => $bar,
46+
]));
47+
48+
$ret = $tester->execute(['tags' => $tagsToInvalidate]);
49+
50+
$this->assertSame(Command::SUCCESS, $ret);
51+
}
52+
53+
public function testCanInvalidateSpecificPools()
54+
{
55+
$tagsToInvalidate = ['tag1', 'tag2'];
56+
57+
$foo = $this->createMock(TagAwareCacheInterface::class);
58+
$foo->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true);
59+
60+
$bar = $this->createMock(TagAwareCacheInterface::class);
61+
$bar->expects($this->never())->method('invalidateTags');
62+
63+
$tester = new CommandTester($this->createCommand([
64+
'foo' => $foo,
65+
'bar' => $bar,
66+
]));
67+
68+
$ret = $tester->execute(['tags' => $tagsToInvalidate, '--pool' => ['foo']]);
69+
70+
$this->assertSame(Command::SUCCESS, $ret);
71+
}
72+
73+
public function testCommandFailsIfPoolNotFound()
74+
{
75+
$tagsToInvalidate = ['tag1', 'tag2'];
76+
77+
$foo = $this->createMock(TagAwareCacheInterface::class);
78+
$foo->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true);
79+
80+
$bar = $this->createMock(TagAwareCacheInterface::class);
81+
$bar->expects($this->never())->method('invalidateTags');
82+
83+
$tester = new CommandTester($this->createCommand([
84+
'foo' => $foo,
85+
'bar' => $bar,
86+
]));
87+
88+
$ret = $tester->execute(['tags' => $tagsToInvalidate, '--pool' => ['invalid', 'foo']]);
89+
90+
$this->assertSame(Command::FAILURE, $ret);
91+
}
92+
93+
public function testCommandFailsIfPoolNotTaggable()
94+
{
95+
$tagsToInvalidate = ['tag1', 'tag2'];
96+
97+
$foo = new \stdClass();
98+
99+
$bar = $this->createMock(TagAwareCacheInterface::class);
100+
$bar->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true);
101+
102+
$tester = new CommandTester($this->createCommand([
103+
'foo' => $foo,
104+
'bar' => $bar,
105+
]));
106+
107+
$ret = $tester->execute(['tags' => $tagsToInvalidate]);
108+
109+
$this->assertSame(Command::FAILURE, $ret);
110+
}
111+
112+
public function testCommandFailsIfInvalidatingTagsFails()
113+
{
114+
$tagsToInvalidate = ['tag1', 'tag2'];
115+
116+
$foo = $this->createMock(TagAwareCacheInterface::class);
117+
$foo->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(false);
118+
119+
$bar = $this->createMock(TagAwareCacheInterface::class);
120+
$bar->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true);
121+
122+
$tester = new CommandTester($this->createCommand([
123+
'foo' => $foo,
124+
'bar' => $bar,
125+
]));
126+
127+
$ret = $tester->execute(['tags' => $tagsToInvalidate]);
128+
129+
$this->assertSame(Command::FAILURE, $ret);
130+
}
131+
132+
private function createCommand(array $services): CachePoolInvalidateTagsCommand
133+
{
134+
return new CachePoolInvalidateTagsCommand(
135+
new ServiceLocator(array_map(fn ($service) => fn () => $service, $services))
136+
);
21137
}
22138
}

0 commit comments

Comments
 (0)