LogicException: Missing default data in Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector

泪湿孤枕 提交于 2019-12-12 12:25:29

问题


Getting this exception in Symfony 2.5.5 with Swiftmailer 5.3.0. I'm following the cookbook example exactly. The error is thrown when calling MessageDataCollector#getMessages():

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

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

The message count assertion is also failing with a value of zero.

As far as I can tell the collector is failing to do any actual collecting in the action. Any ideas?


回答1:


I encountered the same problem after I applied kriswallsmith's test optimization trick. I could see the result of the mail sending in the web profiler when running the development version, but couldn't get the data in the testing environment.

After applying Kris' trick I noticed that the swiftmailer.mailer.default.plugin.messagelogger service was not registered with the container during the test, so the collect() method of the MessageDataCollector class did not log the data for the mail sending. This is why it wasn't possible to retrieve information from the swiftmailer collector.

The solution is either not to overrride the initializeContainer() method in AppKernel.php or to override it, but making sure messagelogger service is available for test cases that send mails.




回答2:


@codeBetyar is right - problem occurs when swiftmailer.mailer.default.plugin.messagelogger is not registered in container.

But instead of getting rid of the optimization trick - you just need to update swiftmailer configuration for test environment

swiftmailer:
    logging: true

The thing is logging config value by default equals to kernel.debug (https://github.com/symfony/swiftmailer-bundle/blob/master/DependencyInjection/Configuration.php#L121) which (thanks to the trick) is true for the first test request, and false - for all following requests.

Above config forces logger being registered.




回答3:


In my case I had multiple mailers in the config.yml file and the same problem was due to a missing parameter in the getMessages() call.

My config.yml file:

swiftmailer:
    default_mailer: default_mailer
    mailers:
        default_mailer:
          # some configuration
        another_mailer:
          # some configuration

The correct call was:

$mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
$collectedMessages = $mailCollector->getMessages('default_mailer'); // please note the 'default_mailer' argument
$messagesCount = $mailCollector->getMessageCount('default_mailer')


来源:https://stackoverflow.com/questions/26270493/logicexception-missing-default-data-in-symfony-bundle-swiftmailerbundle-datacol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!