Skip to content

Commit eac513b

Browse files
authored
Merge pull request #11 from fschmtt/add-realm-collection
Return RealmCollection for Realms::all()
2 parents e5991ff + 9689967 commit eac513b

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ More examples can be found in the [examples](examples) directory.
6060
| Endpoint | Status Code | Response | API |
6161
|----------|-------------|----------|-----|
6262
| `POST /auth/admin/realms` | `201` | [Realm](src/Representation/Realm.php) | [Realms::import()](src/Resource/Realms.php) |
63-
| `GET /auth/admin/realms` | `200` | array<[Realm](src/Representation/Realm.php)> | [Realms::all()](src/Resource/Realms.php) |
63+
| `GET /auth/admin/realms` | `200` | [RealmCollection](src/Collection/RealmCollection.php)> | [Realms::all()](src/Resource/Realms.php) |
6464
| `PUT /auth/admin/realms/{realm}` | `204` | [Realm](src/Representation/Realm.php) | [Realms::update()](src/Resource/Realms.php) |
6565
| `DELETE /auth/admin/realms/{realm}` | `204` | `n/a` | [Realms::delete()](src/Resource/Realms.php) |
6666
| `GET /auth/admin/realms/{realm}/admin-events` | `200` | `array` | [Realms::adminEvents()](src/Resource/Realms.php) |

src/Collection/RealmCollection.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fschmtt\Keycloak\Collection;
6+
7+
use Fschmtt\Keycloak\Representation\Realm;
8+
9+
/**
10+
* @method Realm[] getIterator()
11+
*/
12+
class RealmCollection extends Collection
13+
{
14+
public static function getRepresentationClass(): string
15+
{
16+
return Realm::class;
17+
}
18+
}

src/Resource/Realms.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Fschmtt\Keycloak\Collection\ClientCollection;
88
use Fschmtt\Keycloak\Collection\GroupCollection;
9+
use Fschmtt\Keycloak\Collection\RealmCollection;
910
use Fschmtt\Keycloak\Collection\UserCollection;
1011
use Fschmtt\Keycloak\Json\JsonDecoder;
1112
use Fschmtt\Keycloak\Json\JsonEncoder;
@@ -17,7 +18,7 @@ class Realms extends Resource
1718
{
1819
private const BASE_PATH = '/auth/admin/realms';
1920

20-
public function all(): array
21+
public function all(): RealmCollection
2122
{
2223
/** @var Realm[] $realms */
2324
$realms = [];
@@ -33,7 +34,7 @@ public function all(): array
3334
$realms[] = Realm::from($realm);
3435
}
3536

36-
return $realms;
37+
return new RealmCollection($realms);
3738
}
3839

3940
public function get(string $realm): Realm

tests/Integration/Resource/RealmsTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Fschmtt\Keycloak\Test\Integration\Resource;
66

7+
use Fschmtt\Keycloak\Collection\RealmCollection;
78
use Fschmtt\Keycloak\Representation\Realm;
89
use Fschmtt\Keycloak\Test\Integration\IntegrationTestBehaviour;
910
use PHPUnit\Framework\TestCase;
@@ -16,9 +17,8 @@ public function testCanGetAllRealms(): void
1617
{
1718
$realms = $this->getKeycloak()->realms()->all();
1819

19-
foreach ($realms as $realm) {
20-
static::assertInstanceOf(Realm::class, $realm);
21-
}
20+
static::assertInstanceOf(RealmCollection::class, $realms);
21+
static::assertCount(1, $realms);
2222
}
2323

2424
public function testCanGetRealm(): void

0 commit comments

Comments
 (0)