PHPunit: testing a Repository
<p>I am testing my repository in Laravel and I came across a few issues, most probably with regards to the structure of my methods.</p>

<p>So, my repository looks like:</p>

<pre><code>&lt;?php

namespace Repositories\User;

use App\Test\Models\Entities\User;
use Illuminate\Database\Eloquent\Model;

class UserRepository implements UserInterface
{

    /**
     * @var Model $userModel
     */
    protected $userModel;

    /**
     * Setting our class $userModel to the injected model
     *
     * @param Model $userModel
     * @return UserRepository
     */
    public function __construct(Model $userModel)
    {
        $this-&gt;userModel = $userModel;
    }


    /**
     * Returns the User object associated with the userEmail
     *
     * @param string $userEmail
     * @return User | null
     */
    public function getUserByEmail($userEmail)
    {
        // Search by email
        $user = $this-&gt;userModel
                        -&gt;where('email', '=', strtolower($userEmail))
                        -&gt;first();

        if ($user) {
            return $user-&gt;first();
        }

        return null;
    }



}


 /**
     * @param $id
     * @param $email
     * @param $source
     *
     * @dataProvider usersDataProvider
     */
    public function testGetUserByEmail($id, $email, $source)
    {
        $user = new User();
        $user-&gt;id = $id;
        $user-&gt;email = $email;
        $user-&gt;user_source_id = $source;

        $this-&gt;user-&gt;shouldReceive('getUserByEmail')-&gt;once()
            -&gt;andReturn($user);

    }
</code></pre>

<p>I am quite new working with Mockery and am just wondering whether I am following the correct approach in order to test my <code>getUserByEmail($email)</code> method. Please bare in mind that (as expected) <code>getUserByEmail($email)</code> makes a call to the Database.</p>

<p>Also, this is the message that I receive:</p>

<p><code>PHP Fatal error:  Call to a member function connection() on null in /private/var/www/ff-php-prelaunch/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php</code>, which probably implies that there is no initialized connection to the DB.</p>

<p><strong>UPDATE</strong></p>

<p>Btw, my <code>setUp()</code> is as follows:</p>

<pre><code>  public function setUp()
    {
        $this-&gt;user = Mockery::mock('App\Test\Models\Entities\User');

        parent::setUp();
    }
</code></pre>