2

I'm trying to setup a simple WebTestCase which makes a GET request to the given URL using Symfony 4 (and "phpunit/phpunit": "^6.5"). However, the test fails with:

Failed to start the session because headers have already been sent by \vendor\phpunit\phpunit\src\Util\Printer.php; at line 112. (500 Internal Server Error)

Test Class:

class ControllerTest extends WebTestCase
{
    public function testGetArticles()
    {
        $client = static::createClient();
        $client->request('GET', '/articles');
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
    }
}

Controller (which contains articles route:

/** @Route(path="articles", name="article_list") */  
public function article_list(Request $request)
{
    return $this->render("article/list.html.twig", array(
        "articles" => $this->entityManager->getRepository("App:Article")->getArticles()
    ));
}

framework.yaml:

framework:
    secret: '%env(APP_SECRET)%'
    csrf_protection: ~
    session:
        # With this config, PHP's native session handling is used
        handler_id: ~

These tests were running fine in Symfony 3, in SF 4 not. What is wrong with this test or configuration?

2
  • 1
    See: github.com/sebastianbergmann/phpunit/issues/720 Commented Jan 1, 2018 at 19:30
  • Ok, thanks. But I didn't get why this works with SF 3 but not with SF 4 without using e.g. the @runInSeparateProcess annotation. This error occurs on all Tests for me (using SF 4), so I've to annotate each test with @runInSeparateProcess ? Feels some kind of wrong to me :/ Commented Jan 1, 2018 at 19:37

1 Answer 1

10

You must uncomment session section in config\Packages\test\framework.yaml.

session:
        storage_id: session.storage.mock_file
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, this works fine! Is this somewhere properly documented?
1a) This is documented here: symfony.com/doc/4.4/reference/configuration/… 1b) Best info on what this does I could find was in the repo: github.com/symfony/symfony/blob/… 2a) THIS IS DEPRECATED as of 5.3. Use storage_factory_id: session.storage.factory.mock_file 2b) See: symfony.com/doc/current/reference/configuration/…

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.