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

Getting this exception in Symfony 2.5.5 using Swiftmailer 5.3.0. I definitely follow the cookbook example. The error is caused when calling MessageDataCollector#getMessages() :

 // Check that an e-mail was sent $this->assertEquals(1, $mailCollector->getMessageCount()); $collectedMessages = $mailCollector->getMessages(); $message = $collectedMessages[0]; 

A message counter assertion also fails with a value of zero.

As far as I can tell, the collector does not carry out any actual collection in action. Any ideas?

+4
source share
3 answers

I ran into the same problem after applying the kriswallsmith trend optimization trick . I could see the result of sending mail to the web profiler when starting the development version, but could not get the data in the test environment.

After applying Chris's trick, I noticed that the swiftmailer.mailer.default.plugin.messagelogger service was not registered in the container during the test, therefore the collect () method of the MessageDataCollector class did not register data for sending mail. This is why it was not possible to get information from the swiftmailer collector.

The solution is to not override the initializeContainer () method in AppKernel.php or override it, but make sure the messagelogger service is available for test cases sending emails.

+2
source

In my case, I had several mailers in the config.yml file, and the same problem was caused by 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 

Correct call:

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

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

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

 swiftmailer: logging: true 

The default logging config value is 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 of the following queries.

Above the register, the registrar is registered.

0
source

Source: https://habr.com/ru/post/1207402/


All Articles