1212use Illuminate \Container \Container ;
1313use Illuminate \Contracts \Console \Kernel ;
1414use Illuminate \Http \Request ;
15+ use Illuminate \Log \LogManager ;
1516use Illuminate \Support \Facades \Event ;
16- use Illuminate \Support \Facades \Log ;
1717use Mockery ;
1818use phpseclib \Crypt \RSA ;
1919use Stackkit \LaravelGoogleCloudScheduler \CloudSchedulerException ;
2020use Stackkit \LaravelGoogleCloudScheduler \Command ;
2121use Stackkit \LaravelGoogleCloudScheduler \OpenIdVerificator ;
2222use Stackkit \LaravelGoogleCloudScheduler \TaskHandler ;
2323use Throwable ;
24- use TiMacDonald \Log \LogFake ;
2524
2625class TaskHandlerTest extends TestCase
2726{
@@ -31,6 +30,16 @@ class TaskHandlerTest extends TestCase
3130 private $ request ;
3231 private $ jwt ;
3332
33+ /**
34+ * @var LogManager
35+ */
36+ private $ laravelLogger = null ;
37+
38+ /**
39+ * @var Mockery\Mock
40+ */
41+ private $ log ;
42+
3443 public function setUp (): void
3544 {
3645 parent ::setUp ();
@@ -66,6 +75,26 @@ public function setUp(): void
6675 app (Schedule::class),
6776 Container::getInstance ()
6877 );
78+
79+ $ this ->registerLogFake ();
80+ }
81+
82+ /**
83+ * Mock the Laravel logger so we can assert the commands are or aren't called.
84+ *
85+ * @return void
86+ */
87+ private function registerLogFake ()
88+ {
89+ if (is_null ($ this ->laravelLogger )) {
90+ $ this ->laravelLogger = app ('log ' );
91+ }
92+
93+ $ this ->log = Mockery::mock ($ this ->laravelLogger );
94+
95+ $ this ->app ->singleton ('log ' , function () {
96+ return $ this ->log ;
97+ });
6998 }
7099
71100 /** @test */
@@ -180,21 +209,28 @@ public function it_prevents_overlapping_if_the_command_is_scheduled_without_over
180209
181210 cache ()->clear ();
182211
183- Log::shouldReceive ('debug ' )->twice ();
184-
185212 $ this ->taskHandler ->handle ();
186213
214+ $ this ->log ->shouldHaveReceived ('debug ' )->once ();
215+
187216 $ expression = '* * * * * ' ;
188217 $ command = ConsoleApplication::formatCommandString ('test:command ' );
189218 $ mutex = 'framework ' .DIRECTORY_SEPARATOR .'schedule- ' .sha1 ($ expression .$ command );
190219
191220 cache ()->add ($ mutex , true , 60 );
221+ $ this ->registerLogFake ();
192222
193223 $ this ->taskHandler ->handle ();
194224
225+ $ this ->log ->shouldNotHaveReceived ('debug ' );
226+
195227 cache ()->delete ($ mutex );
196228
229+ $ this ->registerLogFake ();
230+
197231 $ this ->taskHandler ->handle ();
232+
233+ $ this ->log ->shouldNotHaveReceived ('debug ' );
198234 }
199235
200236 /** @test */
@@ -205,13 +241,11 @@ public function it_runs_the_before_and_after_callbacks()
205241 $ this ->openId ->shouldReceive ('getKidFromOpenIdToken ' )->andReturnNull ();
206242 $ this ->openId ->shouldReceive ('decodeOpenIdToken ' )->andReturnNull ();
207243
208- Log::swap (new LogFake ());
209-
210244 $ this ->taskHandler ->handle ();
211245
212- Log:: assertLoggedMessage ( ' info ' , ' log after ' );
213- Log:: assertLoggedMessage ( ' warning ' , ' log before ' );
214- Log:: assertLoggedMessage ( ' debug ' , ' did something testy ' );
246+ $ this -> log -> shouldHaveReceived ()-> info ( ' log after ')-> once ( );
247+ $ this -> log -> shouldHaveReceived ()-> warning ( ' log before ')-> once ( );
248+ $ this -> log -> shouldHaveReceived ()-> debug ( ' did something testy ')-> once ( );
215249 }
216250
217251 /** @test */
@@ -238,13 +272,11 @@ public function it_can_run_the_schedule_run_command()
238272 $ this ->openId ->shouldReceive ('getKidFromOpenIdToken ' )->andReturnNull ();
239273 $ this ->openId ->shouldReceive ('decodeOpenIdToken ' )->andReturnNull ();
240274
241- Log::swap (new LogFake ());
242-
243275 $ this ->taskHandler ->handle ();
244276
245- Log:: assertLoggedMessage ( ' info ' , ' log after ' );
246- Log:: assertLoggedMessage ( ' warning ' , ' log before ' );
247- Log:: assertLoggedMessage ( ' info ' , ' log call ' );
277+ $ this -> log -> shouldHaveReceived ()-> info ( ' log after ')-> once ( );
278+ $ this -> log -> shouldHaveReceived ()-> warning ( ' log before ')-> once ( );
279+ $ this -> log -> shouldHaveReceived ()-> info ( ' log call ')-> once ( );
248280
249281 // @todo - can't test commands run from schedule:run because testbench has no artisan binary.
250282 // Log::assertLoggedMessage('debug', 'did something testy');
0 commit comments