xm/mailer-test-bundle

Allows for easy testing of the mailer/emails when using the SwiftMailerBundle.

0.1.1 2018-02-26 02:13 UTC

This package is auto-updated.

Last update: 2024-12-15 15:12:24 UTC


README

Allows for easy testing of the mailer/emails when using the SwiftMailerBundle.

Installation

Step 1: Download the Bundle

This package is not on Packagist, so the repository will need to be added manually in composer.json

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ php composer.phar require --dev xm/mailer-test-bundle

This command requires Composer.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        // ...
        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            // ...
            $bundles[] = new XM\MailerTestBundle\XMMailerTestBundle();
        }
    }
}

Usage

Below is how this bundle would be typically used.

<?php

namespace Tests\AppBundle\EventSubscriber;

use Symfony\Component\EventDispatcher\GenericEvent;
use XM\MailerTestBundle\Test\MailerTestCase;

class EventSubscriberTest extends MailerTestCase
{
    public function testEvent()
    {
        $mailerPlugin = $this->getMailerPlugin();

        $event = new GenericEvent();

        $this->container->get('app.listener')
            ->onEvent($event);

        // make sure email was sent
        $this->assertNotNull($mailerPlugin->beforeSendEvent);
        $this->assertNotNull($mailerPlugin->sendEvent);

        // grab message and make sure it matches what we wanted
        $msg = $mailerPlugin->sendEvent->getMessage();

        $expected = 'Expected Subject';
        $this->assertEquals($expected, $msg->getSubject());

        $this->assertContains('Expected part of email body', $msg->getBody());
    }
}