Skip to content

Commit dfb24c6

Browse files
committed
Get rid of LogFake and clean up test jobs
1 parent 00b3343 commit dfb24c6

File tree

11 files changed

+64
-183
lines changed

11 files changed

+64
-183
lines changed

src/LogFake.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

tests/QueueTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
use Illuminate\Support\Carbon;
1414
use Illuminate\Support\Facades\DB;
1515
use Illuminate\Support\Facades\Event;
16-
use Illuminate\Support\Facades\Log;
1716
use Illuminate\Support\Facades\Queue;
1817
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
1918
use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleased;
20-
use Stackkit\LaravelGoogleCloudTasksQueue\LogFake;
2119
use Tests\Support\FailingJob;
2220
use Tests\Support\FailingJobWithExponentialBackoff;
21+
use Tests\Support\JobOutput;
2322
use Tests\Support\JobThatWillBeReleased;
2423
use Tests\Support\SimpleJob;
2524
use Tests\Support\User;
@@ -375,7 +374,7 @@ public function test_failing_method_on_job()
375374
{
376375
// Arrange
377376
CloudTasksApi::fake();
378-
Log::swap(new LogFake());
377+
Event::fake(JobOutput::class);
379378

380379
// Act
381380
$this->dispatch(new FailingJob())
@@ -384,53 +383,54 @@ public function test_failing_method_on_job()
384383
->runAndGetReleasedJob();
385384

386385
// Assert
387-
Log::assertLogged('FailingJob:failed');
386+
Event::assertDispatched(fn (JobOutput $event) => $event->output === 'FailingJob:failed');
388387
}
389388

390389
/** @test */
391390
public function test_queue_before_and_after_hooks()
392391
{
393392
// Arrange
394393
CloudTasksApi::fake();
395-
Log::swap(new LogFake());
394+
Event::fake(JobOutput::class);
396395

397396
// Act
398397
Queue::before(function (JobProcessing $event) {
399-
logger('Queue::before:'.$event->job->payload()['data']['commandName']);
398+
event(new JobOutput('Queue::before:'.$event->job->payload()['data']['commandName']));
400399
});
401400
Queue::after(function (JobProcessed $event) {
402-
logger('Queue::after:'.$event->job->payload()['data']['commandName']);
401+
event(new JobOutput('Queue::after:'.$event->job->payload()['data']['commandName']));
403402
});
404403
$this->dispatch(new SimpleJob())->run();
405404

406405
// Assert
407-
Log::assertLogged('Queue::before:Tests\Support\SimpleJob');
408-
Log::assertLogged('Queue::after:Tests\Support\SimpleJob');
406+
Event::assertDispatched(fn (JobOutput $event) => $event->output === 'Queue::before:Tests\Support\SimpleJob');
407+
Event::assertDispatched(fn (JobOutput $event) => $event->output === 'Queue::after:Tests\Support\SimpleJob');
409408
}
410409

411410
/** @test */
412411
public function test_queue_looping_hook_not_supported_with_this_package()
413412
{
414413
// Arrange
415414
CloudTasksApi::fake();
416-
Log::swap(new LogFake());
415+
Event::fake(JobOutput::class);
417416

418417
// Act
419418
Queue::looping(function () {
420-
logger('Queue::looping');
419+
event(new JobOutput('Queue::looping'));
421420
});
422421
$this->dispatch(new SimpleJob())->run();
423422

424423
// Assert
425-
Log::assertNotLogged('Queue::looping');
424+
Event::assertDispatchedTimes(JobOutput::class, times: 1);
425+
Event::assertDispatched(fn (JobOutput $event) => $event->output === 'SimpleJob:success');
426426
}
427427

