mardi 28 juillet 2015

Get content of email sent during command tests

During my tests I call some commands which send emails. I can display the number of emails sent with the following command:

$output->writeln(
    $spool->flushQueue(
        $this->getContainer()->get('swiftmailer.transport.real')
    )
);

The Symfony2 documentation explains how to get email content by using the profiler during a Web test (also explained here on Stack Overflow), but I don't know how to do the same thing when there is no Web request.

I used the code provided in these links:

public function tesEmailCommand()
{
    // load data fixtures

    // http://ift.tt/1silozM
    $client = static::createClient();
    // Enable the profiler for the next request (it does nothing if the profiler is not available)
    $client->enableProfiler();

    /** @var \Symfony\Bundle\FrameworkBundle\Console\Application $application */
    // inherit from the parent class
    $application = clone $this->application;

    $application->add(new EmailCommand());
    $command = $application->find('acme:emails');
    $commandTester = new CommandTester($command);

    $commandTester->execute(array(
        'command' => 'acme:emails'
    ));

    $display = $commandTester->getDisplay();

    $this->assertContains('foo', $display);

    // http://ift.tt/1silozM
    $mailCollector = $client->getProfile()->getCollector('swiftmailer');

    // Check that an email was sent
    $this->assertEquals(1, $mailCollector->getMessageCount());

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

    // Asserting email data
    $this->assertInstanceOf('Swift_Message', $message);
    $this->assertEquals(
        'You should see me from the profiler!',
        $message->getBody()
    );
}

It returns this error:

Argument 1 passed to Symfony\Component\HttpKernel\Profiler\Profiler::loadProfileFromResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given, called in .../vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php on line 72 and defined .../vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Profiler/Profiler.php:81 .../vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:72 .../src/ACME/MyBundle/Tests/Command/ResumeEmailsTest.php:94

The error seems logical because there is no response since there's no request.

I use Symfony 2.3.30.

Aucun commentaire:

Enregistrer un commentaire