2

I'm testing email sending in my symfony2 application, I have custom environment that is properly set with email settings and it's able to actually sent emails. I've created environment test_emails and I want to run my phpunit tests with it.

phpunit doesn't seem to have this functionality, so my only option is probably create custom bootstrap file, but I can't find anywhere how to do it.

4 Answers 4

1

The doc about Environments says:

The test environment is used when writing functional tests and is not accessible in the browser directly via a front controller. In other words, unlike the other environments, there is no app_test.php front controller file.

So when testing emails you're supposed to use functional test which is well described in How to Test that an Email is Sent in a Functional Test.

If you want to write a custom bootstrap with some simple logic see: How to Customize the Bootstrap Process before Running Tests

Maybe you can actually customize what environment the test harness is using by creating a custom test case that boots the kernel just like here How can I test a service in symfony2? and then passing an array of options to bootKernel() method. See:

Sign up to request clarification or add additional context in comments.

Comments

1

If your using Symfonys WebtestCase you can set the environment as option:

$client = static::createClient(array('environment' => 'test-emails'));

Comments

0

I've ended up creating command that sends all emails I wanted and I can easily switch between environments.

Example: php app/console app:emails:test-emails --env=dev

Comments

0

Use different phpunit.xml configuration files for each environment.

.
├── phpunit_test_emails.xml
└── phpunit_other_env.xml

Run the tests with specific configuration:

$ phpunit -c phpunit_test_emails.xml

You can set different PHP configurations in the XML configuration file.

You create a custom bootstrap via the bootstrap attribute where additional initialisations can be done if needed.

<phpunit bootstrap="test/bootstrap.php">

    <!-- ... -->

    <php>
        <includePath>.</includePath>
        <ini name="foo" value="bar"/>
        <const name="foo" value="bar"/>
        <var name="foo" value="bar"/>
        <env name="foo" value="bar"/>
        <post name="foo" value="bar"/>
        <get name="foo" value="bar"/>
        <cookie name="foo" value="bar"/>
        <server name="foo" value="bar"/>
        <files name="foo" value="bar"/>
        <request name="foo" value="bar"/>
    </php>

</phpunit>

See the PHPUnit Manual for more details.

2 Comments

Yes, but you can't change your symfony environment, this will always run on test env.
You can specify a PHPUnit bootstrap via the bootstrap attribute (see edited answer). But perhaps I misunderstand what is that your trying to do. If what you want is to specify the environment for $kernel = new AppKernel('prod', false); then define an env constant or variables in the test bootstrap and then in your application bootstrap do an if TEST_CONSTANT then do this else do that....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.