|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Magento\PageCache\Test\Unit\Model\App\Request\Http; |
| 5 | + |
| 6 | +use Magento\Framework\App\Request\Http; |
| 7 | +use Magento\Framework\App\RequestInterface; |
| 8 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 9 | +use Magento\Framework\View\DesignExceptions; |
| 10 | +use Magento\PageCache\Model\Config; |
| 11 | +use Magento\Store\Model\StoreManager; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class IdentifierStoreReaderTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var DesignExceptions|\PHPUnit\Framework\MockObject\MockObject |
| 18 | + */ |
| 19 | + private $designExceptionsMock; |
| 20 | + /** |
| 21 | + * @var RequestInterface|\PHPUnit\Framework\MockObject\MockObject |
| 22 | + */ |
| 23 | + private \PHPUnit\Framework\MockObject\MockObject|RequestInterface $requestMock; |
| 24 | + /** |
| 25 | + * @var Config|\PHPUnit\Framework\MockObject\MockObject |
| 26 | + */ |
| 27 | + private $configMock; |
| 28 | + /** |
| 29 | + * @var \Magento\PageCache\Model\App\Request\Http\IdentifierStoreReader |
| 30 | + */ |
| 31 | + private $model; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->objectManager = new ObjectManager($this); |
| 36 | + |
| 37 | + $this->designExceptionsMock = $this->createPartialMock( |
| 38 | + DesignExceptions::class, |
| 39 | + ['getThemeByRequest'] |
| 40 | + ); |
| 41 | + |
| 42 | + $this->requestMock = $this->createMock(Http::class); |
| 43 | + |
| 44 | + $this->configMock = $this->getMockBuilder(Config::class) |
| 45 | + ->onlyMethods(['getType', 'isEnabled']) |
| 46 | + //->addMethods(['getType', 'isEnabled']) |
| 47 | + ->disableOriginalConstructor() |
| 48 | + ->getMockForAbstractClass(); |
| 49 | + |
| 50 | + $this->model = new \Magento\PageCache\Model\App\Request\Http\IdentifierStoreReader( |
| 51 | + $this->designExceptionsMock, |
| 52 | + $this->requestMock, |
| 53 | + $this->configMock |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public function testGetPageTagsWithStoreCacheTagsWhenVarnishCacheIsEnabled() |
| 58 | + { |
| 59 | + $this->configMock->expects($this->any()) |
| 60 | + ->method('getType') |
| 61 | + ->willReturn(\Magento\PageCache\Model\Config::VARNISH); |
| 62 | + |
| 63 | + $this->requestMock->expects($this->never())->method('getServerValue'); |
| 64 | + |
| 65 | + $data = ['anything']; |
| 66 | + $this->model->getPageTagsWithStoreCacheTags($data); |
| 67 | + } |
| 68 | + |
| 69 | + public function testGetPageTagsWithStoreCacheTagsWhenFPCIsDisabled() |
| 70 | + { |
| 71 | + $this->configMock->expects($this->any()) |
| 72 | + ->method('isEnabled') |
| 73 | + ->willReturn(false); |
| 74 | + |
| 75 | + $this->requestMock->expects($this->never())->method('getServerValue'); |
| 76 | + |
| 77 | + $data = ['anything']; |
| 78 | + $this->model->getPageTagsWithStoreCacheTags($data); |
| 79 | + } |
| 80 | + |
| 81 | + public function testGetPageTagsWithStoreCacheTagsWhenStoreDataAreInContext() |
| 82 | + { |
| 83 | + $this->configMock->expects($this->any()) |
| 84 | + ->method('isEnabled') |
| 85 | + ->willReturn(true); |
| 86 | + |
| 87 | + $this->configMock->expects($this->any()) |
| 88 | + ->method('getType') |
| 89 | + ->willReturn(\Magento\PageCache\Model\Config::BUILT_IN); |
| 90 | + |
| 91 | + $defaultRequestMock = clone $this->requestMock; |
| 92 | + $defaultRequestMock->expects($this->any()) |
| 93 | + ->method('getServerValue') |
| 94 | + ->willReturnCallback( |
| 95 | + function ($param) { |
| 96 | + if ($param == StoreManager::PARAM_RUN_TYPE) { |
| 97 | + return 'store'; |
| 98 | + } |
| 99 | + if ($param == StoreManager::PARAM_RUN_CODE) { |
| 100 | + return 'default'; |
| 101 | + } |
| 102 | + } |
| 103 | + ); |
| 104 | + |
| 105 | + $data = ['anything']; |
| 106 | + |
| 107 | + $this->model = new \Magento\PageCache\Model\App\Request\Http\IdentifierStoreReader( |
| 108 | + $this->designExceptionsMock, |
| 109 | + $defaultRequestMock, |
| 110 | + $this->configMock |
| 111 | + ); |
| 112 | + $newData = $this->model->getPageTagsWithStoreCacheTags($data); |
| 113 | + |
| 114 | + $this->assertArrayHasKey(StoreManager::PARAM_RUN_TYPE, $newData); |
| 115 | + $this->assertArrayHasKey(StoreManager::PARAM_RUN_CODE, $newData); |
| 116 | + $this->assertEquals($newData[StoreManager::PARAM_RUN_TYPE], 'store'); |
| 117 | + $this->assertEquals($newData[StoreManager::PARAM_RUN_CODE], 'default'); |
| 118 | + } |
| 119 | +} |
0 commit comments