Search code examples
phpsymfonyfunctional-testingswiftmailer

Incorrect e-mail recipients in functional test


A functional test of a process that sends an e-mail with two recipients (one To:, one Cc:) incorrectly claims same recipient as both To: and Cc.

In dev environment the process indicated in the test below sends an e-mail to [email protected] with a Cc to [email protected]. (Stepping through the process in test debug mode confirms correct recipients.) Yet the test below fails. Replacing the To: recipient with [email protected] allows the test to fully succeed.

console output

Output with print_r($recipient); in mailer process during test.

PHPUnit 4.3.5 by Sebastian Bergmann.

Configuration read from G:\Documents\workspace\volunteer\app\phpunit.xml.dist

FArray
(
    [0] => [email protected]
)


Time: 4.5 seconds, Memory: 26.25Mb

There was 1 failure:

1) Truckee\MatchingBundle\Tests\Controller\AdminMailerTest::testActivateOrganizationEmail
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'[email protected]'
+'[email protected]'

config_test.yml

swiftmailer:
    logging: true

config_dev.yml

swiftmailer:
#    disable_delivery:  true
#    delivery_address: [email protected]

test

public function testActivateOrganizationEmail()
{
    $crawler = $this->login('admin');
    $this->client->followRedirects(false);
    $link = $crawler->selectLink('Accept organization')->link();
    $crawler = $this->client->click($link);
    $mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
    $this->assertEquals(1, $mailCollector->getMessageCount());

    $collectedMessages = $mailCollector->getMessages();
    $message = $collectedMessages[0];

    $this->assertEquals('[email protected]', key($message->getTo()));
    $this->assertEquals('[email protected]', key($message->getCc()));
}

Edit - the process.

In this instance, there is a single element [email protected] in $to and a single element [email protected] in $this->adminRecipients.

public function activateOrgMail($organization, $to)
{
    $recipient = [];
    foreach ($to as $user) {
        $recipient[] = $this->getAddressee($user);
    }
    $cc = $this->adminRecipients();
    if (!empty($recipient)) {
        $message = \Swift_Message::newInstance()
                ->setSubject('Organization now active')
                ->setFrom($this->parameters['address'])
                ->setTo($recipient[0])
                ->setCc($cc)
                ->setContentType('text/html')
                ->setBody(
                $this->twig->render(
                        'activated_org', array(
                    'organization' => $organization,
                    'recipient' => $recipient,
                        ), 'text/html'
                )
                )
        ;

        return $this->mailer->send($message);
    }
    else {
        return 0;
    }
}

    private function adminRecipients()
    {
        $em = $this->em;
        $admins = $em->getRepository("TruckeeMatchingBundle:Admin")->findBy(['locked' => false]);
        $adminEmail = [];
        foreach ($admins as $admin) {
            $email = $admin->getEmail();
            $adminEmail[] = $email;
        }

        return $adminEmail;
    }

Solution

  • ARRRGGHHH! It's the ever-popular uncleared cache problem. In this case, however, one needs to clear the test cache! Once cleared, tests succeed as expected.