|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSHttpCacheBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\HttpCacheBundle\Tests\Unit\Command; |
| 13 | + |
| 14 | +use FOS\HttpCache\CacheInvalidator; |
| 15 | +use FOS\HttpCacheBundle\CacheManager; |
| 16 | +use FOS\HttpCacheBundle\Command\ClearCommand; |
| 17 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Symfony\Component\Console\Application; |
| 20 | +use Symfony\Component\Console\Tester\CommandTester; |
| 21 | + |
| 22 | +class ClearCommandTest extends TestCase |
| 23 | +{ |
| 24 | + use MockeryPHPUnitIntegration; |
| 25 | + |
| 26 | + public function testExecuteClear() |
| 27 | + { |
| 28 | + $invalidator = \Mockery::mock(CacheManager::class) |
| 29 | + ->shouldReceive('supports')->once()->with(CacheInvalidator::CLEAR)->andReturnTrue() |
| 30 | + ->shouldReceive('clearCache')->once() |
| 31 | + ->getMock() |
| 32 | + ; |
| 33 | + |
| 34 | + $application = new Application(); |
| 35 | + $application->add(new ClearCommand($invalidator)); |
| 36 | + |
| 37 | + $command = $application->find('fos:httpcache:clear'); |
| 38 | + $commandTester = new CommandTester($command); |
| 39 | + $commandTester->execute([ |
| 40 | + 'command' => $command->getName(), |
| 41 | + ]); |
| 42 | + |
| 43 | + // the only output should be generated by the listener in verbose mode |
| 44 | + $this->assertEquals('', $commandTester->getDisplay()); |
| 45 | + } |
| 46 | + |
| 47 | + public function testExecuteInvalidate() |
| 48 | + { |
| 49 | + $invalidator = \Mockery::mock(CacheManager::class) |
| 50 | + ->shouldReceive('supports')->once()->with(CacheInvalidator::CLEAR)->andReturnFalse() |
| 51 | + ->shouldReceive('supports')->once()->with(CacheInvalidator::INVALIDATE)->andReturnTrue() |
| 52 | + ->shouldReceive('invalidateRegex')->once()->with('.*') |
| 53 | + ->getMock() |
| 54 | + ; |
| 55 | + |
| 56 | + $application = new Application(); |
| 57 | + $application->add(new ClearCommand($invalidator)); |
| 58 | + |
| 59 | + $command = $application->find('fos:httpcache:clear'); |
| 60 | + $commandTester = new CommandTester($command); |
| 61 | + $commandTester->execute([ |
| 62 | + 'command' => $command->getName(), |
| 63 | + ]); |
| 64 | + |
| 65 | + // the only output should be generated by the listener in verbose mode |
| 66 | + $this->assertEquals('', $commandTester->getDisplay()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testExecuteNotSupported() |
| 70 | + { |
| 71 | + $invalidator = \Mockery::mock(CacheManager::class) |
| 72 | + ->shouldReceive('supports')->once()->with(CacheInvalidator::CLEAR)->andReturnFalse() |
| 73 | + ->shouldReceive('supports')->once()->with(CacheInvalidator::INVALIDATE)->andReturnfalse() |
| 74 | + ->getMock() |
| 75 | + ; |
| 76 | + |
| 77 | + $application = new Application(); |
| 78 | + $application->add(new ClearCommand($invalidator)); |
| 79 | + |
| 80 | + $command = $application->find('fos:httpcache:clear'); |
| 81 | + $commandTester = new CommandTester($command); |
| 82 | + $commandTester->execute([ |
| 83 | + 'command' => $command->getName(), |
| 84 | + ]); |
| 85 | + |
| 86 | + $this->assertStringContainsString( |
| 87 | + 'The configured HTTP cache does not support "clear" or "invalidate".', |
| 88 | + $commandTester->getDisplay() |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments