VersionTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\PageCache;
  7. use \Magento\Framework\App\PageCache\Version;
  8. use Magento\TestFramework\ObjectManager;
  9. class VersionTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * Version instance
  13. *
  14. * @var Version
  15. */
  16. protected $version;
  17. /**
  18. * Cookie manager mock
  19. *
  20. * @var \Magento\Framework\Stdlib\CookieManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $cookieManagerMock;
  23. /**
  24. * Cookie manager mock
  25. *
  26. * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $cookieMetadataFactoryMock;
  29. /**
  30. * Request mock
  31. *
  32. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $requestMock;
  35. /**
  36. * Create cookie and request mock, version instance
  37. */
  38. protected function setUp()
  39. {
  40. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  41. $this->cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
  42. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  43. ->disableOriginalConstructor()->getMock();
  44. $this->cookieMetadataFactoryMock = $this->getMockBuilder(
  45. \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
  46. )
  47. ->disableOriginalConstructor()->getMock();
  48. $this->version = $objectManager->getObject(
  49. \Magento\Framework\App\PageCache\Version::class,
  50. [
  51. 'cookieManager' => $this->cookieManagerMock,
  52. 'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
  53. 'request' => $this->requestMock
  54. ]
  55. );
  56. }
  57. /**
  58. * Handle private content version cookie
  59. * Set cookie if it is not set.
  60. * Increment version on post requests.
  61. * In all other cases do nothing.
  62. */
  63. /**
  64. * @dataProvider processProvider
  65. * @param bool $isPost
  66. */
  67. public function testProcess($isPost)
  68. {
  69. $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue($isPost));
  70. if ($isPost) {
  71. $publicCookieMetadataMock = $this->createMock(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class);
  72. $publicCookieMetadataMock->expects($this->once())
  73. ->method('setPath')
  74. ->with('/')
  75. ->will($this->returnSelf());
  76. $publicCookieMetadataMock->expects($this->once())
  77. ->method('setDuration')
  78. ->with(Version::COOKIE_PERIOD)
  79. ->will($this->returnSelf());
  80. $publicCookieMetadataMock->expects($this->once())
  81. ->method('setSecure')
  82. ->with(false)
  83. ->will($this->returnSelf());
  84. $publicCookieMetadataMock->expects($this->once())
  85. ->method('setHttpOnly')
  86. ->with(false)
  87. ->will($this->returnSelf());
  88. $this->cookieMetadataFactoryMock->expects($this->once())
  89. ->method('createPublicCookieMetadata')
  90. ->with()
  91. ->will(
  92. $this->returnValue($publicCookieMetadataMock)
  93. );
  94. $this->cookieManagerMock->expects($this->once())
  95. ->method('setPublicCookie');
  96. }
  97. $this->version->process();
  98. }
  99. /**
  100. * Data provider for testProcess
  101. *
  102. * @return array
  103. */
  104. public function processProvider()
  105. {
  106. return [
  107. "post" => [true],
  108. "notPost" => [false]
  109. ];
  110. }
  111. }