Tom
Tom

Reputation: 2873

Symfony2 Unit Testing with Doctrine Entities

I'm apparently missing something really obvious, but this drives me crazy:

In order to test my code, I need to put my entities into defined states. And it simply doesn't work. It completely ignores any changes I make, like for example this one:

    $this->test_character->setLocation(null);

    $crawler = $this->client->request('GET', '/en/character/start');
    $this->assertTrue($this->client->getResponse()->isSuccessful(), "start page failed to load");
    $this->assertGreaterThan(0, $crawler->filter('html:contains("Character Placement")')->count(), 'start page content failure');

debugging this test shows that it fails because Location is NOT, in fact, set to null. Adding a flush() doesn't change anything, so that's not the issue. My best guess is that it changes it only on the test client, but not on the backend that generates the pages but that leaves the question: How the f*** do I put my entities into a defined state in order to test them ?

Upvotes: 0

Views: 2858

Answers (1)

Rob Hogan
Rob Hogan

Reputation: 2624

You're on the right lines - the kernel which handles your test request is an isolated instance of your app. It won't be able to access the entity you've presumably loaded into test_character unless you persist and flush it to the database before making the request.

Have you considered using Fixtures? If all you're doing is initialising some entities ready to test a request/response against, that's the correct way to go about it. You should never need to be working directly with entities inside your functional tests.

You may also want to look into Liip functional test bundle to automate setting up your test database and test fixtures.

Upvotes: 1

Related Questions