|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kreait\Laravel\Firebase\Tests; |
| 6 | + |
| 7 | +use Kreait\Firebase; |
| 8 | +use Roave\BetterReflection\Reflection\ReflectionObject; |
| 9 | + |
| 10 | +/** |
| 11 | + * @internal |
| 12 | + */ |
| 13 | +final class ServiceProviderTest extends TestCase |
| 14 | +{ |
| 15 | + /** @test */ |
| 16 | + public function it_provides_components(): void |
| 17 | + { |
| 18 | + $this->app->config->set('firebase.credentials.file', realpath(__DIR__.'/_fixtures/service_account.json')); |
| 19 | + |
| 20 | + $this->assertInstanceOf(Firebase\Auth::class, $this->app->make(Firebase\Auth::class)); |
| 21 | + $this->assertInstanceOf(Firebase\Database::class, $this->app->make(Firebase\Database::class)); |
| 22 | + $this->assertInstanceOf(Firebase\DynamicLinks::class, $this->app->make(Firebase\DynamicLinks::class)); |
| 23 | + $this->assertInstanceOf(Firebase\Messaging::class, $this->app->make(Firebase\Messaging::class)); |
| 24 | + $this->assertInstanceOf(Firebase\RemoteConfig::class, $this->app->make(Firebase\RemoteConfig::class)); |
| 25 | + $this->assertInstanceOf(Firebase\Storage::class, $this->app->make(Firebase\Storage::class)); |
| 26 | + } |
| 27 | + |
| 28 | + /** @test */ |
| 29 | + public function it_does_not_provide_optional_components(): void |
| 30 | + { |
| 31 | + $this->expectException(\Throwable::class); |
| 32 | + $this->expectDeprecationMessageMatches('/unable/i'); |
| 33 | + |
| 34 | + $this->app->make(Firebase\Firestore::class); |
| 35 | + } |
| 36 | + |
| 37 | + /** @test */ |
| 38 | + public function credentials_can_be_configured(): void |
| 39 | + { |
| 40 | + $credentialsPath = realpath(__DIR__.'/_fixtures/service_account.json'); |
| 41 | + $credentials = json_decode(file_get_contents($credentialsPath), true); |
| 42 | + |
| 43 | + $this->app->config->set('firebase.credentials.file', realpath(__DIR__.'/_fixtures/service_account.json')); |
| 44 | + $factory = $this->app->make(Firebase\Factory::class); |
| 45 | + |
| 46 | + /** @var Firebase\ServiceAccount $serviceAccount */ |
| 47 | + $serviceAccount = ReflectionObject::createFromInstance($factory) |
| 48 | + ->getMethod('getServiceAccount') |
| 49 | + ->invoke($factory); |
| 50 | + |
| 51 | + $this->assertSame($credentials, $serviceAccount->asArray()); |
| 52 | + } |
| 53 | + |
| 54 | + /** @test */ |
| 55 | + public function credential_auto_discovery_is_enabled_by_default(): void |
| 56 | + { |
| 57 | + $factory = $this->app->make(Firebase\Factory::class); |
| 58 | + |
| 59 | + $property = ReflectionObject::createFromInstance($factory)->getProperty('discoveryIsDisabled'); |
| 60 | + $property->setVisibility(\ReflectionProperty::IS_PUBLIC); |
| 61 | + |
| 62 | + $this->assertFalse($property->getValue($factory)); |
| 63 | + } |
| 64 | + |
| 65 | + /** @test */ |
| 66 | + public function credential_auto_discovery_can_be_disabled(): void |
| 67 | + { |
| 68 | + $this->app->config->set('firebase.credentials.auto_discovery', false); |
| 69 | + |
| 70 | + $factory = $this->app->make(Firebase\Factory::class); |
| 71 | + |
| 72 | + $property = ReflectionObject::createFromInstance($factory)->getProperty('discoveryIsDisabled'); |
| 73 | + $property->setVisibility(\ReflectionProperty::IS_PUBLIC); |
| 74 | + |
| 75 | + $this->assertTrue($property->getValue($factory)); |
| 76 | + } |
| 77 | + |
| 78 | + /** @test */ |
| 79 | + public function the_realtime_database_url_can_be_configured(): void |
| 80 | + { |
| 81 | + $this->app->config->set('firebase.database.url', $url = 'https://domain.tld'); |
| 82 | + |
| 83 | + $database = $this->app->make(Firebase\Database::class); |
| 84 | + |
| 85 | + $property = ReflectionObject::createFromInstance($database)->getProperty('uri'); |
| 86 | + $property->setVisibility(\ReflectionProperty::IS_PUBLIC); |
| 87 | + |
| 88 | + $this->assertSame($url, (string) $property->getValue($database)); |
| 89 | + } |
| 90 | + |
| 91 | + /** @test */ |
| 92 | + public function the_dynamic_links_default_domain_can_be_configured(): void |
| 93 | + { |
| 94 | + $this->app->config->set('firebase.dynamic_links.default_domain', $domain = 'https://domain.tld'); |
| 95 | + |
| 96 | + $dynamicLinks = $this->app->make(Firebase\DynamicLinks::class); |
| 97 | + |
| 98 | + $property = ReflectionObject::createFromInstance($dynamicLinks)->getProperty('defaultDynamicLinksDomain'); |
| 99 | + $property->setVisibility(\ReflectionProperty::IS_PUBLIC); |
| 100 | + |
| 101 | + /** @var Firebase\Value\Url $configuredDomain */ |
| 102 | + $configuredDomain = $property->getValue($dynamicLinks); |
| 103 | + |
| 104 | + $this->assertSame($domain, (string) $configuredDomain->toUri()); |
| 105 | + } |
| 106 | + |
| 107 | + /** @test */ |
| 108 | + public function the_storage_default_bucket_can_be_configured(): void |
| 109 | + { |
| 110 | + $this->app->config->set('firebase.storage.default_bucket', $name = 'my-bucket'); |
| 111 | + |
| 112 | + $storage = $this->app->make(Firebase\Storage::class); |
| 113 | + |
| 114 | + $property = ReflectionObject::createFromInstance($storage)->getProperty('defaultBucket'); |
| 115 | + $property->setVisibility(\ReflectionProperty::IS_PUBLIC); |
| 116 | + |
| 117 | + $this->assertSame($name, $property->getValue($storage)); |
| 118 | + } |
| 119 | + |
| 120 | + /** @test */ |
| 121 | + public function it_uses_the_laravel_cache(): void |
| 122 | + { |
| 123 | + $factory = $this->app->make(Firebase\Factory::class); |
| 124 | + |
| 125 | + $property = ReflectionObject::createFromInstance($factory)->getProperty('verifierCache'); |
| 126 | + $property->setVisibility(\ReflectionProperty::IS_PUBLIC); |
| 127 | + |
| 128 | + $this->assertInstanceOf(\Illuminate\Contracts\Cache\Repository::class, $property->getValue($factory)); |
| 129 | + } |
| 130 | + |
| 131 | + /** @test */ |
| 132 | + public function enabling_debug_with_a_boolean_triggers_a_deprecation(): void |
| 133 | + { |
| 134 | + $this->expectException(\Throwable::class); |
| 135 | + $this->expectExceptionMessageMatches('/deprecated/'); |
| 136 | + |
| 137 | + $this->app->config->set('firebase.debug', true); |
| 138 | + $this->app->make(Firebase\Factory::class); |
| 139 | + } |
| 140 | +} |
0 commit comments