【问题标题】:How to test email in functional test (Symfony2)如何在功能测试中测试电子邮件(Symfony2)
【发布时间】:2013-06-24 15:19:59
【问题描述】:

我正在尝试在功能测试中测试电子邮件...

我的源码和example of the cookbook一样,

控制器:

public function sendEmailAction($name)
{
    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('send@example.com')
        ->setTo('recipient@example.com')
        ->setBody('You should see me from the profiler!')
    ;

    $this->get('mailer')->send($message);

    return $this->render(...);
}

还有测试:

// src/Acme/DemoBundle/Tests/Controller/MailControllerTest.php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MailControllerTest extends WebTestCase
{
    public function testMailIsSentAndContentIsOk()
    {
        $client = static::createClient();

        // Enable the profiler for the next request (it does nothing if the profiler is not available)
        $client->enableProfiler();

        $crawler = $client->request('POST', '/path/to/above/action');

        $mailCollector = $client->getProfile()->getCollector('swiftmailer');

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

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

        // Asserting e-mail data
        $this->assertInstanceOf('Swift_Message', $message);
        $this->assertEquals('Hello Email', $message->getSubject());
        $this->assertEquals('send@example.com', key($message->getFrom()));
        $this->assertEquals('recipient@example.com', key($message->getTo()));
        $this->assertEquals(
            'You should see me from the profiler!',
            $message->getBody()
        );
    }
}

但是我得到了这个错误:

PHP 致命错误:在 a 上调​​用成员函数 getCollector() 非对象

问题出在这一行:

$mailCollector = $client->getProfile()->getCollector('swiftmailer');

有什么想法吗?

【问题讨论】:

  • 我的回答有帮助吗?如果是,请投票/接受,否则请评论是否有遗漏或不起作用:-)
  • 我暂时无法检查...但别担心,我会保留您的解决方案,下周会试用 ;)

标签: symfony functional-testing swiftmailer


【解决方案1】:

抛出异常是因为如果未启用探查器,getProfile() 将返回 false。见here。

public function getProfile()
{
    if (!$this->kernel->getContainer()->has('profiler')) {
        return false;
    }

    return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
}

此外,enableProfiler()仅在使用服务容器注册时启用分析器,也就是启用。见here。

public function enableProfiler()
{
    if ($this->kernel->getContainer()->has('profiler')) {
        $this->profiler = true;
    }
}

现在您必须确保在测试环境中启用了探查器。 (通常应该是default setting)

config_test.yml

framework:
   profiler:
       enabled: true

您可以在测试中添加这样的内容:

$this->assertEquals($this->kernel->getContainer()->has('profiler'), true);

【讨论】:

  • 嗨@nifr,这对我真的很有帮助。谢谢
  • 读者注意:如果您想在提交表单后使用分析器,则必须禁用重定向。 $client->followRedirects(false); $client->enableProfiler(); $client->submit($form); /* use profiler */ if ($profile = $client->getProfile()) {…} /* open target page */ $crawler = $client->request('GET', $client->getResponse()->headers->get('location'));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
相关资源
最近更新 更多