Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Representation/OrganizationDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

/**
* @codeCoverageIgnore
*
* @method string|null getName()
* @method self withName(?string $name)
*
* @method bool|null getVerified()
* @method self withVerified(?bool $verified)
*/
class OrganizationDomain extends Representation
{
Expand Down
15 changes: 15 additions & 0 deletions src/Resource/Organizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ public function create(string $realm, Organization $organization): void
);
}

public function update(string $realm, string $id, Organization $organization): void
{
$this->commandExecutor->executeCommand(
new Command(
'/admin/realms/{realm}/organizations/{id}',
Method::PUT,
[
'realm' => $realm,
'id' => $id,
],
$organization,
),
);
}

public function delete(string $realm, string $id): void
{
$this->commandExecutor->executeCommand(
Expand Down
14 changes: 7 additions & 7 deletions tests/Integration/IntegrationTestBehaviour.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@

trait IntegrationTestBehaviour
{
private ?Keycloak $keycloak = null;
private static ?Keycloak $keycloak = null;

public function getKeycloak(): Keycloak
public static function getKeycloak(): Keycloak
{
if (!$this->keycloak) {
$this->keycloak = new Keycloak(
if (!self::$keycloak) {
self::$keycloak = new Keycloak(
$_SERVER['KEYCLOAK_BASE_URL'] ?? 'http://keycloak:8080',
'admin',
'admin',
);
}

return $this->keycloak;
return self::$keycloak;
}

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

protected function skipIfKeycloakVersionIsGreaterThan(string $version): void
{
if (version_compare($this->getKeycloak()->getVersion(), $version, '>')) {
if (version_compare(self::getKeycloak()->getVersion(), $version, '>')) {
$this->markTestSkipped(sprintf('Keycloak version is greater than %s', $version));
}
}
Expand Down
38 changes: 24 additions & 14 deletions tests/Integration/Resource/GroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -15,6 +16,18 @@ 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();
Expand All @@ -23,32 +36,29 @@ public function testImportSearchUpdateDeleteGroup(): void
$updatedGroupName = Uuid::uuid4()->toString();

// Create group
$groups->create(
'master',
new Group(name: $importedGroupName),
);
$groups->create(self::REALM, new Group(name: $importedGroupName));

// Get all groups
$allGroups = $groups->all('master');
$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('master', new Criteria([
$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('master', $importedGroup->getId(), $importedGroup->withName($updatedGroupName));
$groups->update(self::REALM, $importedGroup->getId(), $importedGroup->withName($updatedGroupName));

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

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

// Create group
$groups->create('master', new Group(name: $importedGroupName));
$group = $groups->all('master')->first();
$groups->create(self::REALM, new Group(name: $importedGroupName));
$group = $groups->all(self::REALM)->first();
static::assertInstanceOf(Group::class, $group);

// Create child group
$groups->createChild('master', new Group(name: $childGroupName), $group->getId());
$childGroups = $groups->children('master', $group->getId());
$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('master', $importedGroupName . '/' . $childGroupName);
$pathGroup = $groups->byPath(self::REALM, $importedGroupName . '/' . $childGroupName);
static::assertInstanceOf(Group::class, $pathGroup);
static::assertSame($childGroup->getId(), $pathGroup->getId());
}
Expand Down
18 changes: 17 additions & 1 deletion tests/Integration/Resource/OrganizationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class OrganizationsTest extends TestCase
{
use IntegrationTestBehaviour;

private const REALM = 'organizations-tests';
private const REALM = 'organizations-test';

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

// Update organization
$updatedOrganization = $organization->withDomains(new OrganizationDomainCollection([
new OrganizationDomain('foo.bar.updated', true),
new OrganizationDomain('bar.foo.updated', false),
]));
$this->getKeycloak()->organizations()->update(self::REALM, $organization->getId(), $updatedOrganization);
$organizations = $this->getKeycloak()->organizations()->all(self::REALM);
static::assertCount(1, $organizations);
static::assertSame($updatedOrganization->getName(), $organizations->first()->getName());
$domains = $organizations->first()->getDomains();
static::assertCount(2, $domains);
static::assertSame([
'foo.bar.updated',
'bar.foo.updated',
], array_map(static fn (OrganizationDomain $domain) => $domain->getName(), $domains->all()));

// Delete newly created organization
$this->getKeycloak()->organizations()->delete(self::REALM, $organizations->first()->getId());
$organizations = $this->getKeycloak()->organizations()->all(self::REALM);
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Resource/RealmsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testCanUpdateRealm(): void
static::assertFalse($realm->getRegistrationAllowed());

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

static::assertTrue($realm->getRegistrationAllowed());
}
Expand All @@ -56,7 +56,7 @@ public function testCanImportRealm(): void
static::assertEquals('testing-id', $realm->getId());
static::assertEquals('testing-realm', $realm->getRealm());

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

public function testCanClearCaches(): void
Expand Down