adachsoft / mailbox-contract-tests
Reusable PHPUnit contract tests for mailbox implementations
Package info
gitlab.com/a.adach/mailbox-contract-tests
pkg:composer/adachsoft/mailbox-contract-tests
Requires
- php: ^8.3
- adachsoft/mailbox-contracts: ^0.2
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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, exclusivebefore, 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
MessageNotFoundExceptionfor unknown IDs. - Attachment content round-trips and
AttachmentNotFoundExceptionfor 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 withFLAG_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.
UnsupportedOperationExceptionfor 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.