title: How to mock an aliased class with a method that returns an instance of itself?
body: <p>I've been successfully using <strong><a href="https://github.com/mockery/mockery" rel="noreferrer">Mockery</a></strong> with PHPUnit tests lately. Yet, there is a dependency in a project I'm currently working that uses static method calls to interact with an API. I'm struggling to test one particular use case and it feels like I'll find other like this during the development roadmap.</p>
  
  <p>Using this class as an example:</p>
  
  <pre><code>namespace Name\Space;
  class User
  {
      /**
       * @return \Name\Space\User[]
       */
      public static function list(): array
      {
          // ...
      }
      public static function create(array $attrs): User
      {
          // ...
      }
  }
  </code></pre>
  
  <p>In case I just want to assert a method returns a primitive type, such as an array:</p>
  
  <pre><code>Mockery::mock('alias:\Name\Space\User')
      -&gt;shouldReceive('list')
      -&gt;andReturn([]);
  </code></pre>
  
  <p>It works fine, primarily because I'm not testing the array contents.</p>
  
  <p>However, I have to call the <code>create</code> method, which returns an instance of the class itself (<code>User</code>). If I do something like this:</p>
  
  <pre><code>$user = new \Name\Space\User();
  Mockery::mock('alias:\Name\Space\User')
      -&gt;shouldReceive('create')
      -&gt;andReturn($user);
  </code></pre>
  
  <p>The alias, obviously, won't work because the class was already loaded through the autoloader (composer's, in this case).</p>
  
  <p>Does anyone have a suggestion on how to workaround this?</p>