forked from fschmtt/keycloak-rest-api-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupsTest.php
More file actions
96 lines (74 loc) · 3.19 KB
/
Copy pathGroupsTest.php
File metadata and controls
96 lines (74 loc) · 3.19 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
namespace Fschmtt\Keycloak\Test\Integration\Resource;
use Exception;
use Fschmtt\Keycloak\Http\Criteria;
use Fschmtt\Keycloak\Representation\Group;
use Fschmtt\Keycloak\Representation\Realm;
use Fschmtt\Keycloak\Test\Integration\IntegrationTestBehaviour;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
class GroupsTest extends TestCase
{
use IntegrationTestBehaviour;
private const REALM = 'groups-test';
public static function setUpBeforeClass(): void
{
self::getKeycloak()->realms()->import(new Realm(realm: self::REALM));
}
public static function tearDownAfterClass(): void
{
self::getKeycloak()->realms()->delete(self::REALM);
}
public function testImportSearchUpdateDeleteGroup(): void
{
$groups = $this->getKeycloak()->groups();
$importedGroupName = Uuid::uuid4()->toString();
$updatedGroupName = Uuid::uuid4()->toString();
// Create group
$groups->create(self::REALM, new Group(name: $importedGroupName));
// Get all groups
$allGroups = $groups->all(self::REALM);
static::assertGreaterThanOrEqual(1, $allGroups->count());
$group = $allGroups->first();
static::assertInstanceOf(Group::class, $group);
// Search for single (imported) group
$importedGroup = $groups->all(self::REALM, new Criteria([
'name' => $importedGroupName,
]))->first();
static::assertInstanceOf(Group::class, $importedGroup);
static::assertSame($importedGroupName, $importedGroup->getName());
// Update (imported) group
$groups->update(self::REALM, $importedGroup->getId(), $importedGroup->withName($updatedGroupName));
// Delete (imported) user
$groups->delete(self::REALM, $importedGroup->getId());
try {
$groups->get(self::REALM, $importedGroup->getId());
static::fail('Group should not exist anymore');
} catch (Exception $e) {
static::assertSame(404, $e->getCode());
}
}
public function testCreateChildGroup(): void
{
$this->skipIfKeycloakVersionIsLessThan('23.0.0');
$importedGroupName = Uuid::uuid4()->toString();
$childGroupName = Uuid::uuid4()->toString();
$groups = $this->getKeycloak()->groups();
// Create group
$groups->create(self::REALM, new Group(name: $importedGroupName));
$group = $groups->all(self::REALM)->first();
static::assertInstanceOf(Group::class, $group);
// Create child group
$groups->createChild(self::REALM, new Group(name: $childGroupName), $group->getId());
$childGroups = $groups->children(self::REALM, $group->getId());
static::assertCount(1, $childGroups);
$childGroup = $childGroups->first();
static::assertInstanceOf(Group::class, $childGroup);
static::assertSame($childGroupName, $childGroup->getName());
// get child group by path
$pathGroup = $groups->byPath(self::REALM, $importedGroupName . '/' . $childGroupName);
static::assertInstanceOf(Group::class, $pathGroup);
static::assertSame($childGroup->getId(), $pathGroup->getId());
}
}