77use Exception ;
88use Fschmtt \Keycloak \Http \Criteria ;
99use Fschmtt \Keycloak \Representation \Group ;
10+ use Fschmtt \Keycloak \Representation \Realm ;
1011use Fschmtt \Keycloak \Test \Integration \IntegrationTestBehaviour ;
1112use PHPUnit \Framework \TestCase ;
1213use Ramsey \Uuid \Uuid ;
@@ -15,6 +16,18 @@ class GroupsTest extends TestCase
1516{
1617 use IntegrationTestBehaviour;
1718
19+ private const REALM = 'groups-test ' ;
20+
21+ public static function setUpBeforeClass (): void
22+ {
23+ self ::getKeycloak ()->realms ()->import (new Realm (realm: self ::REALM ));
24+ }
25+
26+ public static function tearDownAfterClass (): void
27+ {
28+ self ::getKeycloak ()->realms ()->delete (self ::REALM );
29+ }
30+
1831 public function testImportSearchUpdateDeleteGroup (): void
1932 {
2033 $ groups = $ this ->getKeycloak ()->groups ();
@@ -23,32 +36,29 @@ public function testImportSearchUpdateDeleteGroup(): void
2336 $ updatedGroupName = Uuid::uuid4 ()->toString ();
2437
2538 // Create group
26- $ groups ->create (
27- 'master ' ,
28- new Group (name: $ importedGroupName ),
29- );
39+ $ groups ->create (self ::REALM , new Group (name: $ importedGroupName ));
3040
3141 // Get all groups
32- $ allGroups = $ groups ->all (' master ' );
42+ $ allGroups = $ groups ->all (self :: REALM );
3343 static ::assertGreaterThanOrEqual (1 , $ allGroups ->count ());
3444 $ group = $ allGroups ->first ();
3545 static ::assertInstanceOf (Group::class, $ group );
3646
3747 // Search for single (imported) group
38- $ importedGroup = $ groups ->all (' master ' , new Criteria ([
48+ $ importedGroup = $ groups ->all (self :: REALM , new Criteria ([
3949 'name ' => $ importedGroupName ,
4050 ]))->first ();
4151 static ::assertInstanceOf (Group::class, $ importedGroup );
4252 static ::assertSame ($ importedGroupName , $ importedGroup ->getName ());
4353
4454 // Update (imported) group
45- $ groups ->update (' master ' , $ importedGroup ->getId (), $ importedGroup ->withName ($ updatedGroupName ));
55+ $ groups ->update (self :: REALM , $ importedGroup ->getId (), $ importedGroup ->withName ($ updatedGroupName ));
4656
4757 // Delete (imported) user
48- $ groups ->delete (' master ' , $ importedGroup ->getId ());
58+ $ groups ->delete (self :: REALM , $ importedGroup ->getId ());
4959
5060 try {
51- $ groups ->get (' master ' , $ importedGroup ->getId ());
61+ $ groups ->get (self :: REALM , $ importedGroup ->getId ());
5262 static ::fail ('Group should not exist anymore ' );
5363 } catch (Exception $ e ) {
5464 static ::assertSame (404 , $ e ->getCode ());
@@ -65,21 +75,21 @@ public function testCreateChildGroup(): void
6575 $ groups = $ this ->getKeycloak ()->groups ();
6676
6777 // Create group
68- $ groups ->create (' master ' , new Group (name: $ importedGroupName ));
69- $ group = $ groups ->all (' master ' )->first ();
78+ $ groups ->create (self :: REALM , new Group (name: $ importedGroupName ));
79+ $ group = $ groups ->all (self :: REALM )->first ();
7080 static ::assertInstanceOf (Group::class, $ group );
7181
7282 // Create child group
73- $ groups ->createChild (' master ' , new Group (name: $ childGroupName ), $ group ->getId ());
74- $ childGroups = $ groups ->children (' master ' , $ group ->getId ());
83+ $ groups ->createChild (self :: REALM , new Group (name: $ childGroupName ), $ group ->getId ());
84+ $ childGroups = $ groups ->children (self :: REALM , $ group ->getId ());
7585 static ::assertCount (1 , $ childGroups );
7686
7787 $ childGroup = $ childGroups ->first ();
7888 static ::assertInstanceOf (Group::class, $ childGroup );
7989 static ::assertSame ($ childGroupName , $ childGroup ->getName ());
8090
8191 // get child group by path
82- $ pathGroup = $ groups ->byPath (' master ' , $ importedGroupName . '/ ' . $ childGroupName );
92+ $ pathGroup = $ groups ->byPath (self :: REALM , $ importedGroupName . '/ ' . $ childGroupName );
8393 static ::assertInstanceOf (Group::class, $ pathGroup );
8494 static ::assertSame ($ childGroup ->getId (), $ pathGroup ->getId ());
8595 }
0 commit comments