Skip to content

[firebase_core_platform_interface] Migrate to plugin_platform_interface #2065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.4

* Migrate to package:plugin_platform_interface.

## 1.0.3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## 1.0.3
## 1.0.3


* Make the pedantic dev_dependency explicit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'dart:async';

import 'package:meta/meta.dart' show visibleForTesting;
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import 'src/firebase_options.dart';
import 'src/method_channel_firebase_core.dart';
Expand All @@ -22,14 +22,10 @@ export 'src/platform_firebase_app.dart';
/// will get the default implementation, while platform implementations that
/// `implements` this interface will be broken by newly added
/// [FirebaseCorePlatform] methods.
abstract class FirebaseCorePlatform {
/// Only mock implementations should set this to `true`.
///
/// Mockito mocks implement this class with `implements` which is forbidden
/// (see class docs). This property provides a backdoor for mocks to skip the
/// verification that the class isn't implemented with `implements`.
@visibleForTesting
bool get isMock => false;
abstract class FirebaseCorePlatform extends PlatformInterface {
FirebaseCorePlatform() : super(token: _token);

static final Object _token = Object();

/// The default instance of [FirebaseCorePlatform] to use.
///
Expand All @@ -44,26 +40,10 @@ abstract class FirebaseCorePlatform {
// TODO(amirh): Extract common platform interface logic.
// https://github.com/flutter/flutter/issues/43368
static set instance(FirebaseCorePlatform instance) {
if (!instance.isMock) {
try {
instance._verifyProvidesDefaultImplementations();
} on NoSuchMethodError catch (_) {
throw AssertionError(
'Platform interfaces must not be implemented with `implements`');
}
}
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}

/// This method ensures that [FirebaseCorePlatform] isn't implemented with `implements`.
///
/// See class docs for more details on why using `implements` to implement
/// [FirebaseCorePlatform] is forbidden.
///
/// This private method is called by the [instance] setter, which should fail
/// if the provided instance is a class implemented with `implements`.
void _verifyProvidesDefaultImplementations() {}

/// Returns the data for the Firebase app with the given [name].
///
/// If there is no such app, returns null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ description: A common platform interface for the firebase_core plugin.
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_core/firebase_core_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.0.3
version: 1.0.4

dependencies:
flutter:
sdk: flutter
meta: ^1.0.5
plugin_platform_interface: ^1.0.2
quiver: ">=2.0.0 <3.0.0"

dev_dependencies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

void main() {
group('$FirebaseCorePlatform', () {
Expand All @@ -15,23 +16,33 @@ void main() {
test('Cannot be implemented with `implements`', () {
expect(() {
FirebaseCorePlatform.instance = ImplementsFirebaseCorePlatform();
}, throwsAssertionError);
}, throwsNoSuchMethodError);
});

test('Can be extended', () {
FirebaseCorePlatform.instance = ExtendsFirebaseCorePlatform();
});

test('Can be mocked with `implements`', () {
final ImplementsFirebaseCorePlatform mock =
ImplementsFirebaseCorePlatform();
when(mock.isMock).thenReturn(true);
final FirebaseCoreMockPlatform mock = FirebaseCoreMockPlatform();
FirebaseCorePlatform.instance = mock;
});
});
}

class ImplementsFirebaseCorePlatform extends Mock
implements FirebaseCorePlatform {}
class ImplementsFirebaseCorePlatform implements FirebaseCorePlatform {
@override
Future<List<PlatformFirebaseApp>> allApps() => null;

@override
Future<PlatformFirebaseApp> appNamed(String name) => null;

@override
Future<void> configure(String name, FirebaseOptions options) => null;
}

class ExtendsFirebaseCorePlatform extends FirebaseCorePlatform {}

class FirebaseCoreMockPlatform extends Mock
with MockPlatformInterfaceMixin
implements FirebaseCorePlatform {}