From e4221bc6060ed7bc301b0df2b79557079f0a1cd6 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 4 Sep 2025 17:11:25 -0400 Subject: [PATCH] WIP fixing tests to work with new relation --- app/Models/WebhookEvent.php | 5 ++++- .../Factories/WebhookConfigurationFactory.php | 1 - database/Factories/WebhookEventFactory.php | 18 ++++++++++++++++++ tests/Unit/Webhooks/ProcessWebhooksTest.php | 10 ++++++---- 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 database/Factories/WebhookEventFactory.php diff --git a/app/Models/WebhookEvent.php b/app/Models/WebhookEvent.php index 7905da8a4..296f7709e 100644 --- a/app/Models/WebhookEvent.php +++ b/app/Models/WebhookEvent.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; @@ -10,13 +11,15 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany; */ class WebhookEvent extends Model { + use HasFactory; + public $timestamps = false; protected $fillable = [ 'name', ]; - public function webhookConfigurationEvent(): BelongsToMany + public function configurations(): BelongsToMany { return $this->belongsToMany(WebhookConfiguration::class, 'webhook_configurations_events', 'event_id', 'configuration_id'); } diff --git a/database/Factories/WebhookConfigurationFactory.php b/database/Factories/WebhookConfigurationFactory.php index e1a70954e..d173f2577 100644 --- a/database/Factories/WebhookConfigurationFactory.php +++ b/database/Factories/WebhookConfigurationFactory.php @@ -14,7 +14,6 @@ class WebhookConfigurationFactory extends Factory return [ 'endpoint' => fake()->url(), 'description' => fake()->sentence(), - 'events' => [], ]; } } diff --git a/database/Factories/WebhookEventFactory.php b/database/Factories/WebhookEventFactory.php new file mode 100644 index 000000000..889359d53 --- /dev/null +++ b/database/Factories/WebhookEventFactory.php @@ -0,0 +1,18 @@ + fake()->word, + ]; + } +} diff --git a/tests/Unit/Webhooks/ProcessWebhooksTest.php b/tests/Unit/Webhooks/ProcessWebhooksTest.php index b6d458b97..05bcf1186 100644 --- a/tests/Unit/Webhooks/ProcessWebhooksTest.php +++ b/tests/Unit/Webhooks/ProcessWebhooksTest.php @@ -7,6 +7,7 @@ use App\Jobs\ProcessWebhook; use App\Models\Server; use App\Models\Webhook; use App\Models\WebhookConfiguration; +use App\Models\WebhookEvent; use App\Tests\TestCase; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Illuminate\Http\Client\Request; @@ -26,9 +27,10 @@ class ProcessWebhooksTest extends TestCase public function test_it_sends_a_single_webhook(): void { - $webhook = WebhookConfiguration::factory()->create([ - 'events' => [$eventName = 'eloquent.created: '.Server::class], - ]); + $eventName = 'eloquent.created: '.Server::class; + $webhook = WebhookConfiguration::factory() + ->has(WebhookEvent::factory()->state(['name' => $eventName]), 'webhookEvents') + ->create(['events' => []]); Http::fake([$webhook->endpoint => Http::response()]); @@ -67,7 +69,7 @@ class ProcessWebhooksTest extends TestCase [$data], ); - $this->assertCount(1, cache()->get("webhooks.$eventName")); + $this->assertCount(1, cache()->get("webhooks.$eventName", [])); $this->assertEquals($webhook->id, cache()->get("webhooks.$eventName")->first()->id); Http::assertSentCount(1);