1616use Illuminate \Contracts \Events \Dispatcher ;
1717use Illuminate \Contracts \Session \Session ;
1818use Illuminate \Cookie \CookieJar ;
19+ use Illuminate \Support \Timebox ;
1920use Mockery as m ;
2021use PHPUnit \Framework \TestCase ;
2122use Symfony \Component \HttpFoundation \Cookie ;
@@ -94,6 +95,10 @@ public function testAttemptCallsRetrieveByCredentials()
9495 {
9596 $ guard = $ this ->getGuard ();
9697 $ guard ->setDispatcher ($ events = m::mock (Dispatcher::class));
98+ $ timebox = $ guard ->getTimebox ();
99+ $ timebox ->shouldReceive ('call ' )->once ()->andReturnUsing (function ($ callback ) use ($ timebox ) {
100+ return $ callback ($ timebox );
101+ });
97102 $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Attempting::class));
98103 $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Failed::class));
99104 $ events ->shouldNotReceive ('dispatch ' )->with (m::type (Validated::class));
@@ -103,9 +108,12 @@ public function testAttemptCallsRetrieveByCredentials()
103108
104109 public function testAttemptReturnsUserInterface ()
105110 {
106- [$ session , $ provider , $ request , $ cookie ] = $ this ->getMocks ();
107- $ guard = $ this ->getMockBuilder (SessionGuard::class)->onlyMethods (['login ' ])->setConstructorArgs (['default ' , $ provider , $ session , $ request ])->getMock ();
111+ [$ session , $ provider , $ request , $ cookie, $ timebox ] = $ this ->getMocks ();
112+ $ guard = $ this ->getMockBuilder (SessionGuard::class)->onlyMethods (['login ' ])->setConstructorArgs (['default ' , $ provider , $ session , $ request, $ timebox ])->getMock ();
108113 $ guard ->setDispatcher ($ events = m::mock (Dispatcher::class));
114+ $ timebox ->shouldReceive ('call ' )->once ()->andReturnUsing (function ($ callback , $ microseconds ) use ($ timebox ) {
115+ return $ callback ($ timebox ->shouldReceive ('returnEarly ' )->once ()->getMock ());
116+ });
109117 $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Attempting::class));
110118 $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Validated::class));
111119 $ user = $ this ->createMock (Authenticatable::class);
@@ -119,6 +127,10 @@ public function testAttemptReturnsFalseIfUserNotGiven()
119127 {
120128 $ mock = $ this ->getGuard ();
121129 $ mock ->setDispatcher ($ events = m::mock (Dispatcher::class));
130+ $ timebox = $ mock ->getTimebox ();
131+ $ timebox ->shouldReceive ('call ' )->once ()->andReturnUsing (function ($ callback , $ microseconds ) use ($ timebox ) {
132+ return $ callback ($ timebox );
133+ });
122134 $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Attempting::class));
123135 $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Failed::class));
124136 $ events ->shouldNotReceive ('dispatch ' )->with (m::type (Validated::class));
@@ -128,9 +140,12 @@ public function testAttemptReturnsFalseIfUserNotGiven()
128140
129141 public function testAttemptAndWithCallbacks ()
130142 {
131- [$ session , $ provider , $ request , $ cookie ] = $ this ->getMocks ();
132- $ mock = $ this ->getMockBuilder (SessionGuard::class)->onlyMethods (['getName ' ])->setConstructorArgs (['default ' , $ provider , $ session , $ request ])->getMock ();
143+ [$ session , $ provider , $ request , $ cookie, $ timebox ] = $ this ->getMocks ();
144+ $ mock = $ this ->getMockBuilder (SessionGuard::class)->onlyMethods (['getName ' ])->setConstructorArgs (['default ' , $ provider , $ session , $ request, $ timebox ])->getMock ();
133145 $ mock ->setDispatcher ($ events = m::mock (Dispatcher::class));
146+ $ timebox ->shouldReceive ('call ' )->andReturnUsing (function ($ callback ) use ($ timebox ) {
147+ return $ callback ($ timebox ->shouldReceive ('returnEarly ' )->getMock ());
148+ });
134149 $ user = m::mock (Authenticatable::class);
135150 $ events ->shouldReceive ('dispatch ' )->times (3 )->with (m::type (Attempting::class));
136151 $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Login::class));
@@ -212,6 +227,10 @@ public function testFailedAttemptFiresFailedEvent()
212227 {
213228 $ guard = $ this ->getGuard ();
214229 $ guard ->setDispatcher ($ events = m::mock (Dispatcher::class));
230+ $ timebox = $ guard ->getTimebox ();
231+ $ timebox ->shouldReceive ('call ' )->once ()->andReturnUsing (function ($ callback , $ microseconds ) use ($ timebox ) {
232+ return $ callback ($ timebox );
233+ });
215234 $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Attempting::class));
216235 $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Failed::class));
217236 $ events ->shouldNotReceive ('dispatch ' )->with (m::type (Validated::class));
@@ -544,9 +563,12 @@ public function testUserUsesRememberCookieIfItExists()
544563
545564 public function testLoginOnceSetsUser ()
546565 {
547- [$ session , $ provider , $ request , $ cookie ] = $ this ->getMocks ();
548- $ guard = m::mock (SessionGuard::class, ['default ' , $ provider , $ session ])->makePartial ();
566+ [$ session , $ provider , $ request , $ cookie, $ timebox ] = $ this ->getMocks ();
567+ $ guard = m::mock (SessionGuard::class, ['default ' , $ provider , $ session, $ request , $ timebox ])->makePartial ();
549568 $ user = m::mock (Authenticatable::class);
569+ $ timebox ->shouldReceive ('call ' )->once ()->andReturnUsing (function ($ callback ) use ($ timebox ) {
570+ return $ callback ($ timebox ->shouldReceive ('returnEarly ' )->once ()->getMock ());
571+ });
550572 $ guard ->getProvider ()->shouldReceive ('retrieveByCredentials ' )->once ()->with (['foo ' ])->andReturn ($ user );
551573 $ guard ->getProvider ()->shouldReceive ('validateCredentials ' )->once ()->with ($ user , ['foo ' ])->andReturn (true );
552574 $ guard ->shouldReceive ('setUser ' )->once ()->with ($ user );
@@ -555,19 +577,22 @@ public function testLoginOnceSetsUser()
555577
556578 public function testLoginOnceFailure ()
557579 {
558- [$ session , $ provider , $ request , $ cookie ] = $ this ->getMocks ();
559- $ guard = m::mock (SessionGuard::class, ['default ' , $ provider , $ session ])->makePartial ();
580+ [$ session , $ provider , $ request , $ cookie, $ timebox ] = $ this ->getMocks ();
581+ $ guard = m::mock (SessionGuard::class, ['default ' , $ provider , $ session, $ request , $ timebox ])->makePartial ();
560582 $ user = m::mock (Authenticatable::class);
583+ $ timebox ->shouldReceive ('call ' )->once ()->andReturnUsing (function ($ callback ) use ($ timebox ) {
584+ return $ callback ($ timebox );
585+ });
561586 $ guard ->getProvider ()->shouldReceive ('retrieveByCredentials ' )->once ()->with (['foo ' ])->andReturn ($ user );
562587 $ guard ->getProvider ()->shouldReceive ('validateCredentials ' )->once ()->with ($ user , ['foo ' ])->andReturn (false );
563588 $ this ->assertFalse ($ guard ->once (['foo ' ]));
564589 }
565590
566591 protected function getGuard ()
567592 {
568- [$ session , $ provider , $ request , $ cookie ] = $ this ->getMocks ();
593+ [$ session , $ provider , $ request , $ cookie, $ timebox ] = $ this ->getMocks ();
569594
570- return new SessionGuard ('default ' , $ provider , $ session , $ request );
595+ return new SessionGuard ('default ' , $ provider , $ session , $ request, $ timebox );
571596 }
572597
573598 protected function getMocks ()
@@ -577,6 +602,7 @@ protected function getMocks()
577602 m::mock (UserProvider::class),
578603 Request::create ('/ ' , 'GET ' ),
579604 m::mock (CookieJar::class),
605+ m::mock (Timebox::class),
580606 ];
581607 }
582608
0 commit comments