File tree Expand file tree Collapse file tree 6 files changed +108
-0
lines changed Expand file tree Collapse file tree 6 files changed +108
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 33namespace Binarcode \LaravelMailator \Tests \Feature \Models ;
44
55use Binarcode \LaravelMailator \Models \MailatorSchedule ;
6+ use Binarcode \LaravelMailator \Tests \database \Factories \UserFactory ;
67use Binarcode \LaravelMailator \Tests \Fixtures \CustomAction ;
78use Binarcode \LaravelMailator \Tests \Fixtures \InvoiceReminderMailable ;
89use 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 ()
Original file line number Diff line number Diff line change 22
33namespace Binarcode \LaravelMailator \Tests \Fixtures ;
44
5+ use Binarcode \LaravelMailator \Models \Concerns \HasMailatorSchedulers ;
6+ use Illuminate \Database \Eloquent \Factories \HasFactory ;
57use Illuminate \Database \Eloquent \Model ;
68
79class User extends Model
810{
11+ use HasFactory;
12+ use HasMailatorSchedulers;
13+
14+
915 protected $ guarded = [];
1016}
Original file line number Diff line number Diff line change 1010
1111class 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 ();
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments