Skip to content

Commit 9cb9b69

Browse files
committed
Add test suite
1 parent c2d10a9 commit 9cb9b69

File tree

7 files changed

+215
-5
lines changed

7 files changed

+215
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
/composer.lock
55
/google-service-account.json
6+
/phpunit.xml

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,26 @@
1111
}
1212
],
1313
"require": {
14+
"php": "^7.2",
1415
"kreait/firebase-php": "^5.0",
1516
"illuminate/contracts": "^5.8|^6.0|^7.0",
1617
"illuminate/support": "^5.8|^6.0|^7.0"
1718
},
1819
"require-dev": {
19-
"laravel/lumen-framework": "^5.8|^6.0|^7.0"
20+
"laravel/lumen-framework": "^5.8|^6.0|^7.0",
21+
"orchestra/testbench": "^3.8.6|^4.8|^5.2",
22+
"roave/better-reflection": "^3.6|^4.6"
2023
},
2124
"autoload": {
2225
"psr-4": {
2326
"Kreait\\Laravel\\Firebase\\": "src"
2427
}
2528
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"Kreait\\Laravel\\Firebase\\Tests\\": "tests"
32+
}
33+
},
2634
"extra": {
2735
"branch-alias": {
2836
"dev-master": "1.x-dev"

phpunit.xml.dist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
beStrictAboutOutputDuringTests="true"
6+
ignoreDeprecatedCodeUnitsFromCodeCoverage="true"
7+
colors="true">
8+
<testsuites>
9+
<testsuite name="Test Suite">
10+
<directory>tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
14+
<filter>
15+
<whitelist processUncoveredFilesFromWhitelist="true">
16+
<directory suffix=".php">src</directory>
17+
<exclude>
18+
<directory>src/Facades</directory>
19+
</exclude>
20+
</whitelist>
21+
</filter>
22+
23+
<php>
24+
<ini name="date.timezone" value="UTC" />
25+
<server name="APP_ENV" value="testing"/>
26+
<server name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
27+
</php>
28+
</phpunit>

src/ServiceProvider.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ final class ServiceProvider extends \Illuminate\Support\ServiceProvider
1212
{
1313
public function boot()
1414
{
15+
// @codeCoverageIgnoreStart
1516
if (!$this->app->runningInConsole()) {
1617
return;
1718
}
1819

1920
if ($this->app instanceof Lumen) {
2021
return;
2122
}
23+
// @codeCoverageIgnoreEnd
2224

2325
$this->publishes([
2426
__DIR__ . '/../config/firebase.php' => $this->app->configPath('firebase.php'),
@@ -27,19 +29,20 @@ public function boot()
2729

2830
public function register()
2931
{
32+
// @codeCoverageIgnoreStart
3033
if ($this->app instanceof Lumen) {
3134
$this->app->configure('firebase');
3235
}
36+
// @codeCoverageIgnoreEnd
3337

3438
$this->mergeConfigFrom(__DIR__.'/../config/firebase.php', 'firebase');
3539

40+
$this->registerFactory();
3641
$this->registerComponents();
3742
}
3843

39-
private function registerComponents()
44+
private function registerComponents(): void
4045
{
41-
$this->registerFactory();
42-
4346
$this->app->singleton(Firebase\Auth::class, static function (Container $app) {
4447
return $app->make(Firebase\Factory::class)->createAuth();
4548
});
@@ -78,7 +81,7 @@ private function registerComponents()
7881
$this->app->alias(Firebase\Storage::class, 'firebase.storage');
7982
}
8083

81-
private function registerFactory()
84+
private function registerFactory(): void
8285
{
8386
$this->app->singleton(Firebase\Factory::class, function (Container $app) {
8487
$factory = new Firebase\Factory();

tests/ServiceProviderTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
}

tests/TestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kreait\Laravel\Firebase\Tests;
6+
7+
use Kreait\Laravel\Firebase;
8+
9+
/**
10+
* @internal
11+
*/
12+
abstract class TestCase extends \Orchestra\Testbench\TestCase
13+
{
14+
protected function getPackageProviders($app)
15+
{
16+
return [Firebase\ServiceProvider::class];
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "project",
4+
"private_key_id": "private_key_id",
5+
"private_key": "-----BEGIN PRIVATE KEY-----\nsome gibberish\n-----END PRIVATE KEY-----\n",
6+
"client_email": "[email protected]",
7+
"client_id": "1234567890",
8+
"auth_uri": "https://some.google.tld/o/oauth2/auth",
9+
"token_uri": "https://some.google.tld/o/oauth2/token",
10+
"auth_provider_x509_cert_url": "https://some.google.tld/oauth2/v1/certs",
11+
"client_x509_cert_url": "https://some.google.tld/robot/v1/metadata/x509/user%40project.iam.gserviceaccount.com"
12+
}

0 commit comments

Comments
 (0)