16
16
use Illuminate \Contracts \Events \Dispatcher ;
17
17
use Illuminate \Contracts \Session \Session ;
18
18
use Illuminate \Cookie \CookieJar ;
19
+ use Illuminate \Support \Timebox ;
19
20
use Mockery as m ;
20
21
use PHPUnit \Framework \TestCase ;
21
22
use Symfony \Component \HttpFoundation \Cookie ;
@@ -94,6 +95,10 @@ public function testAttemptCallsRetrieveByCredentials()
94
95
{
95
96
$ guard = $ this ->getGuard ();
96
97
$ 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
+ });
97
102
$ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Attempting::class));
98
103
$ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Failed::class));
99
104
$ events ->shouldNotReceive ('dispatch ' )->with (m::type (Validated::class));
@@ -103,9 +108,12 @@ public function testAttemptCallsRetrieveByCredentials()
103
108
104
109
public function testAttemptReturnsUserInterface ()
105
110
{
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 ();
108
113
$ 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
+ });
109
117
$ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Attempting::class));
110
118
$ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Validated::class));
111
119
$ user = $ this ->createMock (Authenticatable::class);
@@ -119,6 +127,10 @@ public function testAttemptReturnsFalseIfUserNotGiven()
119
127
{
120
128
$ mock = $ this ->getGuard ();
121
129
$ 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
+ });
122
134
$ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Attempting::class));
123
135
$ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Failed::class));
124
136
$ events ->shouldNotReceive ('dispatch ' )->with (m::type (Validated::class));
@@ -128,9 +140,12 @@ public function testAttemptReturnsFalseIfUserNotGiven()
128
140
129
141
public function testAttemptAndWithCallbacks ()
130
142
{
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 ();
133
145
$ mock ->setDispatcher ($ events = m::mock (Dispatcher::class));
146
+ $ timebox ->shouldReceive ('call ' )->andReturnUsing (function ($ callback ) use ($ timebox ) {
147
+ return $ callback ($ timebox ->shouldReceive ('returnEarly ' )->getMock ());
148
+ });
134
149
$ user = m::mock (Authenticatable::class);
135
150
$ events ->shouldReceive ('dispatch ' )->times (3 )->with (m::type (Attempting::class));
136
151
$ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Login::class));
@@ -212,6 +227,10 @@ public function testFailedAttemptFiresFailedEvent()
212
227
{
213
228
$ guard = $ this ->getGuard ();
214
229
$ 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
+ });
215
234
$ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Attempting::class));
216
235
$ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (Failed::class));
217
236
$ events ->shouldNotReceive ('dispatch ' )->with (m::type (Validated::class));
@@ -544,9 +563,12 @@ public function testUserUsesRememberCookieIfItExists()
544
563
545
564
public function testLoginOnceSetsUser ()
546
565
{
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 ();
549
568
$ user = m::mock (Authenticatable::class);
569
+ $ timebox ->shouldReceive ('call ' )->once ()->andReturnUsing (function ($ callback ) use ($ timebox ) {
570
+ return $ callback ($ timebox ->shouldReceive ('returnEarly ' )->once ()->getMock ());
571
+ });
550
572
$ guard ->getProvider ()->shouldReceive ('retrieveByCredentials ' )->once ()->with (['foo ' ])->andReturn ($ user );
551
573
$ guard ->getProvider ()->shouldReceive ('validateCredentials ' )->once ()->with ($ user , ['foo ' ])->andReturn (true );
552
574
$ guard ->shouldReceive ('setUser ' )->once ()->with ($ user );
@@ -555,19 +577,22 @@ public function testLoginOnceSetsUser()
555
577
556
578
public function testLoginOnceFailure ()
557
579
{
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 ();
560
582
$ user = m::mock (Authenticatable::class);
583
+ $ timebox ->shouldReceive ('call ' )->once ()->andReturnUsing (function ($ callback ) use ($ timebox ) {
584
+ return $ callback ($ timebox );
585
+ });
561
586
$ guard ->getProvider ()->shouldReceive ('retrieveByCredentials ' )->once ()->with (['foo ' ])->andReturn ($ user );
562
587
$ guard ->getProvider ()->shouldReceive ('validateCredentials ' )->once ()->with ($ user , ['foo ' ])->andReturn (false );
563
588
$ this ->assertFalse ($ guard ->once (['foo ' ]));
564
589
}
565
590
566
591
protected function getGuard ()
567
592
{
568
- [$ session , $ provider , $ request , $ cookie ] = $ this ->getMocks ();
593
+ [$ session , $ provider , $ request , $ cookie, $ timebox ] = $ this ->getMocks ();
569
594
570
- return new SessionGuard ('default ' , $ provider , $ session , $ request );
595
+ return new SessionGuard ('default ' , $ provider , $ session , $ request, $ timebox );
571
596
}
572
597
573
598
protected function getMocks ()
@@ -577,6 +602,7 @@ protected function getMocks()
577
602
m::mock (UserProvider::class),
578
603
Request::create ('/ ' , 'GET ' ),
579
604
m::mock (CookieJar::class),
605
+ m::mock (Timebox::class),
580
606
];
581
607
}
582
608
0 commit comments