Skip to content

Commit a62828e

Browse files
committed
feat(organizations): add integration tests
1 parent bdef932 commit a62828e

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

src/Representation/Realm.php

+6
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@
277277
* @method self withWebAuthnPolicySignatureAlgorithms(?array $value)
278278
* @method self withWebAuthnPolicyUserVerificationRequirement(?string $value)
279279
*
280+
* @method OrganizationCollection|null getOrganizations()
281+
* @method self withOrganizations(?OrganizationCollection $organizations)
282+
*
283+
* @method bool|null getOrganizationsEnabled()
284+
* @method self withOrganizationsEnabled(?bool $organizationsEnabled)
285+
*
280286
* @codeCoverageIgnore
281287
*/
282288
class Realm extends Representation

src/Resource/Realms.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public function import(Realm $realm): Realm
4747
new Command(
4848
'/admin/realms',
4949
Method::POST,
50-
[],
51-
$realm,
50+
payload: $realm,
5251
),
5352
);
5453

tests/Integration/KeycloakTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class KeycloakTest extends TestCase
1414
public function testFetchesKeycloakVersionBeforeResourceIsAccessedForTheFirstTime(): void
1515
{
1616
$reflection = new ReflectionClass($this->getKeycloak());
17-
$version = $reflection->getProperty('version')->getValue($this->keycloak);
17+
$version = $reflection->getProperty('version')->getValue($this->getKeycloak());
1818

1919
static::assertNull($version);
2020

21-
$this->keycloak->realms();
21+
$this->getKeycloak()->realms();
2222

23-
$version = $reflection->getProperty('version')->getValue($this->keycloak);
23+
$version = $reflection->getProperty('version')->getValue($this->getKeycloak());
2424
static::assertIsString($version);
2525
}
2626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)