PHP code example of rpkamp / mailhog-behat-extension

1. Go to this page and download the library: Download rpkamp/mailhog-behat-extension library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rpkamp / mailhog-behat-extension example snippets



declare(strict_types=1);

use rpkamp\Mailhog\MailhogClient;
use rpkamp\Behat\MailhogExtension\Context\MailhogAwareContext;

class FeatureContext implements MailhogAwareContext
{
    private $mailhog;
    
    public function setMailhog(MailhogClient $client)
    {
         $this->mailhog = $client;
    }
}


declare(strict_types=1);

use rpkamp\Behat\MailhogExtension\Context\MailhogAwareContext;

class FeatureContext implements MailhogAwareContext
{
    // implement setMailhog as above
    
    /**
     * @Then /^there should be (\d+) email(?:s) in my inbox$/
     */
    public function thereShouldBeEmailInMyInbox(int $numEmails)
    {
        if ($numEmails !== $this->mailhog->getNumberOfMessages()) {
            throw new Exception('Unexpected number of messages.');
        }
    }
}



use rpkamp\Behat\MailhogExtension\Context\OpenedEmailStorageAwareContext;
use rpkamp\Behat\MailhogExtension\Service\OpenedEmailStorage;

class FeatureContext implements OpenedEmailStorageAwareContext
{
    /**
     * @var OpenedEmailStorage
     */
    private $storage;

    public function setOpenedEmailStorage(OpenedEmailStorage $storage)
    {
        $this->openedEmailStorage = $storage;
    }

    /**
     * @Then ^I do something with the opened email$
     */
    public function iDoSomethingWithTheOpenedEmail(): void
    {
        if (!$this->storage->hasOpenedEmail()) {
            throw new RuntimeException('No email opened, unable to do something!');
        }

        /** @var \rpkamp\Mailhog\Message\Message $openedEmail */
        $openedEmail = $this->storage->getOpenedEmail();

        // do stuff with $openedEmail
    }
}