How to use mock in lumen framework? I use the Lumen framework. The documents of lumen are very simple. I don't know how to use mockery or facades to mock Models. I tried some means, but no one worked. I want to mock two points of UserModel in updatePassword method. Please help me.
UserModel
use Illuminate\Database\Eloquent\Model;
class UserModel extends Model {
// connection
protected $connection = 'db_user';
// table
protected $table = 'user';
// primarykey
protected $primaryKey = 'id';
}
UserLogic
class UserLogic {
public static updatePassword($id, $password) {
// find user
$user = UserModel::find($id); // mock here**************************************
if (empty($user)) {
return 'not find user';
}
// update password
$res = UserModel::where('id', $id)
->update(['password' => $password]); // mock here*****************************
if (false == $res) {
return 'update failed';
}
// re Login
$res2 = LoginModel::clearSession();
if (false == $res2) {
return false;
}
return true;
}
}
phpunit test 1 don't work
use Mockery;
public function testUpdatePassword() {
$mock = Mockery:mock('db_user');
$mock->shouldReceive('find')->andReturn(false);
$res = UserLogic::updatePassword(1, '123456');
$this->assertEquals('not find user', $res);
}
phpunit test 2
// BadMethodCallException: Method Mockery_0_Illuminate_DatabaseManager::connection() does not exist on this mock object
use Illuminate\Support\Facades\DB;
public function testUpdatePassword() {
DB::shouldReceive('select')->andReturnNull();
$res = UserLogic::updatePassword(1, '123456');
$this->assertEquals('not find user', $res);
}
phpunit test 3 don't work
use Mockery;
public function testUpdatePassword() {
$mock = Mockery::mock('alias:UserModel');
$mock->shouldReceive('find')->andReturn(null);
$res = UserLogic::updatePassword(1, '123456');
$this->assertEquals('not find user', $res);
}