22
33namespace Tests \Unit ;
44
5- use Monolog \Logger ;
65use Tests \TestCase ;
76use Illuminate \Http \Request ;
87use Illuminate \Support \Facades \Log ;
@@ -35,12 +34,11 @@ public function test_request_body_is_always_empty_when_not_json()
3534 $ methods = ['get ' , 'post ' , 'put ' , 'patch ' , 'delete ' ];
3635
3736 foreach ($ methods as $ method ) {
38- $ loggerMock = $ this ->createMock (Logger::class);
39-
40- Log::partialMock ()->shouldReceive ('getLogger ' )->once ()->withAnyArgs ()->andReturn ($ loggerMock );
37+ $ loggerMock = Log::partialMock ();
38+ Log::setApplication ($ this ->app );
4139
4240 // Assert debug was called on loggerMock once with {} request body
43- $ loggerMock ->expects ( $ this -> once ())-> method ( 'debug ' )->with ( $ this -> stringStartsWith ( ' Timing for ' ))-> willReturnCallback (function ($ message , $ context ) {
41+ $ loggerMock ->shouldReceive ( 'debug ' )->once ()-> andReturnUsing (function ($ message , $ context ) {
4442 $ this ->assertEquals ('{} ' , $ context ['http ' ]['request ' ]['body ' ]['content ' ]);
4543 });
4644
@@ -51,11 +49,11 @@ public function test_request_body_is_always_empty_when_not_json()
5149 public function test_it_masks_request_headers ()
5250 {
5351 // Arrange
54- $ loggerMock = $ this -> createMock (Logger::class );
55- Log::partialMock ()-> shouldReceive ( ' getLogger ' )-> once ()-> withAnyArgs ()-> andReturn ( $ loggerMock );
52+ $ loggerMock = Log:: partialMock ( );
53+ Log::setApplication ( $ this -> app );
5654
5755 // Assert debug was called on loggerMock once with {} request body
58- $ loggerMock ->expects ( $ this -> once ())-> method ( 'debug ' )->with ( $ this -> stringStartsWith ( ' Timing for ' ))-> willReturnCallback (function ($ message , $ context ) {
56+ $ loggerMock ->shouldReceive ( 'debug ' )->once ()-> andReturnUsing (function ($ message , $ context ) {
5957 $ loggedHeaders = $ context ['http ' ]['request ' ]['headers.raw ' ];
6058 $ loggedHeaders = json_decode ($ loggedHeaders , true );
6159
@@ -76,11 +74,11 @@ public function test_it_masks_request_headers()
7674 public function test_it_masks_duplicate_request_headers ()
7775 {
7876 // Arrange
79- $ loggerMock = $ this -> createMock (Logger::class );
80- Log::partialMock ()-> shouldReceive ( ' getLogger ' )-> once ()-> withAnyArgs ()-> andReturn ( $ loggerMock );
77+ $ loggerMock = Log:: partialMock ( );
78+ Log::setApplication ( $ this -> app );
8179
8280 // Assert debug was called on loggerMock once with {} request body
83- $ loggerMock ->expects ( $ this -> once ())-> method ( 'debug ' )->with ( $ this -> stringStartsWith ( ' Timing for ' ))-> willReturnCallback (function ($ message , $ context ) {
81+ $ loggerMock ->shouldReceive ( 'debug ' )->once ()-> andReturnUsing (function ($ message , $ context ) {
8482 $ loggedHeaders = $ context ['http ' ]['request ' ]['headers.raw ' ];
8583 $ loggedHeaders = json_decode ($ loggedHeaders , true );
8684 $ this ->assertEquals ('[ MASKED ] ' , $ loggedHeaders ['x-encrypt-this-header ' ][0 ]);
@@ -101,11 +99,11 @@ public function test_it_masks_duplicate_request_headers()
10199 public function test_it_masks_request_body ()
102100 {
103101 // Arrange
104- $ loggerMock = $ this -> createMock (Logger::class );
105- Log::partialMock ()-> shouldReceive ( ' getLogger ' )-> once ()-> withAnyArgs ()-> andReturn ( $ loggerMock );
102+ $ loggerMock = Log:: partialMock ( );
103+ Log::setApplication ( $ this -> app );
106104
107105 // Assert debug was called on loggerMock once with {} request body
108- $ loggerMock ->expects ( $ this -> once ())-> method ( 'debug ' )->with ( $ this -> stringStartsWith ( ' Timing for ' ))-> willReturnCallback (function ($ message , $ context ) {
106+ $ loggerMock ->shouldReceive ( 'debug ' )->once ()-> andReturnUsing (function ($ message , $ context ) {
109107 $ loggedBody = json_decode ($ context ['http ' ]['request ' ]['body ' ]['content ' ], true );
110108 $ this ->assertEquals ([
111109 'password ' => '[ MASKED ] ' ,
@@ -155,11 +153,11 @@ public function test_it_masks_request_body()
155153 public function test_it_tests ()
156154 {
157155 // Arrange
158- $ loggerMock = $ this -> createMock (Logger::class );
159- Log::partialMock ()-> shouldReceive ( ' getLogger ' )-> once ()-> withAnyArgs ()-> andReturn ( $ loggerMock );
156+ $ loggerMock = Log:: partialMock ( );
157+ Log::setApplication ( $ this -> app );
160158
161159 // Assert debug was called on loggerMock once with {} request body
162- $ loggerMock ->expects ( $ this -> once ())-> method ( 'debug ' )->with ( $ this -> stringStartsWith ( ' Timing for ' ))-> willReturnCallback (function ($ message , $ context ) {
160+ $ loggerMock ->shouldReceive ( 'debug ' )->once ()-> andReturnUsing (function ($ message , $ context ) {
163161 $ loggedHeaders = $ context ['http ' ]['request ' ]['headers.raw ' ];
164162 $ loggedHeaders = json_decode ($ loggedHeaders , true );
165163 $ this ->assertEquals ('{"token":"[ MASKED ]","cake":"not-secret"} ' , $ context ['http ' ]['request ' ]['query_string ' ]);
@@ -174,11 +172,10 @@ public function test_it_tests()
174172 public function test_it_masks_request_cookies ()
175173 {
176174 // Arrange
177- $ loggerMock = $ this -> createMock (Logger::class );
178- Log::partialMock ()-> shouldReceive ( ' getLogger ' )-> once ()-> withAnyArgs ()-> andReturn ( $ loggerMock );
175+ $ loggerMock = Log:: partialMock ( );
176+ Log::setApplication ( $ this -> app );
179177
180- // Assert debug was called on loggerMock once with {} request body
181- $ loggerMock ->expects ($ this ->once ())->method ('debug ' )->with ($ this ->stringStartsWith ('Timing for ' ))->willReturnCallback (function ($ message , $ context ) {
178+ $ loggerMock ->shouldReceive ('debug ' )->once ()->andReturnUsing (function ($ message , $ context ) {
182179 $ loggedCookies = $ context ['http ' ]['request ' ]['cookies.raw ' ];
183180 $ loggedCookies = json_decode ($ loggedCookies , true );
184181 $ this ->assertEquals ('[ MASKED ] ' , $ loggedCookies ['SECRET_COOKIE ' ]);
@@ -196,11 +193,11 @@ public function test_it_masks_request_cookies()
196193 public function test_it_masks_response_cookies (): void
197194 {
198195 // Arrange
199- $ loggerMock = $ this -> createMock (Logger::class );
200- Log::partialMock ()-> shouldReceive ( ' getLogger ' )-> once ()-> withAnyArgs ()-> andReturn ( $ loggerMock );
196+ $ loggerMock = Log:: partialMock ( );
197+ Log::setApplication ( $ this -> app );
201198
202199 // Assert debug was called on loggerMock once with {} request body
203- $ loggerMock ->expects ( $ this -> once ())-> method ( 'debug ' )->with ( $ this -> stringStartsWith ( ' Timing for ' ))-> willReturnCallback (function ($ message , $ context ) {
200+ $ loggerMock ->shouldReceive ( 'debug ' )->once ()-> andReturnUsing (function ($ message , $ context ) {
204201 $ loggedCookies = $ context ['http ' ]['response ' ]['cookies.raw ' ];
205202 $ loggedCookies = json_decode ($ loggedCookies , true );
206203 $ this ->assertEquals ('[ MASKED ] ' , $ loggedCookies ['SECRET_COOKIE ' ]['value ' ]);
@@ -220,9 +217,11 @@ public function test_it_masks_response_cookies(): void
220217
221218 public function test_it_doesnt_crash_if_exception_on_response_doesnt_exist (): void
222219 {
223- $ loggerMock = $ this ->createMock (Logger::class);
224- Log::partialMock ()->shouldReceive ('getLogger ' )->once ()->withAnyArgs ()->andReturn ($ loggerMock );
225- Log::partialMock ()->shouldNotReceive ('error ' );
220+ $ loggerMock = Log::partialMock ();
221+ Log::setApplication ($ this ->app );
222+
223+ // Assert debug was called on loggerMock once with {} request body
224+ $ loggerMock ->shouldNotReceive ('error ' );
226225
227226 $ middleware = new LogRequest ();
228227
@@ -238,10 +237,10 @@ public function test_it_truncates_very_long_json_bodies(): void
238237 // Set config request-log.truncateBodyLength to 100
239238 Config::set ('request-log.truncateBodyLength ' , 100 );
240239
241- $ loggerMock = $ this -> createMock (Logger::class );
242- Log::partialMock ()-> shouldReceive ( ' getLogger ' )-> once ()-> withAnyArgs ()-> andReturn ( $ loggerMock );
240+ $ loggerMock = Log:: partialMock ( );
241+ Log::setApplication ( $ this -> app );
243242
244- $ loggerMock ->expects ( $ this -> once ())-> method ( 'debug ' )->with ( $ this -> stringStartsWith ( ' Timing for ' ))-> willReturnCallback (function ($ message , $ context ) {
243+ $ loggerMock ->shouldReceive ( 'debug ' )->once ()-> andReturnUsing (function ($ message , $ context ) {
245244 $ this ->assertEquals (100 , strlen ($ context ['http ' ]['request ' ]['body ' ]['content ' ]));
246245 $ this ->assertEquals (100 , strlen ($ context ['http ' ]['response ' ]['body ' ]['content ' ]));
247246 });
@@ -264,10 +263,11 @@ public function test_it_doesnt_truncate_very_long_json_bodies_if_disabled(): voi
264263 // Set config request-log.truncateBodyLength to 100
265264 Config::set ('request-log.truncateBodyLength ' , -1 );
266265
267- $ loggerMock = $ this -> createMock (Logger::class );
268- Log::partialMock ()-> shouldReceive ( ' getLogger ' )-> once ()-> withAnyArgs ()-> andReturn ( $ loggerMock );
266+ $ loggerMock = Log:: partialMock ( );
267+ Log::setApplication ( $ this -> app );
269268
270- $ loggerMock ->expects ($ this ->once ())->method ('debug ' )->with ($ this ->stringStartsWith ('Timing for ' ))->willReturnCallback (function ($ message , $ context ) {
269+ // Assert debug was called on loggerMock once with {} request body
270+ $ loggerMock ->shouldReceive ('debug ' )->once ()->andReturnUsing (function ($ message , $ context ) {
271271 $ this ->assertEquals (48897 , strlen ($ context ['http ' ]['request ' ]['body ' ]['content ' ]));
272272 $ this ->assertEquals (48897 , strlen ($ context ['http ' ]['response ' ]['body ' ]['content ' ]));
273273 });
@@ -290,10 +290,10 @@ public function test_it_doesnt_truncate_bodies_shorter_than_truncate_limit(): vo
290290 // Set config request-log.truncateBodyLength to 100
291291 Config::set ('request-log.truncateBodyLength ' , 100 );
292292
293- $ loggerMock = $ this -> createMock (Logger::class );
294- Log::partialMock ()-> shouldReceive ( ' getLogger ' )-> once ()-> withAnyArgs ()-> andReturn ( $ loggerMock );
293+ $ loggerMock = Log:: partialMock ( );
294+ Log::setApplication ( $ this -> app );
295295
296- $ loggerMock ->expects ( $ this -> once ())-> method ( 'debug ' )->with ( $ this -> stringStartsWith ( ' Timing for ' ))-> willReturnCallback (function ($ message , $ context ) {
296+ $ loggerMock ->shouldReceive ( 'debug ' )->once ()-> andReturnUsing (function ($ message , $ context ) {
297297 $ this ->assertEquals (3 , strlen ($ context ['http ' ]['request ' ]['body ' ]['content ' ]));
298298 $ this ->assertEquals (3 , strlen ($ context ['http ' ]['response ' ]['body ' ]['content ' ]));
299299 });
0 commit comments