Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LockerProcessTest.php
Go to the documentation of this file.
1 <?php
7 
14 
20 class LockerProcessTest extends \PHPUnit\Framework\TestCase
21 {
22  const LOCK_NAME = 'test-lock';
23 
27  private $fileName;
28 
32  private $lockerProcess;
33 
37  private $filesystemMock;
38 
42  private $stateMock;
43 
47  protected function setUp()
48  {
49  $this->fileName = DirectoryList::TMP . DIRECTORY_SEPARATOR . self::LOCK_NAME . LockerProcess::LOCK_EXTENSION;
50 
51  $this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54  $this->stateMock = $this->getMockBuilder(State::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57 
58  $this->lockerProcess = (new ObjectManager($this))->getObject(
59  LockerProcess::class,
60  [
61  'filesystem' => $this->filesystemMock,
62  'state' => $this->stateMock,
63  ]
64  );
65  }
66 
74  public function testLockProcess($method)
75  {
76  $this->stateMock->expects(self::once())->method('getMode')->willReturn(State::MODE_DEVELOPER);
77  $this->filesystemMock->expects(self::once())
78  ->method('getDirectoryWrite')
80  ->willReturn($this->$method());
81 
82  $this->lockerProcess->lockProcess(self::LOCK_NAME);
83  }
84 
86  {
87  $this->stateMock->expects(self::once())->method('getMode')->willReturn(State::MODE_PRODUCTION);
88  $this->filesystemMock->expects($this->never())->method('getDirectoryWrite');
89 
90  $this->lockerProcess->lockProcess(self::LOCK_NAME);
91  }
92 
96  public function testUnlockProcess()
97  {
98  $this->stateMock->expects(self::exactly(2))->method('getMode')->willReturn(State::MODE_DEVELOPER);
99  $this->filesystemMock->expects(self::once())
100  ->method('getDirectoryWrite')
101  ->with(DirectoryList::VAR_DIR)
102  ->willReturn($this->getTmpDirectoryMockFalse(1));
103 
104  $this->lockerProcess->lockProcess(self::LOCK_NAME);
105  $this->lockerProcess->unlockProcess();
106  }
107 
109  {
110  $this->stateMock->expects(self::exactly(2))->method('getMode')->willReturn(State::MODE_PRODUCTION);
111  $this->filesystemMock->expects(self::never())->method('getDirectoryWrite');
112 
113  $this->lockerProcess->lockProcess(self::LOCK_NAME);
114  $this->lockerProcess->unlockProcess();
115  }
116 
120  public function dataProviderTestLockProcess()
121  {
122  return [
123  ['method' => 'getTmpDirectoryMockTrue'],
124  ['method' => 'getTmpDirectoryMockFalse']
125  ];
126  }
127 
131  protected function getTmpDirectoryMockTrue()
132  {
133  $tmpDirectoryMock = $this->getTmpDirectoryMock();
134 
135  $tmpDirectoryMock->expects(self::atLeastOnce())
136  ->method('isExist')
137  ->with($this->fileName)
138  ->willReturn(true);
139 
140  $tmpDirectoryMock->expects(self::atLeastOnce())
141  ->method('readFile')
142  ->with($this->fileName)
143  ->willReturn(time() - 25);
144 
145  $tmpDirectoryMock->expects(self::once())
146  ->method('writeFile')
147  ->with($this->fileName, self::matchesRegularExpression('#\d+#'));
148 
149  return $tmpDirectoryMock;
150  }
151 
156  protected function getTmpDirectoryMockFalse($exactly = 0)
157  {
158  $tmpDirectoryMock = $this->getTmpDirectoryMock();
159 
160  $tmpDirectoryMock->expects(self::atLeastOnce())
161  ->method('isExist')
162  ->with($this->fileName)
163  ->willReturn(false);
164 
165  $tmpDirectoryMock->expects(self::never())
166  ->method('readFile');
167 
168  $tmpDirectoryMock->expects(self::exactly($exactly))
169  ->method('delete')
170  ->with($this->fileName);
171 
172  $tmpDirectoryMock->expects(self::once())
173  ->method('writeFile')
174  ->with($this->fileName, self::matchesRegularExpression('#\d+#'));
175 
176  return $tmpDirectoryMock;
177  }
178 
182  private function getTmpDirectoryMock()
183  {
184  $tmpDirectoryMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
185  ->getMockForAbstractClass();
186 
187  return $tmpDirectoryMock;
188  }
189 }
$method
Definition: info.phtml:13