Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BuiltinPluginTest.php
Go to the documentation of this file.
1 <?php
7 
16 use Zend\Http\Header\HeaderInterface as HttpHeaderInterface;
17 use Magento\PageCache\Model\Cache\Type as CacheType;
18 
22 class BuiltinPluginTest extends \PHPUnit\Framework\TestCase
23 {
27  private $plugin;
28 
32  private $objectManagerHelper;
33 
37  private $configMock;
38 
42  private $kernelMock;
43 
47  private $stateMock;
48 
52  private $registryMock;
53 
57  private $resultMock;
58 
62  private $responseMock;
63 
67  private $httpHeaderMock;
68 
69  protected function setUp()
70  {
71  $this->configMock = $this->getMockBuilder(Config::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74  $this->kernelMock = $this->getMockBuilder(Kernel::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77  $this->stateMock = $this->getMockBuilder(AppState::class)
78  ->disableOriginalConstructor()
79  ->getMock();
80  $this->registryMock = $this->getMockBuilder(Registry::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83  $this->resultMock = $this->getMockBuilder(ResultInterface::class)
84  ->getMockForAbstractClass();
85  $this->responseMock = $this->getMockBuilder(ResponseHttp::class)
86  ->disableOriginalConstructor()
87  ->getMock();
88  $this->httpHeaderMock = $this->getMockBuilder(HttpHeaderInterface::class)
89  ->getMockForAbstractClass();
90 
91  $this->responseMock->expects(static::any())
92  ->method('getHeader')
93  ->willReturnMap(
94  [
95  ['X-Magento-Tags', $this->httpHeaderMock],
96  ['Cache-Control', $this->httpHeaderMock]
97  ]
98  );
99  $this->configMock->expects(static::any())
100  ->method('isEnabled')
101  ->willReturn(true);
102  $this->configMock->expects(static::any())
103  ->method('getType')
104  ->willReturn(Config::BUILT_IN);
105 
106  $this->objectManagerHelper = new ObjectManagerHelper($this);
107  $this->plugin = $this->objectManagerHelper->getObject(
108  BuiltinPlugin::class,
109  [
110  'registry' => $this->registryMock,
111  'config' => $this->configMock,
112  'kernel' => $this->kernelMock,
113  'state' => $this->stateMock
114  ]
115  );
116  }
117 
119  {
120  $this->registryMock->expects(static::once())
121  ->method('registry')
122  ->with('use_page_cache_plugin')
123  ->willReturn(false);
124  $this->kernelMock->expects(static::never())
125  ->method('process')
126  ->with($this->responseMock);
127 
128  $this->assertSame(
129  $this->resultMock,
130  $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock)
131  );
132  }
133 
134  public function testAfterResultWithPlugin()
135  {
136  $this->registryMock->expects(static::once())
137  ->method('registry')
138  ->with('use_page_cache_plugin')
139  ->willReturn(true);
140  $this->stateMock->expects(static::once())
141  ->method('getMode')
142  ->willReturn(null);
143  $this->httpHeaderMock->expects(static::any())
144  ->method('getFieldValue')
145  ->willReturn('tag,tag');
146  $this->responseMock->expects(static::once())
147  ->method('clearHeader')
148  ->with('X-Magento-Tags');
149  $this->responseMock->expects(static::once())
150  ->method('setHeader')
151  ->with('X-Magento-Tags', 'tag,' . CacheType::CACHE_TAG);
152  $this->kernelMock->expects(static::once())
153  ->method('process')
154  ->with($this->responseMock);
155 
156  $this->assertSame(
157  $this->resultMock,
158  $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock)
159  );
160  }
161 
163  {
164  $this->registryMock->expects(static::once())
165  ->method('registry')
166  ->with('use_page_cache_plugin')
167  ->willReturn(true);
168  $this->stateMock->expects(static::once())
169  ->method('getMode')
170  ->willReturn(AppState::MODE_DEVELOPER);
171  $this->httpHeaderMock->expects(static::any())
172  ->method('getFieldValue')
173  ->willReturnOnConsecutiveCalls('test', 'tag,tag2');
174  $this->responseMock->expects(static::any())
175  ->method('setHeader')
176  ->withConsecutive(
177  ['X-Magento-Cache-Control', 'test'],
178  ['X-Magento-Cache-Debug', 'MISS', true],
179  ['X-Magento-Tags', 'tag,tag2,' . CacheType::CACHE_TAG]
180  );
181  $this->responseMock->expects(static::once())
182  ->method('clearHeader')
183  ->with('X-Magento-Tags');
184  $this->registryMock->expects(static::once())
185  ->method('registry')
186  ->with('use_page_cache_plugin')
187  ->willReturn(true);
188  $this->kernelMock->expects(static::once())
189  ->method('process')
190  ->with($this->responseMock);
191 
192  $this->assertSame(
193  $this->resultMock,
194  $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock)
195  );
196  }
197 }