/vendor/magento/module-page-cache/Test/Unit/Model/Controller/Result/BuiltinPluginTest.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 143 lines · 91 code · 23 blank · 29 comment · 0 complexity · f438729fd47f254d985f8193e597996e MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\PageCache\Test\Unit\Model\Controller\Result;
  8. class BuiltinPluginTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var \Magento\PageCache\Model\Controller\Result\BuiltinPlugin
  12. */
  13. protected $plugin;
  14. /**
  15. * @var \Magento\Framework\Controller\ResultInterface
  16. */
  17. protected $subject;
  18. /**
  19. * @var \Closure
  20. */
  21. protected $closure;
  22. /**
  23. * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $response;
  26. /**
  27. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $registry;
  30. /**
  31. * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $state;
  34. /**
  35. * @var \Zend\Http\Header\HeaderInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $header;
  38. /**
  39. * @var \Magento\Framework\App\PageCache\Kernel|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $kernel;
  42. protected function setUp()
  43. {
  44. $result = $this->getMock('Magento\Framework\Controller\ResultInterface', [], [], '', false);
  45. $this->closure = function() use ($result) {
  46. return $result;
  47. };
  48. $this->header = $this->getMock('Zend\Http\Header\HeaderInterface', [], [], '', false);
  49. $this->subject = $this->getMock('Magento\Framework\Controller\ResultInterface', [], [], '', false);
  50. $this->response = $this->getMock(
  51. 'Magento\Framework\App\Response\Http',
  52. ['getHeader', 'clearHeader', 'setHeader'],
  53. [],
  54. '',
  55. false
  56. );
  57. $this->response->expects($this->any())->method('getHeader')->willReturnMap(
  58. [
  59. ['X-Magento-Tags', $this->header],
  60. ['Cache-Control', $this->header]
  61. ]
  62. );
  63. $this->registry = $this->getMock('Magento\Framework\Registry', [], [], '', false);
  64. $config = $this->getMock('Magento\PageCache\Model\Config', ['isEnabled', 'getType'], [], '', false);
  65. $config->expects($this->any())->method('isEnabled')->willReturn(true);
  66. $config->expects($this->any())->method('getType')->willReturn(\Magento\PageCache\Model\Config::BUILT_IN);
  67. $this->kernel = $this->getMock('Magento\Framework\App\PageCache\Kernel', [], [], '', false);
  68. $this->state = $this->getMock('Magento\Framework\App\State', [], [], '', false);
  69. $this->plugin = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
  70. 'Magento\PageCache\Model\Controller\Result\BuiltinPlugin',
  71. [
  72. 'registry' => $this->registry,
  73. 'config' => $config,
  74. 'kernel' => $this->kernel,
  75. 'state' => $this->state
  76. ]
  77. );
  78. }
  79. public function testAroundResultWithoutPlugin()
  80. {
  81. $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(false);
  82. $this->kernel->expects($this->never())->method('process')->with($this->response);
  83. $this->assertSame(
  84. call_user_func($this->closure),
  85. $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response)
  86. );
  87. }
  88. public function testAroundResultWithPlugin()
  89. {
  90. $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(true);
  91. $this->state->expects($this->once())->method('getMode')->willReturn(null);
  92. $this->header->expects($this->any())->method('getFieldValue')->willReturn('tag,tag');
  93. $this->response->expects($this->once())->method('clearHeader')->with('X-Magento-Tags');
  94. $this->response->expects($this->once())->method('setHeader')->with(
  95. 'X-Magento-Tags',
  96. 'tag,' . \Magento\PageCache\Model\Cache\Type::CACHE_TAG
  97. );
  98. $this->kernel->expects($this->once())->method('process')->with($this->response);
  99. $result = call_user_func($this->closure);
  100. $this->assertSame($result, $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response));
  101. }
  102. public function testAroundResultWithPluginDeveloperMode()
  103. {
  104. $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(true);
  105. $this->state->expects($this->once())->method('getMode')
  106. ->willReturn(\Magento\Framework\App\State::MODE_DEVELOPER);
  107. $this->header->expects($this->any())->method('getFieldValue')->willReturnOnConsecutiveCalls('test', 'tag,tag2');
  108. $this->response->expects($this->any())->method('setHeader')->withConsecutive(
  109. ['X-Magento-Cache-Control', 'test'],
  110. ['X-Magento-Cache-Debug', 'MISS', true],
  111. ['X-Magento-Tags', 'tag,tag2,' . \Magento\PageCache\Model\Cache\Type::CACHE_TAG]
  112. );
  113. $this->response->expects($this->once())->method('clearHeader')->with('X-Magento-Tags');
  114. $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')
  115. ->will($this->returnValue(true));
  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. }