|
| 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\Bundle\FrameworkBundle\Tests\Command; |
| 13 | + |
| 14 | +use Psr\Cache\CacheItemPoolInterface; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Command\CachePooltemDeleteCommand; |
| 16 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 17 | +use Symfony\Bundle\FrameworkBundle\Tests\TestCase; |
| 18 | +use Symfony\Component\Console\Tester\CommandTester; |
| 19 | +use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer; |
| 20 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 21 | + |
| 22 | +class CachePooltemDeleteCommandTest extends TestCase |
| 23 | +{ |
| 24 | + private $cachePool; |
| 25 | + |
| 26 | + protected function setUp() |
| 27 | + { |
| 28 | + $this->cachePool = $this->getMockBuilder(CacheItemPoolInterface::class) |
| 29 | + ->getMock(); |
| 30 | + } |
| 31 | + |
| 32 | + public function testCommandWithValidKey() |
| 33 | + { |
| 34 | + $this->cachePool->expects($this->once()) |
| 35 | + ->method('hasItem') |
| 36 | + ->with('bar') |
| 37 | + ->willReturn(true); |
| 38 | + |
| 39 | + $this->cachePool->expects($this->once()) |
| 40 | + ->method('deleteItem') |
| 41 | + ->with('bar') |
| 42 | + ->willReturn(true); |
| 43 | + |
| 44 | + $tester = $this->getCommandTester($this->getKernel()); |
| 45 | + $tester->execute(array('pool' => 'foo', 'key' => 'bar')); |
| 46 | + |
| 47 | + $this->assertContains('[OK] Cache item "bar" was successfully deleted.', $tester->getDisplay()); |
| 48 | + } |
| 49 | + |
| 50 | + public function testCommandWithInValidKey() |
| 51 | + { |
| 52 | + $this->cachePool->expects($this->once()) |
| 53 | + ->method('hasItem') |
| 54 | + ->with('bar') |
| 55 | + ->willReturn(false); |
| 56 | + |
| 57 | + $this->cachePool->expects($this->never()) |
| 58 | + ->method('deleteItem') |
| 59 | + ->with('bar'); |
| 60 | + |
| 61 | + $tester = $this->getCommandTester($this->getKernel()); |
| 62 | + $tester->execute(array('pool' => 'foo', 'key' => 'bar')); |
| 63 | + |
| 64 | + $this->assertContains('[WARNING] Cache item "bar" does not exist in cache pool "foo".', $tester->getDisplay()); |
| 65 | + } |
| 66 | + |
| 67 | + public function testCommandDeleteFailed() |
| 68 | + { |
| 69 | + $this->cachePool->expects($this->once()) |
| 70 | + ->method('hasItem') |
| 71 | + ->with('bar') |
| 72 | + ->willReturn(true); |
| 73 | + |
| 74 | + $this->cachePool->expects($this->once()) |
| 75 | + ->method('deleteItem') |
| 76 | + ->with('bar') |
| 77 | + ->willReturn(false); |
| 78 | + |
| 79 | + if (method_exists($this, 'expectExceptionMessage')) { |
| 80 | + $this->expectExceptionMessage('Cache item "bar" could not be deleted.'); |
| 81 | + } else { |
| 82 | + $this->setExpectedException('Exception', 'Cache item "bar" could not be deleted.'); |
| 83 | + } |
| 84 | + |
| 85 | + $tester = $this->getCommandTester($this->getKernel()); |
| 86 | + $tester->execute(array('pool' => 'foo', 'key' => 'bar')); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return \PHPUnit_Framework_MockObject_MockObject|KernelInterface |
| 91 | + */ |
| 92 | + private function getKernel() |
| 93 | + { |
| 94 | + $container = $this |
| 95 | + ->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface') |
| 96 | + ->getMock(); |
| 97 | + |
| 98 | + $kernel = $this |
| 99 | + ->getMockBuilder(KernelInterface::class) |
| 100 | + ->getMock(); |
| 101 | + |
| 102 | + $kernel |
| 103 | + ->expects($this->any()) |
| 104 | + ->method('getContainer') |
| 105 | + ->willReturn($container); |
| 106 | + |
| 107 | + $kernel |
| 108 | + ->expects($this->once()) |
| 109 | + ->method('getBundles') |
| 110 | + ->willReturn(array()); |
| 111 | + |
| 112 | + return $kernel; |
| 113 | + } |
| 114 | + |
| 115 | + private function getCommandTester(KernelInterface $kernel): CommandTester |
| 116 | + { |
| 117 | + $application = new Application($kernel); |
| 118 | + $application->add(new CachePooltemDeleteCommand(new Psr6CacheClearer(array('foo' => $this->cachePool)))); |
| 119 | + |
| 120 | + return new CommandTester($application->find('cache:pool:delete')); |
| 121 | + } |
| 122 | +} |
0 commit comments