forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFakesUserGroups.php
46 lines (38 loc) · 1.26 KB
/
FakesUserGroups.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace Tests;
use Illuminate\Support\Collection;
use Statamic\Auth\File\UserGroup as FileUserGroup;
use Statamic\Auth\File\UserGroupRepository;
use Statamic\Contracts\Auth\UserGroupRepository as RepositoryContract;
use Statamic\Facades\UserGroup;
trait FakesUserGroups
{
private function setTestUserGroups($groups)
{
$groups = collect($groups)
->mapWithKeys(function ($roles, $handle) {
$handle = is_string($roles) ? $roles : $handle;
$roles = is_string($roles) ? [] : $roles;
return [$handle => $roles];
})
->map(function ($roles, $handle) {
return $roles instanceof FileUserGroup
? $roles->handle($handle)
: UserGroup::make()->handle($handle)->roles($roles);
});
$fake = new class($groups) extends UserGroupRepository
{
protected $groups;
public function __construct($groups)
{
$this->groups = $groups;
}
public function all(): Collection
{
return $this->groups;
}
};
app()->instance(RepositoryContract::class, $fake);
UserGroup::swap($fake);
}
}