1010use Phake ;
1111use PHPUnit \Framework \Attributes \Group ;
1212use PHPUnit \Framework \TestCase ;
13- use Recruiter \Clock ;
13+ use Recruiter \Clock \ProgressiveClock ;
14+ use Symfony \Component \Clock \ClockInterface ;
1415use Symfony \Component \Process \Process ;
1516
1617class MongoLockTest extends TestCase
1718{
1819 use Eris \TestTrait;
1920
2021 private MongoDB \Collection $ lockCollection ;
21- private Clock &Phake \IMock $ clock ;
22- private array $ slept ;
23- private \Closure $ sleep ;
22+ private (ClockInterface &Phake \IMock )|ProgressiveClock $ clock ;
2423 private int $ iteration ;
2524
2625 protected function setUp (): void
2726 {
2827 $ uri = getenv ('MONGODB_URI ' ) ?: null ;
2928 $ this ->lockCollection = new MongoDB \Client ($ uri )->selectCollection ('concurrency-test ' , 'lock ' );
30- $ this ->clock = \Phake::mock (Clock::class);
31-
32- $ this ->slept = [];
33- $ this ->sleep = function ($ amount ): void {
34- $ this ->slept [] = $ amount ;
35- };
29+ $ this ->clock = \Phake::mock (ClockInterface::class);
3630 }
3731
3832 protected function tearDown (): void
@@ -78,9 +72,9 @@ public function testAnAlreadyAcquiredLockCannotBeAcquiredAgainEvenWithRefreshMet
7872
7973 public function testAnAlreadyAcquiredLockCanExpireSoThatItCanBeAcquiredAgain ()
8074 {
81- \Phake::when ($ this ->clock )->current ()
82- ->thenReturn (new \DateTime ('2014-01-01T10:00:00Z ' ))
83- ->thenReturn (new \DateTime ('2014-01-01T11:00:01Z ' ))
75+ \Phake::when ($ this ->clock )->now ()
76+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T10:00:00Z ' ))
77+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T11:00:01Z ' ))
8478 ;
8579 $ first = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
8680 $ first ->acquire (3600 );
@@ -139,8 +133,8 @@ public function testALockCanBeForcedToBeReleasedIfYouReallyKnowWhatYouReDoing()
139133
140134 public function testALockCanBeShownEvenByOtherProcessesWorkingOnTheSameProgram ()
141135 {
142- \Phake::when ($ this ->clock )->current ()
143- ->thenReturn (new \DateTime ('2014-01-01T00:00:00Z ' ))
136+ \Phake::when ($ this ->clock )->now ()
137+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:00Z ' ))
144138 ;
145139 $ first = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
146140 $ first ->acquire (3600 );
@@ -159,35 +153,29 @@ public function testALockCanBeShownEvenByOtherProcessesWorkingOnTheSameProgram()
159153
160154 public function testALockCanBeWaitedOnUntilItsDisappearance ()
161155 {
162- $ allCalls = \Phake::when ($ this ->clock )->current ()
163- ->thenReturn (new \DateTime ('2014-01-01T00:00:00Z ' ))
164- ->thenReturn (new \DateTime ('2014-01-01T00:00:00Z ' ))
165- ->thenReturn (new \DateTime ('2014-01-01T00:00:00Z ' ))
166- ->thenReturn (new \DateTime ('2014-01-01T00:00:30Z ' ))
167- ->thenReturn (new \DateTime ('2014-01-01T00:01:00Z ' ))
156+ $ allCalls = \Phake::when ($ this ->clock )->now ()
157+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:00Z ' ))
158+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:00Z ' ))
159+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:00Z ' ))
160+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:30Z ' ))
161+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:01:00Z ' ))
168162 ;
169163 $ first = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
170164 $ first ->acquire (45 );
171165
172- $ second = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock , $ this -> sleep );
166+ $ second = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
173167 $ second ->wait ($ polling = 30 );
174- $ this ->assertEquals ([ 30 , 30 ], $ this -> slept );
168+ \Phake:: verify ( $ this ->clock , \Phake:: times ( 2 ))-> sleep ( 30 );
175169 }
176170
177171 public function testALockShouldNotBeWaitedUponForever ()
178172 {
179- $ allCalls = \Phake::when ($ this ->clock )->current ()
180- ->thenReturn (new \DateTime ('2014-01-01T00:00:00Z ' ))
181- ->thenReturn (new \DateTime ('2014-01-01T00:00:00Z ' ))
182- ->thenReturn (new \DateTime ('2014-01-01T00:00:30Z ' ))
183- ->thenReturn (new \DateTime ('2014-01-01T00:00:50Z ' ))
184- ->thenReturn (new \DateTime ('2014-01-01T00:01:01Z ' ))
185- ->thenThrow (new \LogicException ('Should not call anymore ' ))
186- ;
173+ $ this ->clock = new ProgressiveClock (new \DateTimeImmutable ('2014-01-01T00:00:00Z ' ), \DateInterval::createFromDateString ('500 milliseconds ' ));
174+
187175 $ first = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
188176 $ first ->acquire (3600 );
189177
190- $ second = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock , $ this -> sleep );
178+ $ second = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
191179 try {
192180 $ second ->wait ($ polling = 30 , $ maximumInterval = 60 );
193181 $ this ->fail ('Should fail after 60 seconds ' );
@@ -201,28 +189,28 @@ public function testALockShouldNotBeWaitedUponForever()
201189
202190 public function testALockWaitedUponCanBeImmediatelyReacquired ()
203191 {
204- $ allCalls = \Phake::when ($ this ->clock )->current ()
205- ->thenReturn (new \DateTime ('2014-01-01T00:00:00Z ' ))
206- ->thenReturn (new \DateTime ('2014-01-01T00:00:30Z ' ))
207- ->thenReturn (new \DateTime ('2014-01-01T00:00:30Z ' ))
208- ->thenReturn (new \DateTime ('2014-01-01T00:00:30Z ' ))
209- ->thenReturn (new \DateTime ('2014-01-01T00:00:31Z ' ))
210- ->thenReturn (new \DateTime ('2014-01-01T00:00:31Z ' ))
192+ $ allCalls = \Phake::when ($ this ->clock )->now ()
193+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:00Z ' ))
194+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:30Z ' ))
195+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:30Z ' ))
196+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:30Z ' ))
197+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:31Z ' ))
198+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:31Z ' ))
211199 ;
212200 $ first = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
213201 $ first ->acquire (30 );
214202
215- $ second = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock , $ this -> sleep );
203+ $ second = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
216204 $ second ->wait ($ polling = 1 );
217205 $ second ->acquire ();
218206 $ this ->expectNotToPerformAssertions ();
219207 }
220208
221209 public function testAnAlreadyAcquiredLockCanBeRefreshed ()
222210 {
223- \Phake::when ($ this ->clock )->current ()
224- ->thenReturn (new \DateTime ('2014-01-01T00:00:00Z ' ))
225- ->thenReturn (new \DateTime ('2014-01-01T00:10:00Z ' ))
211+ \Phake::when ($ this ->clock )->now ()
212+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:00Z ' ))
213+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:10:00Z ' ))
226214 ;
227215
228216 $ first = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
@@ -244,9 +232,9 @@ public function testAnAlreadyAcquiredLockCanBeRefreshed()
244232
245233 public function testAnExpiredLockCannotBeRefreshed ()
246234 {
247- \Phake::when ($ this ->clock )->current ()
248- ->thenReturn (new \DateTime ('2014-01-01T00:00:00Z ' ))
249- ->thenReturn (new \DateTime ('2014-01-01T02:00:00Z ' ))
235+ \Phake::when ($ this ->clock )->now ()
236+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T00:00:00Z ' ))
237+ ->thenReturn (new \DateTimeImmutable ('2014-01-01T02:00:00Z ' ))
250238 ;
251239
252240 $ first = new MongoLock ($ this ->lockCollection , 'windows_defrag ' , 'ws-a-25:42 ' , $ this ->clock );
@@ -261,7 +249,7 @@ public function testAnExpiredLockCannotBeRefreshed()
261249
262250 private function givenTimeIsFixed ()
263251 {
264- \Phake::when ($ this ->clock )->current ()->thenReturn (new \DateTime ('2014-01-01 ' ));
252+ \Phake::when ($ this ->clock )->now ()->thenReturn (new \DateTimeImmutable ('2014-01-01 ' ));
265253 }
266254
267255 #[Group('long ' )]
0 commit comments