55use Illuminate \Auth \GenericUser ;
66use Illuminate \Support \Facades \Auth ;
77use Laravel \Nightwatch \UserProvider ;
8+ use RuntimeException ;
89use Tests \TestCase ;
910
11+ use function collect ;
12+ use function json_encode ;
1013use function str_repeat ;
1114use function strlen ;
1215
@@ -17,15 +20,15 @@ public function test_it_limits_the_length_of_the_user_identifier(): void
1720 Auth::login (new GenericUser ([
1821 'id ' => str_repeat ('x ' , 1000 ),
1922 ]));
20- $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => []);
23+ $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => [], fn () => fn () => null );
2124
2225 $ this ->assertSame (1000 , strlen (Auth::id ()));
2326 $ this ->assertSame ($ provider ->id (), str_repeat ('x ' , 255 ));
2427 }
2528
2629 public function test_it_can_lazily_retrieve_the_user (): void
2730 {
28- $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => []);
31+ $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => [], fn () => fn () => null );
2932
3033 $ id = $ provider ->id ();
3134
@@ -38,11 +41,120 @@ public function test_it_can_lazily_retrieve_the_user(): void
3841
3942 public function test_it_can_remember_an_authenticated_user_and_limits_the_length_of_their_identifier (): void
4043 {
41- $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => []);
44+ $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => [], fn () => fn () => null );
4245 $ provider ->remember ($ user = new GenericUser ([
4346 'id ' => str_repeat ('x ' , 1000 ),
4447 ]));
4548
4649 $ this ->assertSame (str_repeat ('x ' , 255 ), $ provider ->id ()->jsonSerialize ());
4750 }
51+
52+ public function test_it_only_reports_exceptions_occurring_while_resolving_user_ids_once_before_user_is_available (): void
53+ {
54+ $ exceptions = collect ();
55+ $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => [], fn () => function ($ e ) use ($ exceptions ) {
56+ $ exceptions [] = $ e ;
57+ });
58+
59+ $ ids = [
60+ $ provider ->id (),
61+ $ provider ->id (),
62+ ];
63+
64+ $ this ->app ['auth ' ]->setUser (new class ([]) extends GenericUser
65+ {
66+ public function getAuthIdentifier ()
67+ {
68+ throw new RuntimeException ('Whoops! ' );
69+ }
70+ });
71+
72+ json_encode ($ ids );
73+
74+ $ this ->assertCount (1 , $ exceptions );
75+ $ this ->assertSame ('Whoops! ' , $ exceptions [0 ]->getMessage ());
76+ }
77+
78+ public function test_it_only_reports_exceptions_occurring_while_resolving_user_ids_once_after_user_is_available (): void
79+ {
80+ $ exceptions = collect ();
81+ $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => [], fn () => function ($ e ) use ($ exceptions ) {
82+ $ exceptions [] = $ e ;
83+ });
84+
85+ $ this ->app ['auth ' ]->setUser (new class ([]) extends GenericUser
86+ {
87+ public function getAuthIdentifier ()
88+ {
89+ throw new RuntimeException ('Whoops! ' );
90+ }
91+ });
92+
93+ json_encode ([
94+ $ provider ->id (),
95+ $ provider ->id (),
96+ ]);
97+
98+ $ this ->assertCount (1 , $ exceptions );
99+ $ this ->assertSame ('Whoops! ' , $ exceptions [0 ]->getMessage ());
100+ }
101+
102+ public function test_it_only_reports_exceptions_occurring_while_resolving_user_ids_once_regardless_of_where_resolving_occurs (): void
103+ {
104+ $ exceptions = collect ();
105+ $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => [], fn () => function ($ e ) use ($ exceptions ) {
106+ $ exceptions [] = $ e ;
107+ });
108+
109+ $ ids = [
110+ $ provider ->id (),
111+ $ provider ->id (),
112+ ];
113+
114+ $ this ->app ['auth ' ]->setUser (new class ([]) extends GenericUser
115+ {
116+ public function getAuthIdentifier ()
117+ {
118+ throw new RuntimeException ('Whoops! ' );
119+ }
120+ });
121+
122+ json_encode ($ ids );
123+ json_encode ([
124+ $ provider ->id (),
125+ $ provider ->id (),
126+ ]);
127+
128+ $ this ->assertCount (1 , $ exceptions );
129+ $ this ->assertSame ('Whoops! ' , $ exceptions [0 ]->getMessage ());
130+ }
131+
132+ public function test_it_allows_reporting_exceptions_occurring_while_resolving_user_ids_again_after_flush (): void
133+ {
134+ $ exceptions = collect ();
135+ $ provider = new UserProvider ($ this ->app ['auth ' ], fn () => [], fn () => function ($ e ) use ($ exceptions ) {
136+ $ exceptions [] = $ e ;
137+ });
138+ $ this ->app ['auth ' ]->setUser (new class ([]) extends GenericUser
139+ {
140+ public function getAuthIdentifier ()
141+ {
142+ throw new RuntimeException ('Whoops! ' );
143+ }
144+ });
145+
146+ json_encode ($ provider ->id ());
147+ json_encode ($ provider ->id ());
148+
149+ $ this ->assertCount (1 , $ exceptions );
150+ $ this ->assertSame ('Whoops! ' , $ exceptions [0 ]->getMessage ());
151+
152+ $ provider ->flush ();
153+
154+ json_encode ($ provider ->id ());
155+ json_encode ($ provider ->id ());
156+
157+ $ this ->assertCount (2 , $ exceptions );
158+ $ this ->assertSame ('Whoops! ' , $ exceptions [1 ]->getMessage ());
159+ }
48160}
0 commit comments