Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:14:42+00:00 2026-06-17T12:14:42+00:00

I have created a very simple REST controller in Symony2 with Database insert/updates/deletes in

  • 0

I have created a very simple REST controller in Symony2 with Database insert/updates/deletes in the controller actions.

Is there a nice way to write unit/integration tests for these controller actions without polluting the production database? Do I have to work with different environments – or is there a proposed way from the framework vendor for this?

Current Controller Example:

public function postAction()
{
    $json = $this->getRequest()->getContent();
    $params = json_decode($json);
    $name = $params->name;
    $description = $params->description;

    $sandbox = new Sandbox();
    $sandbox->setName($name);
    $sandbox->setDescription($description);
    $em = $this->getDoctrine()->getManager();
    $em->persist($sandbox);
    $em->flush();

    $response = new Response('/sandbox/'.$sandbox->getId());
    $response->setStatusCode(201);
    return $response;
}

Current Test Example:

class SandboxControllerTest extends WebTestCase
{

    public function testRest()
    {
        $client = static::createClient();

        $crawler = $client->request('POST', '/service/sandbox', array(), array(), array(), json_encode(array('name' => 'TestMe', 'description' => 'TestDesc')));

        $this->assertEquals(
                201, $client->getResponse()->getStatusCode()
        );
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:14:44+00:00Added an answer on June 17, 2026 at 12:14 pm

    In my opinion you should definitely avoid change database with your tests.

    My favourite way to achieve this is inject entity manager mock inside a test client. For example:

    public function testRest()
    {
        // create entity manager mock
        $entityManagerMock = $this->getMockBuilder('Doctrine\ORM\EntityManager')
            ->setMethods(array('persist', 'flush'))
            ->disableOriginalConstructor()
            ->getMock();
    
        // now you can get some assertions if you want, eg.:
        $entityManagerMock->expects($this->once())
            ->method('flush');
    
        // next you need inject your mocked em into client's service container
        $client = static::createClient();
        $client->getContainer()->set('doctrine.orm.default_entity_manager', $entityManagerMock);
    
        // then you just do testing as usual
        $crawler = $client->request('POST', '/service/sandbox', array(), array(), array(), json_encode(array('name' => 'TestMe', 'description' => 'TestDesc')));
    
        $this->assertEquals(
                201, $client->getResponse()->getStatusCode()
        );
    }
    

    One thing with this solution which you should be aware is that you need inject your mocked service before each request. This is because the client reboots a kernel between each request (which means that the container is rebuild as well).

    edit:

    My GET approach in controller’s tests is that I can mock entity repositories and so on in order to stub every getting data from db but it’s a lot of work and it’s not very comfortable, so I prefer in this case (I mean only if we speak about controller’s test) actually getting real data from db. By real data I mean data created with doctrine fixtures. And as long as we don’t change database we can depend on the fixtures.

    But if we are speaking about changing data inside db (POST/PUT/DELETE methods) I always use mocks. If you’ll use em mock and set appropriate expectations on “perist” and “flush” methods, you can be sure that the data is correctly created/updated/deleted actually without any database’s modifications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a very simple controller for a REST services using the Spring
I have created a very simple CRUD that queries my database for Hotel details
I'm trying to create a very simple REST server. I just have a test
I have created very simple app with persistence context (hibernate as provider) to read
I have created a very simple sharepoint timer job. All i want it to
I have created a very simple GUI project in Qt as follows: main: #include
What I have created a very simple asp.net web service using .NET framework 3.5,
I am using SQL Server 2008 Enterprise. I have created a very simple test
I have created a GUI for starting a Thread which does something very simple.
I have created a very simple bookmarklet to submit the url of the site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.