|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenForgeProject\MageForge\Test\Unit\Service; |
| 6 | + |
| 7 | +use Magento\Framework\Shell; |
| 8 | +use OpenForgeProject\MageForge\Service\CacheCleaner; |
| 9 | +use PHPUnit\Framework\MockObject\MockObject; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 12 | + |
| 13 | +class CacheCleanerTest extends TestCase |
| 14 | +{ |
| 15 | + private Shell&MockObject $shell; |
| 16 | + private SymfonyStyle&MockObject $io; |
| 17 | + private CacheCleaner $cacheCleaner; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + $this->shell = $this->createMock(Shell::class); |
| 22 | + $this->io = $this->createMock(SymfonyStyle::class); |
| 23 | + $this->cacheCleaner = new CacheCleaner($this->shell); |
| 24 | + } |
| 25 | + |
| 26 | + public function testCleansFrontendCacheTypes(): void |
| 27 | + { |
| 28 | + $this->shell |
| 29 | + ->expects($this->once()) |
| 30 | + ->method('execute') |
| 31 | + ->with('bin/magento cache:clean full_page block_html layout translate'); |
| 32 | + |
| 33 | + $this->assertTrue($this->cacheCleaner->clean($this->io, false)); |
| 34 | + } |
| 35 | + |
| 36 | + public function testPrintsProgressInVerboseMode(): void |
| 37 | + { |
| 38 | + $this->io->expects($this->once())->method('text')->with('Cleaning cache...'); |
| 39 | + $this->io->expects($this->once())->method('success')->with('Cache cleaned successfully.'); |
| 40 | + |
| 41 | + $this->assertTrue($this->cacheCleaner->clean($this->io, true)); |
| 42 | + } |
| 43 | + |
| 44 | + public function testStaysQuietWhenNotVerbose(): void |
| 45 | + { |
| 46 | + $this->io->expects($this->never())->method('text'); |
| 47 | + $this->io->expects($this->never())->method('success'); |
| 48 | + |
| 49 | + $this->assertTrue($this->cacheCleaner->clean($this->io, false)); |
| 50 | + } |
| 51 | + |
| 52 | + public function testReturnsFalseAndPrintsErrorWhenShellFails(): void |
| 53 | + { |
| 54 | + $this->shell->method('execute')->willThrowException(new \RuntimeException('cache backend gone')); |
| 55 | + $this->io->expects($this->once())->method('error')->with('Failed to clean cache: cache backend gone'); |
| 56 | + |
| 57 | + $this->assertFalse($this->cacheCleaner->clean($this->io, true)); |
| 58 | + } |
| 59 | +} |
0 commit comments