How to Mock EntityManager in a serviceTest with Symfony?
<p>Sorry my question is very long. </p>

<p><strong>How to mock entityManager in a service test extending KernelTestCase ?</strong></p>

<p>Now, the explanation and my tests...</p>

<p>I am using Symfony3.2. My Application is standard. I have some Controller and I use WebTestCase to test them.</p>

<p>Generaly, my Controller verify parameters, call a service/a manager, handle some variables and push them to view and my test are pretty simple in test extending <strong>WebTestCase</strong>.</p>

<pre><code>/**
 * Test New Game Action
 */
public function testFooAction(){
    //We mock the Service
    $fooService = $this
        -&gt;getMockBuilder(GameService::class)
        -&gt;disableOriginalConstructor()
        -&gt;getMock();
    $fooService-&gt;expects(self::once())
        -&gt;method('barMethod')
        -&gt;willReturn($result);

    //We create the client
    $client = static::createClient();
    $container = $client-&gt;getContainer();

    //I put my mock here
    $container-&gt;set('app.game-service', $fooService);

    //I launch the request
    $client-&gt;request('GET', '/foo');
    //I handle the response
    $response = $client-&gt;getResponse();

    //I do some tests like this one
    self::assertEquals(200, $response-&gt;getStatusCode());

}
</code></pre>

<p>As you can see, <strong>I do not call EntityManger</strong>, because I use <strong>Services</strong> and these lines to put my Services's Mock</p>

<pre><code>    //We create the client
    $client = static::createClient();
    $container = $client-&gt;getContainer();

    //I put my mock here
    $container-&gt;set('app.game-service', $fooService);
</code></pre>

<p>I have a problem to mock Entity Manager in my services. My Controller is tested but not My Service. </p>

<p>Here is the constructor initialized a simple <strong>protected</strong> property entityManager. The problem is this <em>protected</em> as you will see further...</p>

<pre><code>   /**
     * FooService constructor.
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager)
    {
           $this-&gt;entityManager = $entityManager;
    }
</code></pre>

<p>To Test my Service, here is my initial code :</p>

<pre><code>&lt;?php
class FooServiceTest extends KernelTestCase
{
    /**
     * Foo Service.
     *
     * @var FooService
     */
    private $fooService;

    /**
     * Prepares the environment before running each test.
     */
    protected function setUp()
    {
        parent::setUp();

        self::bootKernel();
        $this-&gt;fooService = static::$kernel-&gt;getContainer()
            -&gt;get('app.foo-service') //HERE IS HOW I HANDLE MY SERVICE TO TEST IT
        ;

    }
</code></pre>

<p>testStart Works perfectly, there is some database interaction.</p>

<p>I know that I can rollback data during test. But I want to Mock entityManager <strong>to verify the call of</strong> <code>commit</code> <strong>method</strong>.</p>

<p>I try this to mock entity manager in my setUp :</p>

<pre><code>    protected function setUp()
    {
        parent::setUp();

        $entityManager = $this
            -&gt;getMockBuilder('Doctrine\ORM\EntityManager')
            -&gt;disableOriginalConstructor()
            -&gt;getMock();
        $entityManager
            -&gt;expects(once())  // I WANT EXACTLY ONE CALL TO COMMIT METHOD
            -&gt;method('commit')  
            -&gt;willReturn(null);

        self::bootKernel();
        $container = static::$kernel-&gt;getContainer();
        $container-&gt;set('doctrine.orm.entity_manager', $entityManager); // THIS LINE DOES NOTHING &lt;=======
        $this-&gt;gameService = static::$kernel-&gt;getContainer()
            -&gt;get('app.game-service')
        ;
</code></pre>

<p>Thes code doesn't work. Mock is not in place. I still have the true entityManager. I think it is because the container is already closed. Do it is nut usefull to set </p>

<p>Like a Barbarian, I change the entityManager property to <strong>public</strong></p>

<p>And I do that :</p>

<pre><code>    protected function setUp()
    {
        parent::setUp();

        $entityManagerMock = $this
            -&gt;getMockBuilder('Doctrine\ORM\EntityManager')
            -&gt;disableOriginalConstructor()
            -&gt;getMock();
        $entityManagerMock
            -&gt;expects(once())  // I WANT EXACTLY ONE CALL TO commit METHOD
            -&gt;method('commit')  
            -&gt;willReturn(null);

        self::bootKernel();
        $this-&gt;gameService = static::$kernel-&gt;getContainer()
            -&gt;get('app.game-service')
        ;
        $this-&gt;gameService-&gt;entityManager = entityManagerMock;
</code></pre>

<p>It works perfectly. Test can be run. But it is NOT a good practice to have an entityManager in a public property</p>

<p>My question is : How to mock entityManager in a service test ?</p>

<p>(Sorry, I am not fluent in english)</p>