WIP fixing tests to work with new relation

This commit is contained in:
Vehikl 2025-09-04 17:11:25 -04:00
parent 00889a8004
commit e4221bc606
4 changed files with 28 additions and 6 deletions

View File

@ -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');
}

View File

@ -14,7 +14,6 @@ class WebhookConfigurationFactory extends Factory
return [
'endpoint' => fake()->url(),
'description' => fake()->sentence(),
'events' => [],
];
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace Database\Factories;
use App\Models\WebhookEvent;
use Illuminate\Database\Eloquent\Factories\Factory;
class WebhookEventFactory extends Factory
{
protected $model = WebhookEvent::class;
public function definition(): array
{
return [
'name' => fake()->word,
];
}
}

View File

@ -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);