Search by

adachsoft / mailbox-contract-tests

Arkadiusz Adach

Reusable PHPUnit contract tests for mailbox implementations

Package info

gitlab.com/a.adach/mailbox-contract-tests

Issues

pkg:composer/adachsoft/mailbox-contract-tests

Statistics

Installs: 5

Dependents: 1

Suggesters: 0

Stars: 0

v0.2.0 2026-11-25 09:17 UTC

This package is auto-updated.

Last update: 2026-11-25 08:18:09 UTC


README

adachsoft/mailbox-contract-tests provides a reusable PHPUnit contract suite for implementations of adachsoft/mailbox-contracts. Run the same behavioral checks against IMAP/SMTP, Gmail API, in-memory, and other mailbox providers to verify that they expose consistent behavior.

Installation

Install the suite as a development dependency in an implementation package:

composer require --dev adachsoft/mailbox-contract-tests

The abstract test case extends PHPUnit's TestCase, so the consuming test project must have PHPUnit installed.

Usage

Writing an implementation test

Extend AbstractMailboxContractTest and provide a fresh, empty mailbox plus a provider-specific way to seed an incoming message. The fixture operations below are intentionally provider-specific; for example, an IMAP implementation can append a message and a Gmail implementation can use messages.insert.

use AdachSoft\MailboxContractTests\Contract\AbstractMailboxContractTest;
use AdachSoft\MailboxContractTests\Contract\MessageSeed;
use AdachSoft\MailboxContracts\Model\MessageId;
use AdachSoft\MailboxContracts\Service\MailboxInterface;

final class ImapMailboxContractTest extends AbstractMailboxContractTest
{
    protected function createMailbox(): MailboxInterface
    {
        return $this->providerFixture->createEmptyMailbox();
    }

    protected function seedMessage(MailboxInterface $mailbox, MessageSeed $seed): MessageId
    {
        return $this->providerFixture->appendIncomingMessage($mailbox, $seed);
    }
}

MessageSeed::simple($subject, $receivedAt, $seen) creates a plain-text fixture using sender@example.test and recipient@example.test. Construct MessageSeed directly when a test needs custom recipients, bodies, folders, or attachment metadata. The default sender used by the suite can be changed by overriding senderAddress().

The mailbox hook must return a new empty mailbox for each test. Seeded provider identifiers are opaque and should be returned unchanged from seedMessage(). The default unknown-message identifier can be customized by overriding unknownMessageId() if it could collide with a provider-generated ID.

Capability-based skipping

Optional behaviors are checked against MailboxInterface::capabilities(). Contract tests for a missing optional capability are skipped rather than failing; operations that advertise no support must still throw UnsupportedOperationException. Implementations should include tests for the capabilities they claim to support. If an implementation's fixture cannot seed attachment content, override supportsAttachmentSeeding() to return false so attachment-content tests are skipped. The default is true.

Requirements

  • PHP 8.3 or newer.
  • adachsoft/mailbox-contracts:^0.1.
  • PHPUnit 13.4 or newer.

Covered behaviors

The suite checks:

  • Search ordering, sender and subject filters, inclusive since, exclusive before, unread and attachment filters, the INBOX special folder, and cursor paging without duplicates or missing messages.
  • Message retrieval of subject, sender, recipients, and text/HTML bodies, plus MessageNotFoundException for unknown IDs.
  • Attachment content round-trips and AttachmentNotFoundException for unknown attachment IDs.
  • Seen/unseen state changes reflected by message retrieval and unread search.
  • Moving messages to a folder, trashing messages out of INBOX search, and adding/removing labels.
  • Change tracking for added messages (CHANGE_TRACKING), flag-changed messages (only with FLAG_CHANGE_TRACKING), and recovery from an unknown synchronization token.
  • Moving: the moved message is located by searching the target folder, because message IDs may change after a move (IMAP).
  • Sending messages, including a non-empty RFC Message-ID and retrieval when the provider returns a stored message ID.
  • UnsupportedOperationException for operations whose optional capability is not advertised.

Tests use deterministic fixture dates and do not sleep or perform provider network operations from this package itself.