Skip to content

Commit

Permalink
add event tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jul 8, 2024
1 parent c0322d5 commit cbe75a9
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
27 changes: 27 additions & 0 deletions database/factories/EventFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Cone\Root\Database\Factories;

use Cone\Root\Models\Event;
use Illuminate\Database\Eloquent\Factories\Factory;

class EventFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<\Cone\Root\Models\Event>
*/
protected $model = Event::class;

/**
* Define the model's default state.
*/
public function definition(): array
{
return [
'action' => 'Create',
'payload' => [],
];
}
}
10 changes: 10 additions & 0 deletions src/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/Models/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Cone\Root\Tests\Models;

use Cone\Root\Models\Event;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

class EventTest extends TestCase
{
protected User $user;

protected Event $event;

public function setUp(): void
{
parent::setUp();

$this->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));
}
}
19 changes: 19 additions & 0 deletions tests/Models/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
2 changes: 2 additions & 0 deletions tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +17,7 @@
class User extends Model implements MustVerifyEmail
{
use HasFactory;
use HasRootEvents;
use SoftDeletes;

protected $guarded = [];
Expand Down

0 comments on commit cbe75a9

Please sign in to comment.