我在Laravel中测试我的存储库,我遇到了一些问题,很可能是关于我的方法的结构。
所以,我的存储库看起来像:
<?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->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->userModel
->where('email', '=', strtolower($userEmail))
->first();
if ($user) {
return $user->first();
}
return null;
}
}
/**
* @param $id
* @param $email
* @param $source
*
* @dataProvider usersDataProvider
*/
public function testGetUserByEmail($id, $email, $source)
{
$user = new User();
$user->id = $id;
$user->email = $email;
$user->user_source_id = $source;
$this->user->shouldReceive('getUserByEmail')->once()
->andReturn($user);
}
我很擅长与Mockery合作,我只是想知道我是否正在按照正确的方法来测试我的getUserByEmail($email)
方法。请记住(正如预期的那样)getUserByEmail($email)
会调用数据库。
此外,这是我收到的消息:
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
,这可能意味着没有与数据库的初始化连接。
更新
顺便说一句,我的setUp()
如下:
public function setUp()
{
$this->user = Mockery::mock('App\Test\Models\Entities\User');
parent::setUp();
}