@@ -39,21 +39,26 @@ protected function setUp(): void {
3939 $ this ->groupManager = $ this ->createMock (IGroupManager::class);
4040 $ this ->userManager = $ this ->createMock (IUserManager::class);
4141 $ this ->command = new AddUser ($ this ->userManager , $ this ->groupManager );
42+ $ this ->output = $ this ->createMock (OutputInterface::class);
43+ }
4244
45+ protected function configureInput (array |string $ returnGroup , array |string $ returnUser ): void {
4346 $ this ->input = $ this ->createMock (InputInterface::class);
4447 $ this ->input ->method ('getArgument ' )
45- ->willReturnCallback (function ($ arg ) {
48+ ->willReturnCallback (function ($ arg ) use ( $ returnGroup , $ returnUser ) {
4649 if ($ arg === 'group ' ) {
47- return 'myGroup ' ;
48- } elseif ($ arg === 'user ' ) {
49- return 'myUser ' ;
50+ return $ returnGroup ;
51+ }
52+ if ($ arg === 'user ' ) {
53+ return $ returnUser ;
5054 }
5155 throw new \Exception ();
5256 });
53- $ this ->output = $ this ->createMock (OutputInterface::class);
5457 }
5558
5659 public function testNoGroup (): void {
60+ $ this ->configureInput ('myGroup ' , 'myUser ' );
61+
5762 $ this ->groupManager ->method ('get ' )
5863 ->with ('myGroup ' )
5964 ->willReturn (null );
@@ -66,6 +71,8 @@ public function testNoGroup(): void {
6671 }
6772
6873 public function testNoUser (): void {
74+ $ this ->configureInput ('myGroup ' , 'myUser ' );
75+
6976 $ group = $ this ->createMock (IGroup::class);
7077 $ this ->groupManager ->method ('get ' )
7178 ->with ('myGroup ' )
@@ -77,12 +84,14 @@ public function testNoUser(): void {
7784
7885 $ this ->output ->expects ($ this ->once ())
7986 ->method ('writeln ' )
80- ->with ('<error>user not found</error> ' );
87+ ->with ('<error>user myUser not found</error> ' );
8188
8289 $ this ->invokePrivate ($ this ->command , 'execute ' , [$ this ->input , $ this ->output ]);
8390 }
8491
8592 public function testAdd (): void {
93+ $ this ->configureInput ('myGroup ' , 'myUser ' );
94+
8695 $ group = $ this ->createMock (IGroup::class);
8796 $ this ->groupManager ->method ('get ' )
8897 ->with ('myGroup ' )
@@ -99,4 +108,66 @@ public function testAdd(): void {
99108
100109 $ this ->invokePrivate ($ this ->command , 'execute ' , [$ this ->input , $ this ->output ]);
101110 }
111+
112+ public function testAddMultiple (): void {
113+ $ this ->configureInput ('myGroup ' , ['myUser ' , 'myOtherUser ' ]);
114+
115+ $ group = $ this ->createMock (IGroup::class);
116+ $ this ->groupManager ->method ('get ' )
117+ ->with ('myGroup ' )
118+ ->willReturn ($ group );
119+
120+ $ user1 = $ this ->createMock (IUser::class);
121+ $ user2 = $ this ->createMock (IUser::class);
122+ $ this ->userManager ->method ('get ' )
123+ ->willReturnMap ([
124+ ['myUser ' , $ user1 ],
125+ ['myOtherUser ' , $ user2 ],
126+ ]);
127+
128+ $ group ->expects ($ this ->exactly (2 ))
129+ ->method ('addUser ' )
130+ ->with ($ this ->callback (static fn (IUser $ user ): bool => in_array ($ user , [$ user1 , $ user2 ], true )));
131+
132+ $ this ->output ->expects ($ this ->exactly (2 ))
133+ ->method ('writeln ' )
134+ ->with ($ this ->callback (static fn (string $ message ): bool => in_array ($ message ,
135+ [
136+ '<info>user myUser added</info> ' ,
137+ '<info>user myOtherUser added</info> ' ,
138+ ], true )));
139+
140+ $ this ->invokePrivate ($ this ->command , 'execute ' , [$ this ->input , $ this ->output ]);
141+ }
142+
143+ public function testAddMultiplePartialSuccess (): void {
144+ $ this ->configureInput ('myGroup ' , ['myUser ' , 'myOtherUser ' ]);
145+
146+ $ group = $ this ->createMock (IGroup::class);
147+ $ this ->groupManager ->method ('get ' )
148+ ->with ('myGroup ' )
149+ ->willReturn ($ group );
150+
151+ $ user = $ this ->createMock (IUser::class);
152+ $ this ->userManager ->method ('get ' )
153+ ->willReturnMap ([
154+ ['myUser ' , $ user ],
155+ ['myOtherUser ' , null ],
156+ ]);
157+
158+ $ group ->expects ($ this ->once ())
159+ ->method ('addUser ' )
160+ ->with ($ user );
161+
162+ $ this ->output ->expects ($ this ->exactly (3 ))
163+ ->method ('writeln ' )
164+ ->with ($ this ->callback (static fn (string $ message ): bool => in_array ($ message ,
165+ [
166+ '<info>user myUser added</info> ' ,
167+ '<error>user myOtherUser not found</error> ' ,
168+ '<error>Some users were not found, all others where added to the group.</error> ' ,
169+ ], true )));
170+
171+ $ this ->invokePrivate ($ this ->command , 'execute ' , [$ this ->input , $ this ->output ]);
172+ }
102173}
0 commit comments