primo upload
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Setup;
|
||||
|
||||
use Vanguard\Permission;
|
||||
use Vanguard\Role;
|
||||
|
||||
class RoleFactory
|
||||
{
|
||||
protected $permissions = [];
|
||||
|
||||
protected $removable = false;
|
||||
|
||||
public function withPermissions($permissions)
|
||||
{
|
||||
$this->permissions = func_get_args();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removable()
|
||||
{
|
||||
$this->removable = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unremovable()
|
||||
{
|
||||
$this->removable = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$role = Role::factory()->create([
|
||||
'removable' => $this->removable
|
||||
]);
|
||||
|
||||
foreach ($this->permissions as $name) {
|
||||
$role->attachPermission(Permission::where('name', $name)->first());
|
||||
}
|
||||
|
||||
return $role;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Setup;
|
||||
|
||||
use Vanguard\Role;
|
||||
use Vanguard\Support\Enum\UserStatus;
|
||||
use Vanguard\User;
|
||||
use \Facades\Tests\Setup\RoleFactory;
|
||||
|
||||
class UserFactory
|
||||
{
|
||||
protected $params = [];
|
||||
protected $role;
|
||||
|
||||
public function withCredentials($username, $password)
|
||||
{
|
||||
$this->params['username'] = $username;
|
||||
$this->params['password'] = $password;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function banned()
|
||||
{
|
||||
$this->params['status'] = UserStatus::BANNED;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unconfirmed()
|
||||
{
|
||||
$this->params['status'] = UserStatus::UNCONFIRMED;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unverified()
|
||||
{
|
||||
$this->params['email_verified_at'] = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function role(Role $role)
|
||||
{
|
||||
$this->role = $role;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function admin()
|
||||
{
|
||||
return $this->role(Role::where('name', 'Admin')->first());
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->role(Role::where('name', 'User')->first());
|
||||
}
|
||||
|
||||
public function email($email)
|
||||
{
|
||||
$this->params['email'] = $email;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function twoFactor($countryCode, $phone)
|
||||
{
|
||||
$this->params['two_factor_country_code'] = $countryCode;
|
||||
$this->params['two_factor_phone'] = $phone;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withPermissions($permissions)
|
||||
{
|
||||
$permissions = func_get_args();
|
||||
|
||||
$this->role(
|
||||
RoleFactory::withPermissions($permissions)->create()
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function create(array $overrides = [])
|
||||
{
|
||||
if ($this->role) {
|
||||
$this->params['role_id'] = $this->role;
|
||||
}
|
||||
|
||||
return User::factory()->create(array_merge($this->params, $overrides));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user