Skip to content

Commit b35960a

Browse files
committed
Test target.
1 parent 32fdb16 commit b35960a

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Models\Concerns;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\MorphMany;
8+
9+
/**
10+
* Trait HasTarget
11+
* @mixin Model
12+
* @package Binarcode\LaravelMailator\Models\Concerns
13+
*/
14+
trait HasMailatorSchedulers
15+
{
16+
public function schedulers(): MorphMany
17+
{
18+
return $this->morphMany(MailatorSchedule::class, 'targetable');
19+
}
20+
}

tests/Feature/Models/MailatorScheduleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Binarcode\LaravelMailator\Tests\Feature\Models;
44

55
use Binarcode\LaravelMailator\Models\MailatorSchedule;
6+
use Binarcode\LaravelMailator\Tests\database\Factories\UserFactory;
67
use Binarcode\LaravelMailator\Tests\Fixtures\CustomAction;
78
use Binarcode\LaravelMailator\Tests\Fixtures\InvoiceReminderMailable;
89
use Binarcode\LaravelMailator\Tests\Fixtures\SerializedConditionCondition;
@@ -23,16 +24,21 @@ protected function setUp(): void
2324

2425
public function test_can_create_mailator_schedule()
2526
{
27+
$user = UserFactory::one();
28+
2629
MailatorSchedule::init('Invoice reminder.')
2730
->mailable(new InvoiceReminderMailable())
2831
->days(1)
2932
->before(now()->addWeek())
33+
->target($user)
3034
->when(function () {
3135
return 'Working.';
3236
})
3337
->save();
3438

3539
$this->assertCount(1, MailatorSchedule::all());
40+
41+
self::assertCount(1, $user->schedulers);
3642
}
3743

3844
public function test_sending_email_only_once()

tests/Fixtures/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace Binarcode\LaravelMailator\Tests\Fixtures;
44

5+
use Binarcode\LaravelMailator\Models\Concerns\HasMailatorSchedulers;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
57
use Illuminate\Database\Eloquent\Model;
68

79
class User extends Model
810
{
11+
use HasFactory;
12+
use HasMailatorSchedulers;
13+
14+
915
protected $guarded = [];
1016
}

tests/TestCase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010

1111
class TestCase extends Orchestra
1212
{
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->loadMigrationsFrom([
18+
'--database' => 'sqlite',
19+
'--path' => realpath(__DIR__.DIRECTORY_SEPARATOR.'database/migrations'),
20+
]);
21+
22+
\Illuminate\Database\Eloquent\Factories\Factory::guessFactoryNamesUsing(
23+
fn (string $modelName) => 'Binarcode\\LaravelMailator\\Tests\\database\\Factories\\'.class_basename($modelName).'Factory'
24+
);
25+
}
26+
1327
protected function tearDown(): void
1428
{
1529
m::close();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Tests\database\Factories;
4+
5+
use Binarcode\LaravelMailator\Tests\Fixtures\User;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Illuminate\Support\Str;
8+
9+
class UserFactory extends Factory
10+
{
11+
protected $model = User::class;
12+
13+
public function definition()
14+
{
15+
return [
16+
'name' => $this->faker->name,
17+
'email' => $this->faker->unique()->safeEmail,
18+
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm',
19+
'remember_token' => Str::random(10),
20+
];
21+
}
22+
23+
public static function one(array $attributes = []): User
24+
{
25+
return app(static::class)->create($attributes);
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateUsersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('users', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('name')->nullable();
19+
$table->string('email');
20+
$table->string('password')->nullable();
21+
$table->string('remember_token')->nullable();
22+
$table->timestamps();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists('users');
34+
}
35+
}

0 commit comments

Comments
 (0)