Skip to content

Commit

Permalink
Merge pull request #110 from mettle/issue-116/delete-subscriber-faile…
Browse files Browse the repository at this point in the history
…d-messages

Delete failed messages on subscriber deletion
  • Loading branch information
JonoB authored Mar 8, 2021
2 parents 23cdcc5 + 960b56c commit dd6c29d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ protected static function boot(): void
static::creating(function ($model) {
$model->hash = $model->hash ?: Uuid::uuid4()->toString();
});

static::deleting(function (self $message) {
$message->failures()->delete();
});
}

public function failures(): HasMany
Expand Down
4 changes: 3 additions & 1 deletion src/Models/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ function ($model) {
$model->hash = Uuid::uuid4()->toString();
}
);

static::deleting(
function (self $subscriber) {
$subscriber->tags()->detach();
$subscriber->messages()->each(static function (Message $message) {
$message->failures()->delete();
});
$subscriber->messages()->delete();
}
);
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Models/MessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Models;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Sendportal\Base\Models\Message;
use Sendportal\Base\Models\MessageFailure;
use Tests\TestCase;

class MessageTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function deleting_a_message_also_deletes_its_associated_failures()
{
// given
$message = Message::factory()->create();
$message->failures()->create();

// when
$message->delete();

// then
static::assertCount(0, Message::all());
static::assertCount(0, MessageFailure::all());
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Models/SubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Sendportal\Base\Models\Campaign;
use Sendportal\Base\Models\Message;
use Sendportal\Base\Models\MessageFailure;
use Sendportal\Base\Models\Subscriber;
use Tests\TestCase;

Expand Down Expand Up @@ -39,6 +40,25 @@ public function it_has_many_messages()
static::assertEquals($campaignTwo->id, $messageTwo->source_id);
}

/** @test */
public function deleting_a_subscriber_also_deletes_its_messages_and_any_failures_associated_to_them()
{
// given
$subscriber = Subscriber::factory()->create();
$message = Message::factory()->create([
'subscriber_id' => $subscriber->id,
]);
$message->failures()->create();

// when
$subscriber->delete();

// then
static::assertCount(0, Subscriber::all());
static::assertCount(0, Message::all());
static::assertCount(0, MessageFailure::all());
}

/**
* @param Subscriber $subscriber
*
Expand Down

0 comments on commit dd6c29d

Please sign in to comment.