title: Unit Test Symfony2
body: <p>I am trying to use Mockery in order to unit test my sf2 functions.
  I am strugeling with my first attempt.</p>
  
  <p>First try at test a class which uses security context:</p>
  
  <pre><code>public function setSecurityContext(SecurityContext $securityContext)
  {
      $this-&gt;securityContext = $securityContext;
      try {
          $this-&gt;isLoggedIn = $securityContext-&gt;isGranted('IS_AUTHENTICATED_FULLY');
          $this-&gt;user = $securityContext-&gt;getToken()-&gt;getUser();
      } catch (\Exception $e) {
          $this-&gt;isLoggedIn = false;
          $this-&gt;user = $securityContext-&gt;getToken()-&gt;getUser();
      }
  }
  </code></pre>
  
  <p>I create a <strong>testsetSecurityContext</strong> function like this:</p>
  
  <pre><code>public function testsetSecurityContext()
  {
      /* @var $securityContext SecurityContext */
      $securityContext = m::mock('Symfony\Component\Security\Core\SecurityContext');
  
      $securityContext-&gt;shouldReceive('isGranted')
      -&gt;with('IS_AUTHENTICATED_FULLY')
      -&gt;once()
      -&gt;andReturn(true);
  
      $factory = m::mock('Knp\Menu\FactoryInterface');
  
      $menu = new MenuBuilder($factory);
  
      $menu-&gt;setSecurityContext($securityContext);
  }
  </code></pre>
  
  <p>When running unit test, I receive the error:</p>
  
  <blockquote>
    <p>testsetSecurityContext</p>
    
    <blockquote>
      <p>Mockery\Exception: The method isGranted is marked final and it is not possible to generate a mock object with such a method defined. You should instead pass an instance of this object to Mockery to create a partial mock.</p>
    </blockquote>
  </blockquote>
  
  <p>So I change my test function accordingly: </p>
  
  <pre><code>public function testsetSecurityContext()
  {
      /* @var $securityContext SecurityContext */
      $securityContext = m::mock(new \Symfony\Component\Security\Core\SecurityContext());
      /* ... skipped ... */
  }
  </code></pre>
  
  <p>Now I receive that error:</p>
  
  <blockquote>
    <p>testsetSecurityContext</p>
    
    <blockquote>
      <p>ErrorException: Catchable Fatal Error: Argument 1 passed to Symfony\Component\Security\Core\SecurityContext::__construct() must implement interface <strong>Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface</strong>, none given, called in ..MenuBuilderTest.php on line 91 and defined in ..Symfony\Component\Security\Core\SecurityContext.php line 41</p>
    </blockquote>
  </blockquote>
  
  <p>So I modify my code again:</p>
  
  <pre><code>public function testsetSecurityContext()
  {
  
      $auth = m::mock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
  
      /* @var $securityContext SecurityContext */
      $securityContext = m::mock(new \Symfony\Component\Security\Core\SecurityContext($auth));
  
      /* ... skipped ... */
  
  }
  </code></pre>
  
  <p>And I get another error:</p>
  
  <blockquote>
    <p>testsetSecurityContext</p>
    
    <blockquote>
      <p>ErrorException: Catchable Fatal Error: Argument 2 passed to Symfony\Component\Security\Core\SecurityContext::__construct() must implement interface <strong>Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface</strong>, none given, called in ...\MenuBuilderTest.php on line 94 and defined in ...\Symfony\Component\Security\Core\SecurityContext.php line 41</p>
    </blockquote>
  </blockquote>
  
  <p>I endup with:</p>
  
  <pre><code>public function testsetSecurityContext()
  {
  
      $am = m::mock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
      $adm = m::mock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface');
  
      /* @var $securityContext SecurityContext */
      $securityContext = m::mock(new \Symfony\Component\Security\Core\SecurityContext($am, $adm));
  
      $securityContext-&gt;shouldReceive('isGranted')
      -&gt;with('IS_AUTHENTICATED_FULLY')
      -&gt;once()
      -&gt;andReturn(true);
  
      $factory = m::mock('Knp\Menu\FactoryInterface');
  
      $menu = new MenuBuilder($factory);
  
      $menu-&gt;setSecurityContext($securityContext);
  }
  </code></pre>
  
  <p>And that is still not OK as I get that error:</p>
  
  <blockquote>
    <p>testsetSecurityContext</p>
    
    <blockquote>
      <p>ErrorException: Catchable Fatal Error: Argument 1 passed to Atos\Worldline\Fm\Integration\Ucs\EventFlowAnalyser\Menu\MenuBuilder::setSecurityContext() must be an instance of <strong>Symfony\Component\Security\Core\SecurityContext</strong>, instance of <strong>Mockery_50c5c1e0e68d2</strong> given, called in ..\MenuBuilderTest.php on line 106 and defined in ..\MenuBuilder.php line 140</p>
    </blockquote>
  </blockquote>
  
  <p>I really would appreciate some help before I end-up with 100 lines test to test a 8 lines function...</p>