death-notifier/src/test/php/com/fwdekker/deathnotifier/mailer/EmailQueueTest.php

133 lines
5.0 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier\mailer;
use com\fwdekker\deathnotifier\Database;
use com\fwdekker\deathnotifier\DatabaseTestCase;
/**
* Unit tests for {@see Config}.
*/
class EmailQueueTest extends DatabaseTestCase
{
public function setUpWithDatabase(Database $database): void
{
parent::setUpWithDatabase($database);
$this->email_queue = new EmailQueue($database);
}
public function test_get_queue_initially_empty(): void
{
self::assertEmpty($this->email_queue->get_queue());
}
public function test_get_queue_returns_added_emails(): void
{
$this->email_queue->enqueue([
new TestEmail("key1", "recipient1", "subject1", "body1"),
new TestEmail("key2", "recipient2", "subject2", "body2"),
]);
self::assertSame(
[
["type_key" => "key1", "recipient" => "recipient1", "subject" => "subject1", "body" => "body1"],
["type_key" => "key2", "recipient" => "recipient2", "subject" => "subject2", "body" => "body2"],
],
$this->email_queue->get_queue()
);
}
public function test_enqueue_adds_emails(): void
{
$this->email_queue->enqueue([new TestEmail(type_key: "key1"), new TestEmail(type_key: "key2")]);
self::assertSame(["key1", "key2"], array_column($this->email_queue->get_queue(), "type_key"));
}
public function test_enqueue_appends_emails(): void
{
$this->email_queue->enqueue([new TestEmail(type_key: "key1"), new TestEmail(type_key: "key2")]);
$this->email_queue->enqueue([new TestEmail(type_key: "key3"), new TestEmail(type_key: "key4")]);
self::assertSame(["key1", "key2", "key3", "key4"], array_column($this->email_queue->get_queue(), "type_key"));
}
public function test_enqueue_appends_email_if_only_type_key_is_different(): void
{
$this->email_queue->enqueue([new TestEmail(type_key: "key1", recipient: "recipient")]);
$this->email_queue->enqueue([new TestEmail(type_key: "key2", recipient: "recipient")]);
$queue = $this->email_queue->get_queue();
self::assertSame(["key1", "key2"], array_column($queue, "type_key"));
self::assertSame(["recipient", "recipient"], array_column($queue, "recipient"));
}
public function test_enqueue_appends_email_if_only_recipient_is_different(): void
{
$this->email_queue->enqueue([new TestEmail(type_key: "key", recipient: "recipient1")]);
$this->email_queue->enqueue([new TestEmail(type_key: "key", recipient: "recipient2")]);
$queue = $this->email_queue->get_queue();
self::assertSame(["key", "key"], array_column($queue, "type_key"));
self::assertSame(["recipient1", "recipient2"], array_column($queue, "recipient"));
}
public function test_enqueue_replaces_email_if_both_type_key_and_recipient_are_same(): void
{
$this->email_queue->enqueue([new TestEmail(type_key: "key", recipient: "recipient", subject: "old_subject")]);
$this->email_queue->enqueue([new TestEmail(type_key: "key", recipient: "recipient", subject: "new_subject")]);
$queue = $this->email_queue->get_queue();
self::assertSame(["key"], array_column($queue, "type_key"));
self::assertSame(["recipient"], array_column($queue, "recipient"));
self::assertSame(["new_subject"], array_column($queue, "subject"));
}
public function test_unqueue_does_not_remove_if_both_type_key_and_recipient_are_different(): void
{
$this->email_queue->enqueue([new TestEmail(type_key: "key1", recipient: "recipient1")]);
$this->email_queue->unqueue([["type_key" => "key2", "recipient" => "recipient2"]]);
$queue = $this->email_queue->get_queue();
self::assertSame(["key1"], array_column($queue, "type_key"));
self::assertSame(["recipient1"], array_column($queue, "recipient"));
}
public function test_unqueue_does_not_remove_if_type_key_is_different(): void
{
$this->email_queue->enqueue([new TestEmail(type_key: "key1", recipient: "recipient")]);
$this->email_queue->unqueue([["type_key" => "key2", "recipient" => "recipient"]]);
self::assertSame(["key1"], array_column($this->email_queue->get_queue(), "type_key"));
}
public function test_unqueue_does_not_remove_if_recipient_is_different(): void
{
$this->email_queue->enqueue([new TestEmail(type_key: "key", recipient: "recipient1")]);
$this->email_queue->unqueue([["type_key" => "key", "recipient" => "recipient2"]]);
self::assertSame(["recipient1"], array_column($this->email_queue->get_queue(), "recipient"));
}
public function test_unqueue_removes_email_with_same_key_and_recipient(): void
{
$this->email_queue->enqueue([new TestEmail(type_key: "key", recipient: "recipient")]);
$this->email_queue->unqueue([["type_key" => "key", "recipient" => "recipient"]]);
self::assertEmpty($this->email_queue->get_queue());
}
}