Skip to content

Commit e094036

Browse files
authored
Merge pull request #165 from b1scione/organizations-update
Organizations update
2 parents a8d8687 + 6ba1d7b commit e094036

6 files changed

Lines changed: 71 additions & 24 deletions

File tree

src/Representation/OrganizationDomain.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
/**
88
* @codeCoverageIgnore
9+
*
10+
* @method string|null getName()
11+
* @method self withName(?string $name)
12+
*
13+
* @method bool|null getVerified()
14+
* @method self withVerified(?bool $verified)
915
*/
1016
class OrganizationDomain extends Representation
1117
{

src/Resource/Organizations.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ public function create(string $realm, Organization $organization): void
4949
);
5050
}
5151

52+
public function update(string $realm, string $id, Organization $organization): void
53+
{
54+
$this->commandExecutor->executeCommand(
55+
new Command(
56+
'/admin/realms/{realm}/organizations/{id}',
57+
Method::PUT,
58+
[
59+
'realm' => $realm,
60+
'id' => $id,
61+
],
62+
$organization,
63+
),
64+
);
65+
}
66+
5267
public function delete(string $realm, string $id): void
5368
{
5469
$this->commandExecutor->executeCommand(

tests/Integration/IntegrationTestBehaviour.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@
88

99
trait IntegrationTestBehaviour
1010
{
11-
private ?Keycloak $keycloak = null;
11+
private static ?Keycloak $keycloak = null;
1212

13-
public function getKeycloak(): Keycloak
13+
public static function getKeycloak(): Keycloak
1414
{
15-
if (!$this->keycloak) {
16-
$this->keycloak = new Keycloak(
15+
if (!self::$keycloak) {
16+
self::$keycloak = new Keycloak(
1717
$_SERVER['KEYCLOAK_BASE_URL'] ?? 'http://keycloak:8080',
1818
'admin',
1919
'admin',
2020
);
2121
}
2222

23-
return $this->keycloak;
23+
return self::$keycloak;
2424
}
2525

2626
protected function skipIfKeycloakVersionIsLessThan(string $version): void
2727
{
28-
if (version_compare($this->getKeycloak()->getVersion(), $version, '<')) {
28+
if (version_compare(self::getKeycloak()->getVersion(), $version, '<')) {
2929
$this->markTestSkipped(sprintf('Keycloak version is less than %s', $version));
3030
}
3131
}
3232

3333
protected function skipIfKeycloakVersionIsGreaterThan(string $version): void
3434
{
35-
if (version_compare($this->getKeycloak()->getVersion(), $version, '>')) {
35+
if (version_compare(self::getKeycloak()->getVersion(), $version, '>')) {
3636
$this->markTestSkipped(sprintf('Keycloak version is greater than %s', $version));
3737
}
3838
}

tests/Integration/Resource/GroupsTest.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Exception;
88
use Fschmtt\Keycloak\Http\Criteria;
99
use Fschmtt\Keycloak\Representation\Group;
10+
use Fschmtt\Keycloak\Representation\Realm;
1011
use Fschmtt\Keycloak\Test\Integration\IntegrationTestBehaviour;
1112
use PHPUnit\Framework\TestCase;
1213
use Ramsey\Uuid\Uuid;
@@ -15,6 +16,18 @@ class GroupsTest extends TestCase
1516
{
1617
use IntegrationTestBehaviour;
1718

19+
private const REALM = 'groups-test';
20+
21+
public static function setUpBeforeClass(): void
22+
{
23+
self::getKeycloak()->realms()->import(new Realm(realm: self::REALM));
24+
}
25+
26+
public static function tearDownAfterClass(): void
27+
{
28+
self::getKeycloak()->realms()->delete(self::REALM);
29+
}
30+
1831
public function testImportSearchUpdateDeleteGroup(): void
1932
{
2033
$groups = $this->getKeycloak()->groups();
@@ -23,32 +36,29 @@ public function testImportSearchUpdateDeleteGroup(): void
2336
$updatedGroupName = Uuid::uuid4()->toString();
2437

2538
// Create group
26-
$groups->create(
27-
'master',
28-
new Group(name: $importedGroupName),
29-
);
39+
$groups->create(self::REALM, new Group(name: $importedGroupName));
3040

3141
// Get all groups
32-
$allGroups = $groups->all('master');
42+
$allGroups = $groups->all(self::REALM);
3343
static::assertGreaterThanOrEqual(1, $allGroups->count());
3444
$group = $allGroups->first();
3545
static::assertInstanceOf(Group::class, $group);
3646

3747
// Search for single (imported) group
38-
$importedGroup = $groups->all('master', new Criteria([
48+
$importedGroup = $groups->all(self::REALM, new Criteria([
3949
'name' => $importedGroupName,
4050
]))->first();
4151
static::assertInstanceOf(Group::class, $importedGroup);
4252
static::assertSame($importedGroupName, $importedGroup->getName());
4353

4454
// Update (imported) group
45-
$groups->update('master', $importedGroup->getId(), $importedGroup->withName($updatedGroupName));
55+
$groups->update(self::REALM, $importedGroup->getId(), $importedGroup->withName($updatedGroupName));
4656

4757
// Delete (imported) user
48-
$groups->delete('master', $importedGroup->getId());
58+
$groups->delete(self::REALM, $importedGroup->getId());
4959

5060
try {
51-
$groups->get('master', $importedGroup->getId());
61+
$groups->get(self::REALM, $importedGroup->getId());
5262
static::fail('Group should not exist anymore');
5363
} catch (Exception $e) {
5464
static::assertSame(404, $e->getCode());
@@ -65,21 +75,21 @@ public function testCreateChildGroup(): void
6575
$groups = $this->getKeycloak()->groups();
6676

6777
// Create group
68-
$groups->create('master', new Group(name: $importedGroupName));
69-
$group = $groups->all('master')->first();
78+
$groups->create(self::REALM, new Group(name: $importedGroupName));
79+
$group = $groups->all(self::REALM)->first();
7080
static::assertInstanceOf(Group::class, $group);
7181

7282
// Create child group
73-
$groups->createChild('master', new Group(name: $childGroupName), $group->getId());
74-
$childGroups = $groups->children('master', $group->getId());
83+
$groups->createChild(self::REALM, new Group(name: $childGroupName), $group->getId());
84+
$childGroups = $groups->children(self::REALM, $group->getId());
7585
static::assertCount(1, $childGroups);
7686

7787
$childGroup = $childGroups->first();
7888
static::assertInstanceOf(Group::class, $childGroup);
7989
static::assertSame($childGroupName, $childGroup->getName());
8090

8191
// get child group by path
82-
$pathGroup = $groups->byPath('master', $importedGroupName . '/' . $childGroupName);
92+
$pathGroup = $groups->byPath(self::REALM, $importedGroupName . '/' . $childGroupName);
8393
static::assertInstanceOf(Group::class, $pathGroup);
8494
static::assertSame($childGroup->getId(), $pathGroup->getId());
8595
}

tests/Integration/Resource/OrganizationsTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class OrganizationsTest extends TestCase
1919
{
2020
use IntegrationTestBehaviour;
2121

22-
private const REALM = 'organizations-tests';
22+
private const REALM = 'organizations-test';
2323

2424
protected function setUp(): void
2525
{
@@ -74,6 +74,22 @@ public function testOrganizations(): void
7474
// Create user and add it to the organization
7575
$this->getKeycloak()->organizations()->addUser(self::REALM, $organization->getId(), $this->createAndGetUser()->getId());
7676

77+
// Update organization
78+
$updatedOrganization = $organization->withDomains(new OrganizationDomainCollection([
79+
new OrganizationDomain('foo.bar.updated', true),
80+
new OrganizationDomain('bar.foo.updated', false),
81+
]));
82+
$this->getKeycloak()->organizations()->update(self::REALM, $organization->getId(), $updatedOrganization);
83+
$organizations = $this->getKeycloak()->organizations()->all(self::REALM);
84+
static::assertCount(1, $organizations);
85+
static::assertSame($updatedOrganization->getName(), $organizations->first()->getName());
86+
$domains = $organizations->first()->getDomains();
87+
static::assertCount(2, $domains);
88+
static::assertSame([
89+
'foo.bar.updated',
90+
'bar.foo.updated',
91+
], array_map(static fn (OrganizationDomain $domain) => $domain->getName(), $domains->all()));
92+
7793
// Delete newly created organization
7894
$this->getKeycloak()->organizations()->delete(self::REALM, $organizations->first()->getId());
7995
$organizations = $this->getKeycloak()->organizations()->all(self::REALM);

tests/Integration/Resource/RealmsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testCanUpdateRealm(): void
4343
static::assertFalse($realm->getRegistrationAllowed());
4444

4545
$realm = $realm->withRegistrationAllowed(true);
46-
$realm = $this->keycloak->realms()->update($realm->getRealm(), $realm);
46+
$realm = $this->getKeycloak()->realms()->update($realm->getRealm(), $realm);
4747

4848
static::assertTrue($realm->getRegistrationAllowed());
4949
}
@@ -56,7 +56,7 @@ public function testCanImportRealm(): void
5656
static::assertEquals('testing-id', $realm->getId());
5757
static::assertEquals('testing-realm', $realm->getRealm());
5858

59-
static::assertCount(2, $this->keycloak->realms()->all());
59+
static::assertCount(2, $this->getKeycloak()->realms()->all());
6060
}
6161

6262
public function testCanClearCaches(): void

0 commit comments

Comments
 (0)