|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +namespace Magento\Backend\Test\Unit\Plugin; |
| 7 | + |
| 8 | +use Magento\Backend\Plugin\StoreSaveFlushConfig; |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 10 | +use Magento\Backend\Controller\Adminhtml\System\Store\Save; |
| 11 | +use Magento\Framework\App\Cache\TypeListInterface; |
| 12 | +use Magento\Framework\App\RequestInterface; |
| 13 | +use Magento\Backend\Model\View\Result\Redirect; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class StoreSaveFlushConfigTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var ObjectManager |
| 21 | + */ |
| 22 | + private $objectManager; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var StoreSaveFlushConfig |
| 26 | + */ |
| 27 | + private $plugin; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var TypeListInterface|MockObject |
| 31 | + */ |
| 32 | + private $cacheTypeListMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Save|MockObject |
| 36 | + */ |
| 37 | + private $subjectMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var RequestInterface|MockObject |
| 41 | + */ |
| 42 | + private $requestMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Redirect|MockObject |
| 46 | + */ |
| 47 | + private $resultMock; |
| 48 | + |
| 49 | + protected function setUp(): void |
| 50 | + { |
| 51 | + $this->objectManager = new ObjectManager($this); |
| 52 | + $this->cacheTypeListMock = $this->createMock(TypeListInterface::class); |
| 53 | + $this->subjectMock = $this->createMock(Save::class); |
| 54 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class) |
| 55 | + ->addMethods(['getPostValue']) |
| 56 | + ->disableOriginalConstructor() |
| 57 | + ->getMockForAbstractClass(); |
| 58 | + $this->resultMock = $this->createMock(Redirect::class); |
| 59 | + $this->plugin = $this->objectManager->getObject( |
| 60 | + StoreSaveFlushConfig::class, |
| 61 | + ['cacheTypeList' => $this->cacheTypeListMock] |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function testAfterExecuteWithStoreTypeStoreClearsConfigCache() |
| 66 | + { |
| 67 | + $postData = ['store_type' => 'store']; |
| 68 | + $this->subjectMock->method('getRequest')->willReturn($this->requestMock); |
| 69 | + $this->requestMock->method('getPostValue')->willReturn($postData); |
| 70 | + $this->cacheTypeListMock->expects($this->once())->method('cleanType')->with('config'); |
| 71 | + $result = $this->plugin->afterExecute($this->subjectMock, $this->resultMock); |
| 72 | + $this->assertSame($this->resultMock, $result); |
| 73 | + } |
| 74 | + |
| 75 | + public function testAfterExecuteWithNonStoreTypeDoesNotClearCache() |
| 76 | + { |
| 77 | + $postData = ['store_type' => 'website']; |
| 78 | + $this->subjectMock->method('getRequest')->willReturn($this->requestMock); |
| 79 | + $this->requestMock->method('getPostValue')->willReturn($postData); |
| 80 | + $this->cacheTypeListMock->expects($this->never())->method('cleanType'); |
| 81 | + $result = $this->plugin->afterExecute($this->subjectMock, $this->resultMock); |
| 82 | + $this->assertSame($this->resultMock, $result); |
| 83 | + } |
| 84 | +} |
0 commit comments