title: Mocking a service in unit test in Symfony 3.4
body: <p>I have a simple DefaultController as service, where the DI is autowired.</p>
  
  <pre class="lang-php prettyprint-override"><code>class DefaultController extends BaseController
  {
      private $logger;
      private $recordRepository;
  
      public function __construct(LoggerInterface $logger, RecordRepository $recordRepository)
      {
          $this-&gt;logger = $logger;
          $this-&gt;recordRepository = $recordRepository;
      }
  
      public function downloadAction($uuid)
      {
          $recordRepo = $this-&gt;recordRepository;
  
          $record = $recordRepo-&gt;findActive($uuid);
  
          $logger = $this-&gt;logger;
  
          if (!$record) {
              $logger-&gt;error('Record not found: '.$uuid);
  
              return $this-&gt;render('default/download_error.html.twig', [
                  'error' =&gt; 'record_not_found'
              ]);
          }
      }
  }
  </code></pre>
  
  <p>I've written a unit test that tries to call this method while also replacing one of the services (the logger) with a mock:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  namespace Tests\AppBundle\Controller;
  
  use Monolog\Logger;
  use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  
  class DefaultControllerTest extends WebTestCase {
  
      public function testDownloadAction()
      {
          $mock = $this-&gt;getMockBuilder(Logger::class)
              -&gt;disableOriginalConstructor()
              -&gt;getMock();
  
          $mock-&gt;expects($this-&gt;once())-&gt;method('error')
              -&gt;with('Record not found: 1223');
  
          $client = static::createClient([]);
  
          $client-&gt;getContainer()-&gt;set('logger', $mock);
  
          $client-&gt;request('GET', '/download/12233');
  
          print $client-&gt;getResponse()-&gt;getContent();
  
          $this-&gt;assertEquals(200, $client-&gt;getResponse()-&gt;getStatusCode());
      }
  
  }
  </code></pre>
  
  <p>The problem is, when this test fails (because the logger error and expected string dont match up), the PHPunit message is put in the <code>$client-&gt;getResponse</code> output instead of being caught in the CLI.</p>
  
  <p>Output CLI:</p>
  
  <pre><code>PHPUnit 4.8.36 by Sebastian Bergmann and contributors.
  F&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset="UTF-8" /&gt;
          &lt;meta name="robots" content="noindex,nofollow" /&gt;
          &lt;meta name="viewport" content="width=device-width,initial-scale=1" /&gt;
          &lt;title&gt;    Expectation failed for method name is equal to &amp;lt;string:error&amp;gt; when invoked 1 time(s) Parameter 0 for invocation Monolog\Logger::error(&amp;#039;Record not found: 12233&amp;#039;, Array ()) does not match expected value. Failed asserting that two strings are equal. (500 Internal Server Error) &lt;/title&gt; 
  
  (further long webpage output removed here)
  
  PHPUnit message:
  
  There was 1 failure:
  
  1) Tests\AppBundle\Controller\DefaultControllerTest::testDownloadAction
  Failed asserting that 500 matches expected 200.
  </code></pre>
  
  <p>Do I have to use a regular PHPUnit case if I want to use mock objects? In that case I should probably refactor the <code>$this-&gt;render</code> in the Controller as well.</p>