title: ZF2 unit-testing authentication
body: <p>I was learning about unit testing and I attempted to resolve the following issue:</p>
  
  <pre><code>Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for zfcUserAuthentication
  </code></pre>
  
  <p>... using the only answer given at:</p>
  
  <p><a href="https://stackoverflow.com/questions/14354456/simple-zf2-unit-tests-for-a-controller-using-zfcuser">Simple ZF2 Unit Tests for a controller using ZfcUser</a></p>
  
  <p>So my setUp function looks the same. Unfortunately, I get the error message:</p>
  
  <pre><code>Zend\Mvc\Exception\InvalidPluginException: Plugin of type Mock_ZfcUserAuthentication_868bf824 is invalid; must implement Zend\Mvc\Controller\Plugin\PluginInterface
  </code></pre>
  
  <p>It is caused at this part of the code (split up in my code in the same way):</p>
  
  <pre><code>$this -&gt; controller-&gt;getPluginManager()
  -&gt;setService('zfcUserAuthentication', $authMock); // Error refers to this line.
  </code></pre>
  
  <p>The $authMock object is apparently not implementing plugininterface, which I need to implement to pass into setService. </p>
  
  <p>Is $authMock not meant to be passed there for it's use in unit testing? Should I be using a different (unit-testing oriented) setService method?</p>
  
  <p>I need a way to handle logging into my application, or my unit testing is pointless.</p>
  
  <p>Thanks for any advice.</p>
  
  <p>=== Edit (11/02/2013) ===</p>
  
  <p>I wanted to focus on this part for clarification, as I think this is the problem area:</p>
  
  <pre><code>// Getting mock of authentication object, which is used as a plugin.
  $authMock = $this-&gt;getMock('ZfcUser\Controller\Plugin\ZfcUserAuthentication');
  
  // Some expectations of the authentication service.
  $authMock   -&gt; expects($this-&gt;any())
      -&gt; method('hasIdentity')
      -&gt; will($this-&gt;returnValue(true));  
  
  $authMock   -&gt; expects($this-&gt;any())
      -&gt; method('getIdentity')
      -&gt; will($this-&gt;returnValue($ZfcUserMock));
  
  // At this point, PluginManager disallows mock being assigned as plugin because 
  // it will not implement plugin interface, as mentioned.
  $this -&gt; controller-&gt;getPluginManager()
  -&gt;setService('zfcUserAuthentication', $authMock);
  </code></pre>
  
  <p>If the mock doesn't handle necessary implementations, how else am I to pretend to login?</p>