Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement testing system #8

Open
stevebauman opened this issue Feb 13, 2025 · 2 comments
Open

Implement testing system #8

stevebauman opened this issue Feb 13, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@stevebauman
Copy link
Member

Something like this:

namespace DirectoryTree\ImapEngine;

class Mailbox
{
    // ...

    public static function fake(array $config = []): static
    {
        $mailbox = new static($config);

        // Inject the fake connection instance
        $mailbox->connection = new FakeImapConnection();
        
        return $mailbox;
    }
}

Example fake connection:

<?php

namespace DirectoryTree\ImapEngine\Connection;

use DirectoryTree\ImapEngine\Folder;

class FakeImapConnection implements ConnectionInterface
{
    protected bool $connected = false;
    
    // "Global" in-memory store: array of [folderPath => [uid => messageData]]
    protected array $folders = [];

    public function connect(string $host, int $port, array $options = []): void
    {
        $this->connected = true;
    }

    public function connected(): bool
    {
        return $this->connected;
    }

    public function disconnect(): void
    {
        $this->connected = false;
    }

    public function login(string $username, string $password): void
    {
        // Do nothing or store internally for reference...
    }

    public function capability(): UntaggedResponse
    {
        // Return mocked capabilities..
        return $this->capabilities;
    }

    public function select(string $folder = 'INBOX'): ResponseCollection
    {
        // Track the “selected” folder in an internal property...
    }
    
    // Here is where you do the actual "faking":
    public function append(string $folder, string $message, ?array $flags = null): TaggedResponse
    {
        if (! isset($this->folders[$folder])) {
            $this->folders[$folder] = [];
        }
        
        $uid = $this->generateUid();
        
        $this->folders[$folder][$uid] = [
            'message' => $message,
            'flags' => $flags,
            // anything else to store...
        ];
        
        return $uid;
    }
    
    // ...
}
@stevebauman stevebauman added the enhancement New feature or request label Feb 13, 2025
@chrootchad
Copy link

chrootchad commented Feb 28, 2025

Hi @stevebauman ,

Fantastic project - amazing stuff!

Would be super handy to be able to connect to a local .mbox file using ImapEngine (eg. from a google takeout gmail export)

Native php imap_open() equivalent:

$mbox = imap_open('mail-export.mbox','','');

Apologies if this has already been implemented or this is best suited as its own feature request. 👍

@stevebauman
Copy link
Member Author

Hey @chrootchad, thanks!

I actually didn't know that was a thing. I'm also not sure if that would be possible. ImapEngine connects to IMAP servers using raw TCP streams. It doesn't have any capability of reading files right now.

Are you sure that PHP supports this with imap_open? I can't find any documentation indicating such. If so, link it here so I can take a look 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants