44
55namespace Kreait \Laravel \Firebase \Tests ;
66
7+ use Illuminate \Contracts \Cache \Repository ;
78use Kreait \Firebase ;
89use Kreait \Firebase \Exception \InvalidArgumentException ;
910use Kreait \Firebase \Factory ;
1011use Kreait \Laravel \Firebase \FirebaseProjectManager ;
11- use Roave \ BetterReflection \ Reflection \ ReflectionObject ;
12+ use ReflectionObject ;
1213
1314/**
1415 * @internal
1516 */
1617final class FirebaseProjectManagerTest extends TestCase
1718{
18- protected function factoryForProject (?string $ project = null ): Factory
19+ private function factoryForProject (?string $ project = null ): Factory
1920 {
20- $ manager = $ this ->app ->make (FirebaseProjectManager::class);
21- $ project = $ manager ->project ($ project );
22-
23- $ factory = ReflectionObject::createFromInstance ($ project )->getProperty ('factory ' );
21+ $ project = $ this ->app ->make (FirebaseProjectManager::class)->project ($ project );
2422
25- return $ factory ->getValue ($ project );
23+ return $ this -> getAccessibleProperty ( $ project , ' factory ' ) ->getValue ($ project );
2624 }
2725
2826 /**
@@ -36,9 +34,7 @@ public function a_project_configuration_has_to_exist(): void
3634
3735 $ this ->expectException (InvalidArgumentException::class);
3836
39- ReflectionObject::createFromInstance ($ manager )
40- ->getMethod ('configuration ' )
41- ->invoke ($ manager , $ projectName );
37+ $ this ->getAccessibleMethod ($ manager , 'configuration ' )->invoke ($ manager , $ projectName );
4238 }
4339
4440 /**
@@ -84,9 +80,7 @@ public function credentials_can_be_configured(): void
8480
8581 // Retrieve service account
8682 /** @var Firebase\ServiceAccount $serviceAccount */
87- $ serviceAccount = ReflectionObject::createFromInstance ($ factory )
88- ->getMethod ('getServiceAccount ' )
89- ->invoke ($ factory );
83+ $ serviceAccount = $ this ->getAccessibleMethod ($ factory , 'getServiceAccount ' )->invoke ($ factory );
9084
9185 // Validate value
9286 $ this ->assertSame ($ credentials , $ serviceAccount ->asArray ());
@@ -102,7 +96,7 @@ public function a_tenant_id_can_be_set(): void
10296 $ auth = $ this ->app ->make (Firebase \Auth::class);
10397
10498 /** @var Firebase\Auth\TenantId|null $tenantId */
105- $ tenantId = ReflectionObject:: createFromInstance ( $ auth )-> getProperty ( 'tenantId ' )->getValue ($ auth );
99+ $ tenantId = $ this -> getAccessibleProperty ( $ auth , 'tenantId ' )->getValue ($ auth );
106100
107101 $ this ->assertInstanceOf (Firebase \Auth \TenantId::class, $ tenantId );
108102
@@ -134,14 +128,10 @@ public function projects_can_have_different_credentials(): void
134128 $ secondFactory = $ this ->factoryForProject ($ secondProjectName );
135129
136130 /** @var Firebase\ServiceAccount $serviceAccount */
137- $ serviceAccount = ReflectionObject::createFromInstance ($ factory )
138- ->getMethod ('getServiceAccount ' )
139- ->invoke ($ factory );
131+ $ serviceAccount = $ this ->getAccessibleMethod ($ factory , 'getServiceAccount ' )->invoke ($ factory );
140132
141133 /** @var Firebase\ServiceAccount $secondServiceAccount */
142- $ secondServiceAccount = ReflectionObject::createFromInstance ($ secondFactory )
143- ->getMethod ('getServiceAccount ' )
144- ->invoke ($ secondFactory );
134+ $ secondServiceAccount = $ this ->getAccessibleMethod ($ factory , 'getServiceAccount ' )->invoke ($ secondFactory );
145135
146136 // Validate values
147137 $ this ->assertSame ($ credentials , $ serviceAccount ->asArray ());
@@ -157,8 +147,9 @@ public function credential_auto_discovery_is_enabled_by_default_for_default_proj
157147
158148 $ factory = $ this ->factoryForProject ($ projectName );
159149
160- $ property = ReflectionObject::createFromInstance ($ factory )->getProperty ('discoveryIsDisabled ' );
161- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
150+ $ this ->getAccessibleMethod ($ factory , 'getServiceAccount ' )->invoke ($ factory );
151+
152+ $ property = $ this ->getAccessibleProperty ($ factory , 'discoveryIsDisabled ' );
162153
163154 $ this ->assertFalse ($ property ->getValue ($ factory ));
164155 }
@@ -174,8 +165,7 @@ public function credential_auto_discovery_can_be_disabled_for_default_project():
174165
175166 $ factory = $ this ->factoryForProject ($ projectName );
176167
177- $ property = ReflectionObject::createFromInstance ($ factory )->getProperty ('discoveryIsDisabled ' );
178- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
168+ $ property = $ this ->getAccessibleProperty ($ factory , 'discoveryIsDisabled ' );
179169
180170 $ this ->assertTrue ($ property ->getValue ($ factory ));
181171 }
@@ -191,8 +181,7 @@ public function credential_auto_discovery_is_not_enabled_by_default_for_other_pr
191181
192182 $ factory = $ this ->factoryForProject ($ projectName ); // factory for default project with default settings
193183
194- $ property = ReflectionObject::createFromInstance ($ factory )->getProperty ('discoveryIsDisabled ' );
195- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
184+ $ property = $ this ->getAccessibleProperty ($ factory , 'discoveryIsDisabled ' );
196185
197186 $ this ->assertTrue ($ property ->getValue ($ factory ));
198187 }
@@ -208,8 +197,7 @@ public function credential_auto_discovery_can_be_enabled_for_other_project(): vo
208197
209198 $ factory = $ this ->factoryForProject ($ projectName );
210199
211- $ property = ReflectionObject::createFromInstance ($ factory )->getProperty ('discoveryIsDisabled ' );
212- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
200+ $ property = $ this ->getAccessibleProperty ($ factory , 'discoveryIsDisabled ' );
213201
214202 $ this ->assertFalse ($ property ->getValue ($ factory ));
215203 }
@@ -225,8 +213,7 @@ public function the_realtime_database_url_can_be_configured(): void
225213
226214 $ database = $ this ->app ->make (Firebase \Database::class);
227215
228- $ property = ReflectionObject::createFromInstance ($ database )->getProperty ('uri ' );
229- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
216+ $ property = $ this ->getAccessibleProperty ($ database , 'uri ' );
230217
231218 $ this ->assertSame ($ url , (string ) $ property ->getValue ($ database ));
232219 }
@@ -241,8 +228,7 @@ public function the_dynamic_links_default_domain_can_be_configured(): void
241228
242229 $ dynamicLinks = $ this ->app ->make (Firebase \DynamicLinks::class);
243230
244- $ property = ReflectionObject::createFromInstance ($ dynamicLinks )->getProperty ('defaultDynamicLinksDomain ' );
245- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
231+ $ property = $ this ->getAccessibleProperty ($ dynamicLinks , 'defaultDynamicLinksDomain ' );
246232
247233 /** @var Firebase\Value\Url $configuredDomain */
248234 $ configuredDomain = $ property ->getValue ($ dynamicLinks );
@@ -260,8 +246,7 @@ public function the_storage_default_bucket_can_be_configured(): void
260246
261247 $ storage = $ this ->app ->make (Firebase \Storage::class);
262248
263- $ property = ReflectionObject::createFromInstance ($ storage )->getProperty ('defaultBucket ' );
264- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
249+ $ property = $ this ->getAccessibleProperty ($ storage , 'defaultBucket ' );
265250
266251 $ this ->assertSame ($ name , $ property ->getValue ($ storage ));
267252 }
@@ -276,8 +261,7 @@ public function logging_can_be_configured(): void
276261
277262 $ factory = $ this ->factoryForProject ($ projectName );
278263
279- $ property = ReflectionObject::createFromInstance ($ factory )->getProperty ('httpLogMiddleware ' );
280- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
264+ $ property = $ this ->getAccessibleProperty ($ factory , 'httpLogMiddleware ' );
281265
282266 $ this ->assertNotNull ($ property ->getValue ($ factory ));
283267 }
@@ -292,8 +276,7 @@ public function debug_logging_can_be_configured(): void
292276
293277 $ factory = $ this ->factoryForProject ($ projectName );
294278
295- $ property = ReflectionObject::createFromInstance ($ factory )->getProperty ('httpDebugLogMiddleware ' );
296- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
279+ $ property = $ this ->getAccessibleProperty ($ factory , 'httpDebugLogMiddleware ' );
297280
298281 $ this ->assertNotNull ($ property ->getValue ($ factory ));
299282 }
@@ -309,11 +292,8 @@ public function http_client_options_can_be_configured(): void
309292
310293 $ factory = $ this ->factoryForProject ($ projectName );
311294
312- $ property = ReflectionObject::createFromInstance ($ factory )->getProperty ('httpClientOptions ' );
313- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
314-
315295 /** @var Firebase\Http\HttpClientOptions $httpClientOptions */
316- $ httpClientOptions = $ property ->getValue ($ factory );
296+ $ httpClientOptions = $ this -> getAccessibleProperty ( $ factory , ' httpClientOptions ' ) ->getValue ($ factory );
317297
318298 $ this ->assertSame ('proxy.domain.tld ' , $ httpClientOptions ->proxy ());
319299 $ this ->assertSame (1.23 , $ httpClientOptions ->timeout ());
@@ -327,9 +307,24 @@ public function it_uses_the_laravel_cache(): void
327307 $ projectName = $ this ->app ->config ->get ('firebase.default ' );
328308 $ factory = $ this ->factoryForProject ($ projectName );
329309
330- $ property = ReflectionObject::createFromInstance ($ factory )->getProperty ('verifierCache ' );
331- $ property ->setVisibility (\ReflectionProperty::IS_PUBLIC );
310+ $ property = $ this ->getAccessibleProperty ($ factory , 'verifierCache ' );
311+
312+ $ this ->assertInstanceOf (Repository::class, $ property ->getValue ($ factory ));
313+ }
314+
315+ private function getAccessibleProperty (object $ object , string $ propertyName ): \ReflectionProperty
316+ {
317+ $ property = (new ReflectionObject ($ object ))->getProperty ($ propertyName );
318+ $ property ->setAccessible (true );
319+
320+ return $ property ;
321+ }
322+
323+ private function getAccessibleMethod (object $ object , string $ methodName ): \ReflectionMethod
324+ {
325+ $ property = (new ReflectionObject ($ object ))->getMethod ($ methodName );
326+ $ property ->setAccessible (true );
332327
333- $ this -> assertInstanceOf (\ Illuminate \ Contracts \ Cache \Repository::class, $ property-> getValue ( $ factory )) ;
328+ return $ property ;
334329 }
335330}
0 commit comments