diff --git a/src/Representation/OrganizationDomain.php b/src/Representation/OrganizationDomain.php index 8e93ba9..c52a44e 100644 --- a/src/Representation/OrganizationDomain.php +++ b/src/Representation/OrganizationDomain.php @@ -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 { diff --git a/src/Resource/Organizations.php b/src/Resource/Organizations.php index fd96314..0cfbf26 100644 --- a/src/Resource/Organizations.php +++ b/src/Resource/Organizations.php @@ -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( diff --git a/tests/Integration/IntegrationTestBehaviour.php b/tests/Integration/IntegrationTestBehaviour.php index f13cb66..33ab92a 100644 --- a/tests/Integration/IntegrationTestBehaviour.php +++ b/tests/Integration/IntegrationTestBehaviour.php @@ -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)); } } diff --git a/tests/Integration/Resource/GroupsTest.php b/tests/Integration/Resource/GroupsTest.php index 8eeba2a..b0ee4b2 100644 --- a/tests/Integration/Resource/GroupsTest.php +++ b/tests/Integration/Resource/GroupsTest.php @@ -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; @@ -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(); @@ -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()); @@ -65,13 +75,13 @@ 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(); @@ -79,7 +89,7 @@ public function testCreateChildGroup(): void 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()); } diff --git a/tests/Integration/Resource/OrganizationsTest.php b/tests/Integration/Resource/OrganizationsTest.php index 8c57660..7d918b2 100644 --- a/tests/Integration/Resource/OrganizationsTest.php +++ b/tests/Integration/Resource/OrganizationsTest.php @@ -19,7 +19,7 @@ class OrganizationsTest extends TestCase { use IntegrationTestBehaviour; - private const REALM = 'organizations-tests'; + private const REALM = 'organizations-test'; protected function setUp(): void { @@ -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); diff --git a/tests/Integration/Resource/RealmsTest.php b/tests/Integration/Resource/RealmsTest.php index 83c3674..c2441ce 100644 --- a/tests/Integration/Resource/RealmsTest.php +++ b/tests/Integration/Resource/RealmsTest.php @@ -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()); } @@ -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