File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -116,12 +116,13 @@ public function getServer(int $id): ?array {
116116 $ this ->trustedServersCache = $ this ->dbHandler ->getAllServer ();
117117 }
118118
119- $ server = array_filter ($ this ->trustedServersCache , fn ($ server ) => $ server ['id ' ] === $ id );
120- if (empty ($ server )) {
121- throw new \Exception ('No server found with ID: ' . $ id );
119+ foreach ($ this ->trustedServersCache as $ server ) {
120+ if ($ server ['id ' ] === $ id ) {
121+ return $ server ;
122+ }
122123 }
123124
124- return $ server[ 0 ] ;
125+ throw new \ Exception ( ' No server found with ID: ' . $ id ) ;
125126 }
126127
127128 /**
Original file line number Diff line number Diff line change @@ -164,6 +164,64 @@ public function testGetServers(): void {
164164 );
165165 }
166166
167+ public static function dataTestGetServer () {
168+ return [
169+ [
170+ 15 ,
171+ [
172+ 'id ' => 15 ,
173+ 'otherData ' => 'first server ' ,
174+ ]
175+ ],
176+ [
177+ 16 ,
178+ [
179+ 'id ' => 16 ,
180+ 'otherData ' => 'second server ' ,
181+ ]
182+ ],
183+ [
184+ 42 ,
185+ [
186+ 'id ' => 42 ,
187+ 'otherData ' => 'last server ' ,
188+ ]
189+ ],
190+ [
191+ 108 ,
192+ null
193+ ],
194+ ];
195+ }
196+
197+ #[\PHPUnit \Framework \Attributes \DataProvider('dataTestGetServer ' )]
198+ public function testGetServer (int $ id , ?array $ expectedServer ): void {
199+ $ servers = [
200+ [
201+ 'id ' => 15 ,
202+ 'otherData ' => 'first server ' ,
203+ ],
204+ [
205+ 'id ' => 16 ,
206+ 'otherData ' => 'second server ' ,
207+ ],
208+ [
209+ 'id ' => 42 ,
210+ 'otherData ' => 'last server ' ,
211+ ],
212+ ];
213+ $ this ->dbHandler ->expects ($ this ->once ())->method ('getAllServer ' )->willReturn ($ servers );
214+
215+ if ($ expectedServer === null ) {
216+ $ this ->expectException (\Exception::class);
217+ $ this ->expectExceptionMessage ('No server found with ID: ' . $ id );
218+ }
219+
220+ $ this ->assertEquals (
221+ $ expectedServer ,
222+ $ this ->trustedServers ->getServer ($ id )
223+ );
224+ }
167225
168226 public function testIsTrustedServer (): void {
169227 $ this ->dbHandler ->expects ($ this ->once ())
You can’t perform that action at this time.
0 commit comments