Skip to content

Commit 2cc4ffc

Browse files
committed
More tests.
1 parent 71fe908 commit 2cc4ffc

File tree

7 files changed

+134
-2
lines changed

7 files changed

+134
-2
lines changed

tests/Feature/SchedulerDefaultConstraintsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function test_can_send_serialized_constrained(): void
107107
)
108108
->many()
109109
->constraint(
110-
new SerializedConditionCondition(new User([
110+
new SerializedConditionCondition(User::create([
111111
'email' => '[email protected]',
112112
]))
113113
);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Tests\Feature;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
6+
use Binarcode\LaravelMailator\Tests\Fixtures\InvoiceReminderMailable;
7+
use Binarcode\LaravelMailator\Tests\Fixtures\Post;
8+
use Binarcode\LaravelMailator\Tests\Fixtures\SerializedConditionCondition;
9+
use Binarcode\LaravelMailator\Tests\Fixtures\User;
10+
use Binarcode\LaravelMailator\Tests\TestCase;
11+
use Illuminate\Database\Eloquent\Model;
12+
use Illuminate\Support\Facades\Mail;
13+
14+
class SerializationTest extends TestCase
15+
{
16+
public function test_constraints_gets_latest_database_values(): void
17+
{
18+
Mail::fake();
19+
Mail::assertNothingSent();
20+
21+
$user = User::factory()->has(
22+
Post::factory()->state([
23+
'title' => 'Test title'
24+
]), 'posts'
25+
)->create([
26+
'email' => '[email protected]',
27+
]);
28+
29+
$scheduler = MailatorSchedule::init('Invoice reminder.')
30+
->recipients([
31+
32+
])
33+
->mailable(
34+
(new InvoiceReminderMailable())->to('[email protected]')
35+
)
36+
->many()
37+
->constraint(
38+
new SerializedConditionCondition($user)
39+
);
40+
41+
$scheduler->save();
42+
43+
$user->update([
44+
'email' => $newEmail = '[email protected]',
45+
]);
46+
47+
$_SERVER['fakeSerializedEmail'] = $newEmail;
48+
49+
MailatorSchedule::run();
50+
51+
Mail::assertSent(InvoiceReminderMailable::class, 1);
52+
}
53+
54+
}

tests/Fixtures/Post.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Tests\Fixtures;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Post extends Model
9+
{
10+
use HasFactory;
11+
}

tests/Fixtures/SerializedConditionCondition.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
use Binarcode\LaravelMailator\Constraints\SendScheduleConstraint;
66
use Binarcode\LaravelMailator\Models\MailatorSchedule;
7+
use Illuminate\Queue\SerializesModels;
78
use Illuminate\Support\Collection;
89

910
class SerializedConditionCondition implements SendScheduleConstraint
1011
{
12+
use SerializesModels;
13+
1114
public User $user;
1215

1316
public function __construct(User $user)
@@ -17,6 +20,6 @@ public function __construct(User $user)
1720

1821
public function canSend(MailatorSchedule $schedule, Collection $logs): bool
1922
{
20-
return $this->user->email === '[email protected]';
23+
return $this->user->email === data_get($_SERVER, 'fakeSerializedEmail', '[email protected]');
2124
}
2225
}

tests/Fixtures/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Auth\MustVerifyEmail;
77
use Illuminate\Database\Eloquent\Factories\HasFactory;
88
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Relations\HasMany;
910

1011
class User extends Model
1112
{
@@ -15,4 +16,9 @@ class User extends Model
1516

1617

1718
protected $guarded = [];
19+
20+
public function posts(): HasMany
21+
{
22+
return $this->hasMany(Post::class);
23+
}
1824
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Tests\database\Factories;
4+
5+
use Binarcode\LaravelMailator\Tests\Fixtures\Post;
6+
use Binarcode\LaravelMailator\Tests\Fixtures\User;
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
9+
class PostFactory extends Factory
10+
{
11+
protected $model = Post::class;
12+
13+
public function definition()
14+
{
15+
return [
16+
'title' => $this->faker->sentence(),
17+
'user_id' => User::factory(),
18+
];
19+
}
20+
21+
public static function one(array $attributes = []): Post
22+
{
23+
return app(static::class)->create($attributes);
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('posts', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('title')->nullable();
19+
$table->foreignId('user_id')->constrained('users');
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::dropIfExists('posts');
32+
}
33+
};

0 commit comments

Comments
 (0)