428428
/** @test */
429429
public function test_ignoring_jobs_with_deleted_models()
430430
{
431431
// Arrange
432432
CloudTasksApi::fake();
433-
Log::swap(new LogFake());
433+
Event::fake(JobOutput::class);
434434

435435
$user1 = User::create([
436436
'name' => 'John',
@@ -452,7 +452,7 @@ public function test_ignoring_jobs_with_deleted_models()
452452
$job->runWithoutExceptionHandler();
453453

454454
// Act
455-
Log::assertLogged('UserJob:John');
455+
Event::assertDispatched(fn (JobOutput $event) => $event->output === 'UserJob:John');
456456
CloudTasksApi::assertTaskNotDeleted($job->task->getName());
457457
}
458458

tests/Support/BaseJob.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Support;
6+
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class BaseJob implements ShouldQueue
14+
{
15+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16+
}

tests/Support/EncryptedJob.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@
44

55
namespace Tests\Support;
66

7-
use Illuminate\Bus\Queueable;
87
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
9-
use Illuminate\Contracts\Queue\ShouldQueue;
10-
use Illuminate\Foundation\Bus\Dispatchable;
11-
use Illuminate\Queue\InteractsWithQueue;
12-
use Illuminate\Queue\SerializesModels;
138

14-
class EncryptedJob implements ShouldBeEncrypted, ShouldQueue
9+
class EncryptedJob extends BaseJob implements ShouldBeEncrypted
1510
{
16-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
17-
1811
public function handle()
1912
{
20-
logger('EncryptedJob:success');
13+
event(new JobOutput('EncryptedJob:success'));
2114
}
2215
}

tests/Support/FailingJob.php

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,19 @@
44

55
namespace Tests\Support;
66

7-
use Illuminate\Bus\Queueable;
8-
use Illuminate\Contracts\Queue\ShouldQueue;
9-
use Illuminate\Foundation\Bus\Dispatchable;
10-
use Illuminate\Queue\InteractsWithQueue;
11-
use Illuminate\Queue\SerializesModels;
7+
use Error;
128

13-
class FailingJob implements ShouldQueue
9+
class FailingJob extends BaseJob
1410
{
15-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16-
1711
public $tries = 3;
1812

19-
/**
20-
* Create a new job instance.
21-
*
22-
* @return void
23-
*/
24-
public function __construct()
25-
{
26-
//
27-
}
28-
29-
/**
30-
* Execute the job.
31-
*
32-
* @return void
33-
*/
3413
public function handle()
3514
{
36-
throw new \Error('simulating a failing job');
15+
throw new Error('simulating a failing job');
3716
}
3817

39-
public function failed(\Throwable $throwable)
18+
public function failed()
4019
{
41-
logger('FailingJob:failed');
20+
event(new JobOutput('FailingJob:failed'));
4221
}
4322
}

tests/Support/FailingJobWithExponentialBackoff.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,10 @@
44

55
namespace Tests\Support;
66

7-
use Illuminate\Bus\Queueable;
8-
use Illuminate\Contracts\Queue\ShouldQueue;
9-
use Illuminate\Foundation\Bus\Dispatchable;
10-
use Illuminate\Queue\InteractsWithQueue;
11-
use Illuminate\Queue\SerializesModels;
12-
13-
class FailingJobWithExponentialBackoff implements ShouldQueue
7+
class FailingJobWithExponentialBackoff extends FailingJob
148
{
15-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16-
179
public $tries = 5;
1810

19-
public function handle()
20-
{
21-
throw new \Error('simulating a failing job');
22-
}
23-
2411
public function backoff(): array
2512
{
2613
return [50, 60, 70];

tests/Support/JobOutput.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Support;
6+
7+
class JobOutput
8+
{
9+
public function __construct(public string $output)
10+
{
11+
//
12+
}
13+
}

tests/Support/JobThatWillBeReleased.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,17 @@
44

55
namespace Tests\Support;
66

7-
use Illuminate\Bus\Queueable;
8-
use Illuminate\Contracts\Queue\ShouldQueue;
9-
use Illuminate\Foundation\Bus\Dispatchable;
10-
use Illuminate\Queue\InteractsWithQueue;
11-
use Illuminate\Queue\SerializesModels;
12-
13-
class JobThatWillBeReleased implements ShouldQueue
7+
class JobThatWillBeReleased extends BaseJob
148
{
15-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16-
17-
private int $releaseDelay;
18-
199
public $tries = 3;
2010

21-
/**
22-
* Create a new job instance.
23-
*
24-
* @return void
25-
*/
26-
public function __construct(int $releaseDelay = 0)
11+
public function __construct(private int $releaseDelay = 0)
2712
{
28-
$this->releaseDelay = $releaseDelay;
13+
//
2914
}
3015

31-
/**
32-
* Execute the job.
33-
*
34-
* @return void
35-
*/
3616
public function handle()
3717
{
38-
logger('JobThatWillBeReleased:beforeRelease');
3918
$this->release($this->releaseDelay);
40-
logger('JobThatWillBeReleased:afterRelease');
4119
}
4220
}

tests/Support/SimpleJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public function __construct()
3333
*/
3434
public function handle()
3535
{
36-
logger('SimpleJob:success');
36+
event(new JobOutput('SimpleJob:success'));
3737
}
3838
}

tests/Support/UserJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public function __construct(User $user)
3030
*/
3131
public function handle()
3232
{
33-
logger('UserJob:'.$this->user->name);
33+
event(new JobOutput('UserJob:'.$this->user->name));
3434
}
3535
}

0 commit comments

Comments
 (0)