Skip to content

Commit 7333144

Browse files
committed
MAGE-1245 Add integration test for caching with plugin spy
1 parent 64e1d1b commit 7333144

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration\Category;
4+
5+
use Magento\Framework\App\RequestInterface;
6+
use Magento\Framework\App\Response\Http as ResponseHttp;
7+
use Magento\Framework\App\ResponseInterface;
8+
use Magento\Framework\Data\Form\FormKey;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
class CategoryCacheTest extends \Magento\TestFramework\TestCase\AbstractController
12+
{
13+
protected $cacheManager;
14+
15+
protected $url = '/catalog/category/view/id/';
16+
17+
public static function getCategoryProvider(): array
18+
{
19+
return [
20+
['categoryId' => 20, 'name' => 'Ladies'],
21+
['categoryId' => 21, 'name' => 'Ladies Tops'],
22+
];
23+
}
24+
25+
protected function setUp(): void
26+
{
27+
parent::setUp();
28+
$this->cacheManager = $this->_objectManager->get(\Magento\Framework\App\Cache\Manager::class);
29+
}
30+
31+
/**
32+
* @dataProvider getCategoryProvider
33+
* @depends testFullPageCacheAvailable
34+
* @magentoConfigFixture default/system/full_page_cache/caching_application 1
35+
* @magentoCache full_page enabled
36+
* @param int $categoryId
37+
* @param string $name
38+
* @return void
39+
*/
40+
public function testCategoryPlpMiss(int $categoryId, string $name): void
41+
{
42+
$this->cacheManager->clean(['full_page']);
43+
$this->dispatch($this->url . $categoryId);
44+
$response = $this->getResponse();
45+
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
46+
$this->assertEquals(
47+
'MISS',
48+
$response->getHeader('X-Magento-Cache-Debug')->getFieldValue(),
49+
"expected MISS on category {$name} id {$categoryId}"
50+
);
51+
}
52+
53+
protected function resetResponse(): void
54+
{
55+
$this->_objectManager->removeSharedInstance(ResponseInterface::class);
56+
$this->_response = null;
57+
}
58+
59+
/**
60+
* The response object is modified differently by the BuiltinPlugin which prevents anything useful
61+
* being returned by AbstractController::getResponse
62+
*
63+
* Therefore we apply a "spy" on the plugin via a mock to ensure that the proper header is added
64+
* when the cache has been warmed (by the first MISS)
65+
*
66+
* @dataProvider getCategoryProvider
67+
* @depends testCategoryPlpMiss
68+
* @magentoConfigFixture default/system/full_page_cache/caching_application 1
69+
* @magentoCache full_page enabled
70+
* @param int $categoryId
71+
* @param string $name
72+
* @return void
73+
*/
74+
public function testCategoryPlpHit(int $categoryId, string $name): void
75+
{
76+
$mockedPluginClass = \Magento\PageCache\Model\App\FrontController\BuiltinPlugin::class;
77+
$mockedPluginMethod = 'addDebugHeader';
78+
$cachePluginMock = $this->getMockBuilder($mockedPluginClass)
79+
->setConstructorArgs([
80+
$this->_objectManager->get(\Magento\PageCache\Model\Config::class),
81+
$this->_objectManager->get(\Magento\Framework\App\PageCache\Version::class),
82+
$this->_objectManager->get(\Magento\Framework\App\PageCache\Kernel::class),
83+
$this->_objectManager->get(\Magento\Framework\App\State::class)
84+
])
85+
->onlyMethods([$mockedPluginMethod])
86+
->getMock();
87+
$cachePluginMock
88+
->expects($this->once())
89+
->method($mockedPluginMethod)
90+
->with(
91+
$this->isInstanceOf(ResponseHttp::class),
92+
$this->equalTo("X-Magento-Cache-Debug"),
93+
$this->equalTo("HIT"),
94+
$this->isType('boolean')
95+
)
96+
->willReturnCallback(
97+
function (ResponseHttp $response, $name, $value, $replace)
98+
use ($mockedPluginClass, $mockedPluginMethod, $cachePluginMock)
99+
{
100+
$originalMethod = new \ReflectionMethod($mockedPluginClass, $mockedPluginMethod);
101+
return $originalMethod->invoke($cachePluginMock, $response, $name, $value, $replace);
102+
}
103+
);
104+
$this->_objectManager->addSharedInstance(
105+
$cachePluginMock,
106+
$mockedPluginClass
107+
);
108+
109+
$this->dispatch("catalog/category/view/id/{$categoryId}");
110+
$response = $this->getResponse();
111+
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
112+
}
113+
114+
public function xtestCategoryPlpHit(int $categoryId, string $name): void
115+
{
116+
$this->dispatch("catalog/category/view/id/{$categoryId}");
117+
$response = $this->getResponse();
118+
$this->assertEquals(
119+
'HIT',
120+
$response->getHeader('X-Magento-Cache-Debug')->getFieldValue(),
121+
"expected HIT on category {$name} id {$categoryId}"
122+
);
123+
}
124+
125+
public function testFullPageCacheAvailable(): void
126+
{
127+
$types = $this->cacheManager->getAvailableTypes();
128+
$this->assertContains('full_page', $types);
129+
}
130+
131+
protected function dispatchNew($uri)
132+
{
133+
$request = $this->_objectManager->get(\Magento\Framework\App\RequestInterface::class);
134+
135+
$request->setDispatched(false);
136+
$request->setRequestUri($uri);
137+
$this->_getBootstrap()->runApp();
138+
}
139+
140+
}

0 commit comments

Comments
 (0)