|
11 | 11 | class BuiltinPluginTest extends \PHPUnit_Framework_TestCase
|
12 | 12 | {
|
13 | 13 | /**
|
14 |
| - * @param bool $usePlugin |
15 |
| - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $getHeaderCount |
16 |
| - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCacheControlHeaderCount |
17 |
| - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCacheDebugHeaderCount |
18 |
| - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $getModeCount |
19 |
| - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $processCount |
20 |
| - * @dataProvider dataProvider |
| 14 | + * @var \Magento\PageCache\Model\Controller\Result\BuiltinPlugin |
21 | 15 | */
|
22 |
| - public function testAroundResult( |
23 |
| - $usePlugin, $getHeaderCount, $setCacheControlHeaderCount, $setCacheDebugHeaderCount, $getModeCount, |
24 |
| - $processCount |
25 |
| - ) { |
26 |
| - $cacheControl = 'test'; |
27 |
| - |
28 |
| - $header = $this->getMockBuilder('Zend\Http\Header\HeaderInterface') |
29 |
| - ->getMockForAbstractClass(); |
30 |
| - $header->expects($this->any())->method('getFieldValue') |
31 |
| - ->willReturn($cacheControl); |
32 |
| - |
33 |
| - $response = $this->getMock('Magento\Framework\App\Response\Http', [], [], '', false); |
34 |
| - $response->expects($getHeaderCount) |
35 |
| - ->method('getHeader') |
36 |
| - ->with('Cache-Control') |
37 |
| - ->willReturn($header); |
38 |
| - $response->expects($setCacheControlHeaderCount)->method('setHeader') |
39 |
| - ->with('X-Magento-Cache-Control', $cacheControl); |
40 |
| - $response->expects($setCacheDebugHeaderCount)->method('setHeader') |
41 |
| - ->with('X-Magento-Cache-Debug', 'MISS', true); |
42 |
| - |
43 |
| - /** @var \Magento\Framework\Controller\ResultInterface $result */ |
| 16 | + protected $plugin; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Magento\Framework\Controller\ResultInterface |
| 20 | + */ |
| 21 | + protected $subject; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Closure |
| 25 | + */ |
| 26 | + protected $closure; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + protected $response; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + protected $registry; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 41 | + protected $state; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Zend\Http\Header\HeaderInterface|\PHPUnit_Framework_MockObject_MockObject |
| 45 | + */ |
| 46 | + protected $header; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var \Magento\Framework\App\PageCache\Kernel|\PHPUnit_Framework_MockObject_MockObject |
| 50 | + */ |
| 51 | + protected $kernel; |
| 52 | + |
| 53 | + protected function setUp() |
| 54 | + { |
44 | 55 | $result = $this->getMock('Magento\Framework\Controller\ResultInterface', [], [], '', false);
|
45 |
| - $closure = function () use ($result) { |
| 56 | + $this->closure = function() use ($result) { |
46 | 57 | return $result;
|
47 | 58 | };
|
48 | 59 |
|
49 |
| - /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject $registry */ |
50 |
| - $registry = $this->getMock('Magento\Framework\Registry', [], [], '', false); |
51 |
| - $registry->expects($this->once())->method('registry')->with('use_page_cache_plugin') |
52 |
| - ->will($this->returnValue($usePlugin)); |
53 |
| - |
54 |
| - /** @var \Magento\PageCache\Model\Config|\PHPUnit_Framework_MockObject_MockObject $config */ |
55 |
| - $config = $this->getMock('Magento\PageCache\Model\Config', [], [], '', false); |
56 |
| - $config->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); |
57 |
| - $config->expects($this->once())->method('getType') |
58 |
| - ->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN)); |
| 60 | + $this->header = $this->getMock('Zend\Http\Header\HeaderInterface', [], [], '', false); |
| 61 | + $this->subject = $this->getMock('Magento\Framework\Controller\ResultInterface', [], [], '', false); |
| 62 | + $this->response = $this->getMock( |
| 63 | + 'Magento\Framework\App\Response\Http', |
| 64 | + ['getHeader', 'clearHeader', 'setHeader'], |
| 65 | + [], |
| 66 | + '', |
| 67 | + false |
| 68 | + ); |
| 69 | + $this->response->expects($this->any())->method('getHeader')->willReturnMap( |
| 70 | + [ |
| 71 | + ['X-Magento-Tags', $this->header], |
| 72 | + ['Cache-Control', $this->header] |
| 73 | + ] |
| 74 | + ); |
59 | 75 |
|
60 |
| - /** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject $state */ |
61 |
| - $state = $this->getMock('Magento\Framework\App\State', [], [], '', false); |
62 |
| - $state->expects($getModeCount)->method('getMode') |
63 |
| - ->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER)); |
| 76 | + $this->registry = $this->getMock('Magento\Framework\Registry', [], [], '', false); |
64 | 77 |
|
65 |
| - $kernel = $this->getMock('Magento\Framework\App\PageCache\Kernel', [], [], '', false); |
66 |
| - $kernel->expects($processCount)->method('process')->with($response); |
| 78 | + $config = $this->getMock('Magento\PageCache\Model\Config', ['isEnabled', 'getType'], [], '', false); |
| 79 | + $config->expects($this->any())->method('isEnabled')->willReturn(true); |
| 80 | + $config->expects($this->any())->method('getType')->willReturn(\Magento\PageCache\Model\Config::BUILT_IN); |
67 | 81 |
|
68 |
| - $subject = $this->getMock('Magento\Framework\Controller\ResultInterface', [], [], '', false); |
| 82 | + $this->kernel = $this->getMock('Magento\Framework\App\PageCache\Kernel', [], [], '', false); |
69 | 83 |
|
70 |
| - /** @var \Magento\PageCache\Model\Controller\Result\BuiltinPlugin $plugin */ |
71 |
| - $plugin = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( |
| 84 | + $this->state = $this->getMock('Magento\Framework\App\State', [], [], '', false); |
| 85 | + $this->plugin = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( |
72 | 86 | 'Magento\PageCache\Model\Controller\Result\BuiltinPlugin',
|
73 | 87 | [
|
74 |
| - 'registry' => $registry, |
| 88 | + 'registry' => $this->registry, |
75 | 89 | 'config' => $config,
|
76 |
| - 'kernel' => $kernel, |
77 |
| - 'state' => $state |
| 90 | + 'kernel' => $this->kernel, |
| 91 | + 'state' => $this->state |
78 | 92 | ]
|
79 | 93 | );
|
80 |
| - $this->assertSame($result, $plugin->aroundRenderResult($subject, $closure, $response)); |
81 | 94 | }
|
82 | 95 |
|
83 |
| - /** |
84 |
| - * @return array |
85 |
| - */ |
86 |
| - public function dataProvider() |
| 96 | + public function testAroundResultWithoutPlugin() |
87 | 97 | {
|
88 |
| - return [ |
89 |
| - [true, $this->once(), $this->at(1), $this->at(2), $this->once(), $this->once()], |
90 |
| - [false, $this->never(), $this->never(), $this->never(), $this->never(), $this->never()] |
91 |
| - ]; |
| 98 | + $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(false); |
| 99 | + $this->kernel->expects($this->never())->method('process')->with($this->response); |
| 100 | + $this->assertSame( |
| 101 | + call_user_func($this->closure), |
| 102 | + $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response) |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + public function testAroundResultWithPlugin() |
| 107 | + { |
| 108 | + $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(true); |
| 109 | + $this->state->expects($this->once())->method('getMode')->willReturn(null); |
| 110 | + $this->header->expects($this->any())->method('getFieldValue')->willReturn('tag,tag'); |
| 111 | + $this->response->expects($this->once())->method('clearHeader')->with('X-Magento-Tags'); |
| 112 | + $this->response->expects($this->once())->method('setHeader')->with( |
| 113 | + 'X-Magento-Tags', |
| 114 | + 'tag,' . \Magento\PageCache\Model\Cache\Type::CACHE_TAG |
| 115 | + ); |
| 116 | + $this->kernel->expects($this->once())->method('process')->with($this->response); |
| 117 | + $result = call_user_func($this->closure); |
| 118 | + $this->assertSame($result, $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response)); |
| 119 | + } |
| 120 | + |
| 121 | + public function testAroundResultWithPluginDeveloperMode() |
| 122 | + { |
| 123 | + $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(true); |
| 124 | + $this->state->expects($this->once())->method('getMode') |
| 125 | + ->willReturn(\Magento\Framework\App\State::MODE_DEVELOPER); |
| 126 | + |
| 127 | + $this->header->expects($this->any())->method('getFieldValue')->willReturnOnConsecutiveCalls('test', 'tag,tag2'); |
| 128 | + |
| 129 | + $this->response->expects($this->any())->method('setHeader')->withConsecutive( |
| 130 | + ['X-Magento-Cache-Control', 'test'], |
| 131 | + ['X-Magento-Cache-Debug', 'MISS', true], |
| 132 | + ['X-Magento-Tags', 'tag,tag2,' . \Magento\PageCache\Model\Cache\Type::CACHE_TAG] |
| 133 | + ); |
| 134 | + |
| 135 | + $this->response->expects($this->once())->method('clearHeader')->with('X-Magento-Tags'); |
| 136 | + $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin') |
| 137 | + ->will($this->returnValue(true)); |
| 138 | + $this->kernel->expects($this->once())->method('process')->with($this->response); |
| 139 | + |
| 140 | + $result = call_user_func($this->closure); |
| 141 | + $this->assertSame($result, $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response)); |
92 | 142 | }
|
93 | 143 | }
|
0 commit comments