|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Fschmtt\Keycloak\Test\Integration\Resource; |
| 6 | + |
| 7 | +use Fschmtt\Keycloak\Collection\OrganizationDomainCollection; |
| 8 | +use Fschmtt\Keycloak\Representation\Organization; |
| 9 | +use Fschmtt\Keycloak\Representation\OrganizationDomain; |
| 10 | +use Fschmtt\Keycloak\Representation\Realm; |
| 11 | +use Fschmtt\Keycloak\Test\Integration\IntegrationTestBehaviour; |
| 12 | +use GuzzleHttp\Exception\ServerException; |
| 13 | +use PHPUnit\Framework\Attributes\AfterClass; |
| 14 | +use PHPUnit\Framework\Attributes\BeforeClass; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class OrganizationsTest extends TestCase |
| 18 | +{ |
| 19 | + use IntegrationTestBehaviour; |
| 20 | + private const REALM = 'organizations-tests'; |
| 21 | + |
| 22 | + public function testOrganizations(): void |
| 23 | + { |
| 24 | + $this->skipIfKeycloakVersionIsLessThan('26.0.0'); |
| 25 | + |
| 26 | + // Create realm |
| 27 | + $this->getKeycloak()->realms()->import(new Realm(realm: self::REALM, organizationsEnabled: true)); |
| 28 | + |
| 29 | + // No organizations exist yet in realm |
| 30 | + $organizations = $this->getKeycloak()->organizations()->all(self::REALM); |
| 31 | + static::assertCount(0, $organizations); |
| 32 | + |
| 33 | + // Create a new organization in realm |
| 34 | + $createdOrganization = new Organization( |
| 35 | + name: 'created-organization', |
| 36 | + domains: new OrganizationDomainCollection([ |
| 37 | + new OrganizationDomain('foo.bar', true), |
| 38 | + new OrganizationDomain('bar.foo', false), |
| 39 | + ]), |
| 40 | + ); |
| 41 | + $this->getKeycloak()->organizations()->create(self::REALM, $createdOrganization); |
| 42 | + |
| 43 | + $organizations = $this->getKeycloak()->organizations()->all(self::REALM); |
| 44 | + static::assertCount(1, $organizations); |
| 45 | + static::assertSame($createdOrganization->getName(), $organizations->first()->getName()); |
| 46 | + |
| 47 | + // Get newly created organization |
| 48 | + $organization = $this->getKeycloak()->organizations()->get(self::REALM, $organizations->first()->getId()); |
| 49 | + static::assertSame($createdOrganization->getName(), $organization->getName()); |
| 50 | + |
| 51 | + try { |
| 52 | + // Invite user to newly created organization |
| 53 | + $this->getKeycloak()->organizations()->inviteUser( |
| 54 | + self::REALM, |
| 55 | + $organizations->first()->getId(), |
| 56 | + |
| 57 | + 'John', |
| 58 | + 'Doe', |
| 59 | + ); |
| 60 | + } catch (ServerException $e) { |
| 61 | + // Error is expected as SMTP is not configured |
| 62 | + static::assertSame(500, $e->getCode()); |
| 63 | + static::assertSame( |
| 64 | + ['errorMessage' => 'Failed to send invite email'], |
| 65 | + json_decode($e->getResponse()->getBody()->getContents(), true, flags: JSON_THROW_ON_ERROR), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + // Delete newly created organization |
| 70 | + $this->getKeycloak()->organizations()->delete(self::REALM, $organizations->first()->getId()); |
| 71 | + $organizations = $this->getKeycloak()->organizations()->all(self::REALM); |
| 72 | + static::assertCount(0, $organizations); |
| 73 | + |
| 74 | + // Delete realm |
| 75 | + $this->getKeycloak()->realms()->delete(self::REALM); |
| 76 | + } |
| 77 | +} |
0 commit comments