Symfony 2.1 phpunit Swift_Message class not found

I have a problem with symfony and phpunit. Our facility is growing more and more. Therefore, we decided to activate process isolation for phpunit, because the server could no longer withstand the number of tests (not enough RAM). Since then, all tests sending mail no longer work. Can someone help us? The test below works fine if processIsolation = "false", but it fails if processIsolation = "true"

Versions:

  • Symfony 2.1.8-dev

  • phpunit 3.7.9

ERROR message

Project \ AppBundle \ Tests \ MailTest :: testSendMail PHPUnit_Framework_Exception: PHP Fatal error: class Swift_Message not found in /var/www/project/src/Project/AppBundle/Tests/MailTest.php

Test

public function testSendMail() { $client = static::createClient(); $message = \Swift_Message::newInstance(); $message->setFrom(' example@example.com ') ->setTo(' example@example.com ') ->setSubject('Subject') ->setBody('Hello World') ->setContentType('text/html'); $client->getContainer()->get('mailer')->send($message); $this->assertTrue(true); } 

phpunit.xml

 <phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" bootstrap="./autoload.php" processIsolation="true" stopOnFailure="false" syntaxCheck="false" > <testsuites> <testsuite name="Project Test Suite"> <directory>../src/Project/AppBundle/Tests/MailTest.php</directory> </testsuite> </testsuites> </phpunit> 
+4
source share
3 answers

This error is caused by the fact that Switfmailer determines the constant SWIFT_REQUIRED_LOADED after installing the autoloader. the autoloader checks this constant, and if it is already defined, refuses to install the autoloader. During the isolation process, PHPUnit ensures that all defined constants are overridden in the processes that spawn to run the test. Unfortunately, this means that SWIFT_REQUIRED_LOADED is determined during testing, so the autoloader does not load (see "swift_required.php" in the swiftmailer source folder). Note that if you turn it off, turn on the global state for the test through annotations, the tests will still not work, because the bootstrap file is passed to the test process through the global variable __PHPUNIT_BOOTSTRAP (see TestCaseMethod.tpl in the PHPUnit directory). Without globals, this global undefined was in the process of testing, and the boot file did not include punching tests.

The only work around which I found that the job is to replace the string $constants = PHPUnit_Util_GlobalState::getConstantsAsString(); with $constants = ''; in the execution method TestCase.php in the original PHPUnit distribution. If your code relies on global constants that must be defined before the test runs, this fix will obviously not work for you (class constants are a different story); unless the specified constants are redefined during your test cases.

+1
source

This is caused by a swiftmailer autoloader problem.

I ran into the same problem and fixed it in the swiftmailer forked repository.

Here you can find the PR to fix: https://github.com/swiftmailer/swiftmailer/pull/416 .

If you do not want to wait until this is merged, you can use my repository by adding it to your composer.json .

 "require": { "swiftmailer/swiftmailer": "dev-master as 5.0.x-dev" }, "repositories": [ { "type": "vcs", "url": "https://github.com/NickStemerdink/swiftmailer.git" } ] 

Strike>

UPDATE: PR was interconnected, so you can just upgrade to the latest version of swiftmailer.

+1
source

Does this work if you first create $ message and call static :: createClient?

0
source

All Articles