diff --git a/database/factories/EventFactory.php b/database/factories/EventFactory.php new file mode 100644 index 00000000..b4e9410c --- /dev/null +++ b/database/factories/EventFactory.php @@ -0,0 +1,27 @@ + + */ + protected $model = Event::class; + + /** + * Define the model's default state. + */ + public function definition(): array + { + return [ + 'action' => 'Create', + 'payload' => [], + ]; + } +} diff --git a/src/Models/Event.php b/src/Models/Event.php index 464d8e2e..322a2985 100644 --- a/src/Models/Event.php +++ b/src/Models/Event.php @@ -2,8 +2,10 @@ namespace Cone\Root\Models; +use Cone\Root\Database\Factories\EventFactory; use Cone\Root\Interfaces\Models\Event as Contract; use Cone\Root\Traits\InteractsWithProxy; +use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -48,6 +50,14 @@ public static function getProxiedInterface(): string return Contract::class; } + /** + * Create a new factory instance for the model. + */ + protected static function newFactory(): Factory + { + return EventFactory::new(); + } + /** * Get the event target. */ diff --git a/src/Models/User.php b/src/Models/User.php index 9aaf0448..226691c8 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -70,6 +70,14 @@ public function authCodes(): HasMany return $this->hasMany(AuthCode::getProxiedClass())->active(); } + /** + * Get the triggered root events for the user. + */ + public function triggeredRootEvents(): HasMany + { + return $this->hasMany(Event::getProxiedClass()); + } + /** * Determine whether the object requires two factor authentitaction. */ diff --git a/tests/Models/EventTest.php b/tests/Models/EventTest.php new file mode 100644 index 00000000..653dd0df --- /dev/null +++ b/tests/Models/EventTest.php @@ -0,0 +1,33 @@ +user = User::factory()->create(); + + $this->event = Event::factory()->for($this->user)->for($this->user, 'target')->create(); + } + + public function test_an_event_belongs_to_a_user(): void + { + $this->assertTrue($this->event->user->is($this->user)); + } + + public function test_an_event_belongs_to_a_target(): void + { + $this->assertTrue($this->event->target->is($this->user)); + } +} diff --git a/tests/Models/UserTest.php b/tests/Models/UserTest.php index 7927a20c..532a7a57 100644 --- a/tests/Models/UserTest.php +++ b/tests/Models/UserTest.php @@ -3,6 +3,7 @@ namespace Cone\Root\Tests\Models; use Cone\Root\Models\AuthCode; +use Cone\Root\Models\Event; use Cone\Root\Models\Medium; use Cone\Root\Models\Notification; use Cone\Root\Tests\TestCase; @@ -54,4 +55,22 @@ public function test_a_user_has_auth_codes(): void $this->assertTrue($this->user->authCode->is($code)); } + + public function test_a_user_has_triggered_root_events(): void + { + $event = $this->user->triggeredRootEvents()->save( + Event::factory()->for($this->user, 'target')->make() + ); + + $this->assertTrue($this->user->triggeredRootEvents->contains($event)); + } + + public function test_a_user_has_root_events(): void + { + $event = $this->user->rootEvents()->save( + Event::factory()->for($this->user)->make() + ); + + $this->assertTrue($this->user->rootEvents->contains($event)); + } } diff --git a/tests/User.php b/tests/User.php index 31503854..b2c0fb57 100644 --- a/tests/User.php +++ b/tests/User.php @@ -3,6 +3,7 @@ namespace Cone\Root\Tests; use Cone\Root\Models\User as Model; +use Cone\Root\Traits\HasRootEvents; use Database\Factories\UserFactory; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -16,6 +17,7 @@ class User extends Model implements MustVerifyEmail { use HasFactory; + use HasRootEvents; use SoftDeletes; protected $guarded = [];