LockerProcessTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Test\Unit\Asset;
  7. use Magento\Framework\Filesystem;
  8. use Magento\Framework\View\Asset\LockerProcess;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\Filesystem\Directory\WriteInterface;
  11. use Magento\Framework\App\State;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  13. /**
  14. * Class LockerProcessTest
  15. *
  16. * @see \Magento\Framework\View\Asset\LockerProcess
  17. */
  18. class LockerProcessTest extends \PHPUnit\Framework\TestCase
  19. {
  20. const LOCK_NAME = 'test-lock';
  21. /**
  22. * @var string
  23. */
  24. private $fileName;
  25. /**
  26. * @var LockerProcess
  27. */
  28. private $lockerProcess;
  29. /**
  30. * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $filesystemMock;
  33. /**
  34. * @var State|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $stateMock;
  37. /**
  38. * Set up
  39. */
  40. protected function setUp()
  41. {
  42. $this->fileName = DirectoryList::TMP . DIRECTORY_SEPARATOR . self::LOCK_NAME . LockerProcess::LOCK_EXTENSION;
  43. $this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->stateMock = $this->getMockBuilder(State::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->lockerProcess = (new ObjectManager($this))->getObject(
  50. LockerProcess::class,
  51. [
  52. 'filesystem' => $this->filesystemMock,
  53. 'state' => $this->stateMock,
  54. ]
  55. );
  56. }
  57. /**
  58. * Test for lockProcess method
  59. *
  60. * @param string $method
  61. *
  62. * @dataProvider dataProviderTestLockProcess
  63. */
  64. public function testLockProcess($method)
  65. {
  66. $this->stateMock->expects(self::once())->method('getMode')->willReturn(State::MODE_DEVELOPER);
  67. $this->filesystemMock->expects(self::once())
  68. ->method('getDirectoryWrite')
  69. ->with(DirectoryList::VAR_DIR)
  70. ->willReturn($this->$method());
  71. $this->lockerProcess->lockProcess(self::LOCK_NAME);
  72. }
  73. public function testNotLockProcessInProductionMode()
  74. {
  75. $this->stateMock->expects(self::once())->method('getMode')->willReturn(State::MODE_PRODUCTION);
  76. $this->filesystemMock->expects($this->never())->method('getDirectoryWrite');
  77. $this->lockerProcess->lockProcess(self::LOCK_NAME);
  78. }
  79. /**
  80. * Test for unlockProcess method
  81. */
  82. public function testUnlockProcess()
  83. {
  84. $this->stateMock->expects(self::exactly(2))->method('getMode')->willReturn(State::MODE_DEVELOPER);
  85. $this->filesystemMock->expects(self::once())
  86. ->method('getDirectoryWrite')
  87. ->with(DirectoryList::VAR_DIR)
  88. ->willReturn($this->getTmpDirectoryMockFalse(1));
  89. $this->lockerProcess->lockProcess(self::LOCK_NAME);
  90. $this->lockerProcess->unlockProcess();
  91. }
  92. public function testNotUnlockProcessInProductionMode()
  93. {
  94. $this->stateMock->expects(self::exactly(2))->method('getMode')->willReturn(State::MODE_PRODUCTION);
  95. $this->filesystemMock->expects(self::never())->method('getDirectoryWrite');
  96. $this->lockerProcess->lockProcess(self::LOCK_NAME);
  97. $this->lockerProcess->unlockProcess();
  98. }
  99. /**
  100. * @return array
  101. */
  102. public function dataProviderTestLockProcess()
  103. {
  104. return [
  105. ['method' => 'getTmpDirectoryMockTrue'],
  106. ['method' => 'getTmpDirectoryMockFalse']
  107. ];
  108. }
  109. /**
  110. * @return WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  111. */
  112. protected function getTmpDirectoryMockTrue()
  113. {
  114. $tmpDirectoryMock = $this->getTmpDirectoryMock();
  115. $tmpDirectoryMock->expects(self::atLeastOnce())
  116. ->method('isExist')
  117. ->with($this->fileName)
  118. ->willReturn(true);
  119. $tmpDirectoryMock->expects(self::atLeastOnce())
  120. ->method('readFile')
  121. ->with($this->fileName)
  122. ->willReturn(time() - 25);
  123. $tmpDirectoryMock->expects(self::once())
  124. ->method('writeFile')
  125. ->with($this->fileName, self::matchesRegularExpression('#\d+#'));
  126. return $tmpDirectoryMock;
  127. }
  128. /**
  129. * @param int $exactly
  130. * @return WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  131. */
  132. protected function getTmpDirectoryMockFalse($exactly = 0)
  133. {
  134. $tmpDirectoryMock = $this->getTmpDirectoryMock();
  135. $tmpDirectoryMock->expects(self::atLeastOnce())
  136. ->method('isExist')
  137. ->with($this->fileName)
  138. ->willReturn(false);
  139. $tmpDirectoryMock->expects(self::never())
  140. ->method('readFile');
  141. $tmpDirectoryMock->expects(self::exactly($exactly))
  142. ->method('delete')
  143. ->with($this->fileName);
  144. $tmpDirectoryMock->expects(self::once())
  145. ->method('writeFile')
  146. ->with($this->fileName, self::matchesRegularExpression('#\d+#'));
  147. return $tmpDirectoryMock;
  148. }
  149. /**
  150. * @return WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  151. */
  152. private function getTmpDirectoryMock()
  153. {
  154. $tmpDirectoryMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
  155. ->getMockForAbstractClass();
  156. return $tmpDirectoryMock;
  157. }
  158. }