Skip to content

Commit e45ba17

Browse files
committed
Test $deleteWhenMissingModels
1 parent 8a19e95 commit e45ba17

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

tests/QueueTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Tests\Support\FailingJobWithExponentialBackoff;
2525
use Tests\Support\JobThatWillBeReleased;
2626
use Tests\Support\SimpleJob;
27+
use Tests\Support\User;
28+
use Tests\Support\UserJob;
2729

2830
class QueueTest extends TestCase
2931
{
@@ -444,7 +446,32 @@ public function test_queue_looping_hook_not_supported_with_this_package()
444446
/** @test */
445447
public function test_ignoring_jobs_with_deleted_models()
446448
{
447-
// todo
448-
$this->assertTrue(true);
449+
// Arrange
450+
CloudTasksApi::fake();
451+
OpenIdVerificator::fake();
452+
Log::swap(new LogFake());
453+
454+
$user1 = User::create([
455+
'name' => 'John',
456+
'email' => 'johndoe@example.com',
457+
'password' => bcrypt('test'),
458+
]);
459+
460+
$user2 = User::create([
461+
'name' => 'Jane',
462+
'email' => 'janedoe@example.com',
463+
'password' => bcrypt('test'),
464+
]);
465+
466+
// Act
467+
$this->dispatch(new UserJob($user1))->runWithoutExceptionHandler();
468+
469+
$job = $this->dispatch(new UserJob($user2));
470+
$user2->delete();
471+
$job->runWithoutExceptionHandler();
472+
473+
// Act
474+
Log::assertLogged('UserJob:John');
475+
CloudTasksApi::assertTaskDeleted($job->task->getName());
449476
}
450477
}

tests/Support/User.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Support;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class User extends Model
10+
{
11+
protected $guarded = [];
12+
}

tests/Support/UserJob.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Tests\Support;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Queue\SerializesModels;
10+
use Illuminate\Support\Facades\Mail;
11+
12+
class UserJob implements ShouldQueue
13+
{
14+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
15+
16+
public $deleteWhenMissingModels = true;
17+
18+
protected User $user;
19+
20+
public function __construct(User $user)
21+
{
22+
$this->user = $user;
23+
}
24+
25+
/**
26+
* Execute the job.
27+
*
28+
* @return void
29+
*/
30+
public function handle()
31+
{
32+
logger('UserJob:' . $this->user->name);
33+
}
34+
}

0 commit comments

Comments
 (0)