title: PHP Unit Testing - Mock static autoloaded class with Mockery
body: <p>I want to unit test a wrapper class for the official Segment PHP integration. Therefore I'll have to mock <a href="https://github.com/segmentio/analytics-php/blob/master/lib/Segment.php" rel="nofollow noreferrer">the Segment class</a> with Mockery so there won't be any real API requests.</p>
  
  <p><strong>The Problem</strong></p>
  
  <p>The class to mock consists only of static methods. Because of this I try to mock it like this (with <a href="http://docs.mockery.io/en/latest/reference/startup_methods.html" rel="nofollow noreferrer">alias</a>):</p>
  
  <p><code>$segment = Mockery::mock('alias:Segment');</code></p>
  
  <p>This works, but only if the class isn't autoloaded by composer. If I load it - like I have to for the rest of the app - I'll get the error </p>
  
  <p><code>Could not load mock Segment, class already exists.</code></p>
  
  <p>(That makes sense, because the docs state that an aliased class must not be loaded before.) </p>
  
  <p><strong>The question</strong></p>
  
  <p>How can I mock this (evil?) class, but still use it as usual in the rest of my app?</p>