From 7d8e994bf9cf6714a66b4aef9f5bc85df268cb64 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Fri, 18 Aug 2023 09:48:49 -0700 Subject: [PATCH] Stop passing nullable values to Future.value --- .../test/firebase_auth_test.dart | 42 ++++---- .../firebase_auth/test/user_test.dart | 98 +++++++++---------- 2 files changed, 70 insertions(+), 70 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/test/firebase_auth_test.dart b/packages/firebase_auth/firebase_auth/test/firebase_auth_test.dart index 04f71f5bb1c0..47df9161555b 100644 --- a/packages/firebase_auth/firebase_auth/test/firebase_auth_test.dart +++ b/packages/firebase_auth/firebase_auth/test/firebase_auth_test.dart @@ -63,12 +63,12 @@ void main() { ], ); - MockUserPlatform? mockUserPlatform; - MockUserCredentialPlatform? mockUserCredPlatform; - MockConfirmationResultPlatform? mockConfirmationResultPlatform; - MockRecaptchaVerifier? mockVerifier; - AdditionalUserInfo? mockAdditionalUserInfo; - EmailAuthCredential? mockCredential; + MockUserPlatform mockUserPlatform; + MockUserCredentialPlatform mockUserCredPlatform; + MockConfirmationResultPlatform mockConfirmationResultPlatform; + late MockRecaptchaVerifier mockVerifier; + AdditionalUserInfo mockAdditionalUserInfo; + EmailAuthCredential mockCredential; MockFirebaseAuth mockAuthPlatform = MockFirebaseAuth(); @@ -109,14 +109,14 @@ void main() { ) as EmailAuthCredential; mockUserCredPlatform = MockUserCredentialPlatform( FirebaseAuthPlatform.instance, - mockAdditionalUserInfo!, - mockCredential!, - mockUserPlatform!, + mockAdditionalUserInfo, + mockCredential, + mockUserPlatform, ); mockVerifier = MockRecaptchaVerifier(); when(mockAuthPlatform.signInAnonymously()) - .thenAnswer((_) async => mockUserCredPlatform!); + .thenAnswer((_) async => mockUserCredPlatform); when(mockAuthPlatform.signInWithCredential(any)).thenAnswer( (_) => Future.value(mockUserCredPlatform)); @@ -138,39 +138,39 @@ void main() { )).thenAnswer((_) => mockAuthPlatform); when(mockAuthPlatform.createUserWithEmailAndPassword(any, any)) - .thenAnswer((_) async => mockUserCredPlatform!); + .thenAnswer((_) async => mockUserCredPlatform); when(mockAuthPlatform.getRedirectResult()) - .thenAnswer((_) async => mockUserCredPlatform!); + .thenAnswer((_) async => mockUserCredPlatform); when(mockAuthPlatform.signInWithCustomToken(any)) - .thenAnswer((_) async => mockUserCredPlatform!); + .thenAnswer((_) async => mockUserCredPlatform); when(mockAuthPlatform.signInWithEmailAndPassword(any, any)) - .thenAnswer((_) async => mockUserCredPlatform!); + .thenAnswer((_) async => mockUserCredPlatform); when(mockAuthPlatform.signInWithEmailLink(any, any)) - .thenAnswer((_) async => mockUserCredPlatform!); + .thenAnswer((_) async => mockUserCredPlatform); when(mockAuthPlatform.signInWithPhoneNumber(any, any)) - .thenAnswer((_) async => mockConfirmationResultPlatform!); + .thenAnswer((_) async => mockConfirmationResultPlatform); - when(mockVerifier!.delegate).thenReturn(mockVerifier!.mockDelegate); + when(mockVerifier.delegate).thenReturn(mockVerifier.mockDelegate); when(mockAuthPlatform.signInWithPopup(any)) - .thenAnswer((_) async => mockUserCredPlatform!); + .thenAnswer((_) async => mockUserCredPlatform); when(mockAuthPlatform.signInWithRedirect(any)) .thenAnswer((_) async => mockUserCredPlatform); when(mockAuthPlatform.authStateChanges()).thenAnswer((_) => - Stream.fromIterable([mockUserPlatform!])); + Stream.fromIterable([mockUserPlatform])); when(mockAuthPlatform.idTokenChanges()).thenAnswer((_) => - Stream.fromIterable([mockUserPlatform!])); + Stream.fromIterable([mockUserPlatform])); when(mockAuthPlatform.userChanges()).thenAnswer((_) => - Stream.fromIterable([mockUserPlatform!])); + Stream.fromIterable([mockUserPlatform])); TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger .setMockMethodCallHandler(MethodChannelFirebaseAuth.channel, diff --git a/packages/firebase_auth/firebase_auth/test/user_test.dart b/packages/firebase_auth/firebase_auth/test/user_test.dart index 6b5ebae89521..bd8ca7caa9cf 100644 --- a/packages/firebase_auth/firebase_auth/test/user_test.dart +++ b/packages/firebase_auth/firebase_auth/test/user_test.dart @@ -29,7 +29,7 @@ Map kMockUser1 = { void main() { setupFirebaseAuthMocks(); - FirebaseAuth? auth; + late FirebaseAuth auth; final kMockIdTokenResult = PigeonIdTokenResult( token: '12345', @@ -68,8 +68,8 @@ void main() { } ], ); - MockUserPlatform? mockUserPlatform; - MockUserCredentialPlatform? mockUserCredPlatform; + late MockUserPlatform mockUserPlatform; + late MockUserCredentialPlatform mockUserCredPlatform; AdditionalUserInfo mockAdditionalInfo = AdditionalUserInfo( isNewUser: false, @@ -85,7 +85,7 @@ void main() { var mockAuthPlatform = MockFirebaseAuth(); group('$User', () { - PigeonUserDetails? user; + late PigeonUserDetails user; // used to generate a unique application name for each test var testCount = 0; @@ -108,13 +108,13 @@ void main() { user = kMockUser; - mockUserPlatform = MockUserPlatform(mockAuthPlatform, user!); + mockUserPlatform = MockUserPlatform(mockAuthPlatform, user); mockUserCredPlatform = MockUserCredentialPlatform( FirebaseAuthPlatform.instance, mockAdditionalInfo, mockCredential, - mockUserPlatform!, + mockUserPlatform, ); when(mockAuthPlatform.signInAnonymously()).thenAnswer( @@ -145,42 +145,42 @@ void main() { setUp(() async { user = kMockUser; - await auth!.signInAnonymously(); + await auth.signInAnonymously(); }); test('delete()', () async { // Necessary as we otherwise get a "null is not a Future" error - when(mockUserPlatform!.delete()).thenAnswer((i) async {}); + when(mockUserPlatform.delete()).thenAnswer((i) async {}); - await auth!.currentUser!.delete(); + await auth.currentUser!.delete(); - verify(mockUserPlatform!.delete()); + verify(mockUserPlatform.delete()); }); test('getIdToken()', () async { // Necessary as we otherwise get a "null is not a Future" error - when(mockUserPlatform!.getIdToken(any)).thenAnswer((_) async => 'token'); + when(mockUserPlatform.getIdToken(any)).thenAnswer((_) async => 'token'); - final token = await auth!.currentUser!.getIdToken(true); + final token = await auth.currentUser!.getIdToken(true); - verify(mockUserPlatform!.getIdToken(true)); + verify(mockUserPlatform.getIdToken(true)); expect(token, isA()); }); test('getIdTokenResult()', () async { - when(mockUserPlatform!.getIdTokenResult(any)) + when(mockUserPlatform.getIdTokenResult(any)) .thenAnswer((_) async => IdTokenResult(kMockIdTokenResult)); - final idTokenResult = await auth!.currentUser!.getIdTokenResult(true); + final idTokenResult = await auth.currentUser!.getIdTokenResult(true); - verify(mockUserPlatform!.getIdTokenResult(true)); + verify(mockUserPlatform.getIdTokenResult(true)); expect(idTokenResult, isA()); }); group('linkWithCredential()', () { setUp(() { - when(mockUserPlatform!.linkWithCredential(any)) - .thenAnswer((_) async => mockUserCredPlatform!); + when(mockUserPlatform.linkWithCredential(any)) + .thenAnswer((_) async => mockUserCredPlatform); }); test('should call linkWithCredential()', () async { @@ -189,15 +189,15 @@ void main() { EmailAuthProvider.credential(email: newEmail, password: 'test') as EmailAuthCredential; - await auth!.currentUser!.linkWithCredential(credential); + await auth.currentUser!.linkWithCredential(credential); - verify(mockUserPlatform!.linkWithCredential(credential)); + verify(mockUserPlatform.linkWithCredential(credential)); }); }); group('reauthenticateWithCredential()', () { setUp(() { - when(mockUserPlatform!.reauthenticateWithCredential(any)) + when(mockUserPlatform.reauthenticateWithCredential(any)) .thenAnswer((_) => Future.value(mockUserCredPlatform)); }); test('should call reauthenticateWithCredential()', () async { @@ -206,91 +206,91 @@ void main() { EmailAuthProvider.credential(email: newEmail, password: 'test') as EmailAuthCredential; - await auth!.currentUser!.reauthenticateWithCredential(credential); + await auth.currentUser!.reauthenticateWithCredential(credential); - verify(mockUserPlatform!.reauthenticateWithCredential(credential)); + verify(mockUserPlatform.reauthenticateWithCredential(credential)); }); }); test('reload()', () async { // Necessary as we otherwise get a "null is not a Future" error - when(mockUserPlatform!.reload()).thenAnswer((i) async {}); + when(mockUserPlatform.reload()).thenAnswer((i) async {}); - await auth!.currentUser!.reload(); + await auth.currentUser!.reload(); - verify(mockUserPlatform!.reload()); + verify(mockUserPlatform.reload()); }); test('sendEmailVerification()', () async { // Necessary as we otherwise get a "null is not a Future" error - when(mockUserPlatform!.sendEmailVerification(any)) + when(mockUserPlatform.sendEmailVerification(any)) .thenAnswer((i) async {}); final ActionCodeSettings actionCodeSettings = ActionCodeSettings(url: 'test'); - await auth!.currentUser!.sendEmailVerification(actionCodeSettings); + await auth.currentUser!.sendEmailVerification(actionCodeSettings); - verify(mockUserPlatform!.sendEmailVerification(actionCodeSettings)); + verify(mockUserPlatform.sendEmailVerification(actionCodeSettings)); }); group('unlink()', () { setUp(() { - when(mockUserPlatform!.unlink(any)) + when(mockUserPlatform.unlink(any)) .thenAnswer((_) => Future.value(mockUserPlatform)); }); test('should call unlink()', () async { const String providerId = 'providerId'; - await auth!.currentUser!.unlink(providerId); + await auth.currentUser!.unlink(providerId); - verify(mockUserPlatform!.unlink(providerId)); + verify(mockUserPlatform.unlink(providerId)); }); }); group('updateEmail()', () { test('should call updateEmail()', () async { // Necessary as we otherwise get a "null is not a Future" error - when(mockUserPlatform!.updateEmail(any)).thenAnswer((i) async {}); + when(mockUserPlatform.updateEmail(any)).thenAnswer((i) async {}); const String newEmail = 'newEmail'; - await auth!.currentUser!.updateEmail(newEmail); + await auth.currentUser!.updateEmail(newEmail); - verify(mockUserPlatform!.updateEmail(newEmail)); + verify(mockUserPlatform.updateEmail(newEmail)); }); }); group('updatePassword()', () { test('should call updatePassword()', () async { // Necessary as we otherwise get a "null is not a Future" error - when(mockUserPlatform!.updatePassword(any)).thenAnswer((i) async {}); + when(mockUserPlatform.updatePassword(any)).thenAnswer((i) async {}); const String newPassword = 'newPassword'; - await auth!.currentUser!.updatePassword(newPassword); + await auth.currentUser!.updatePassword(newPassword); - verify(mockUserPlatform!.updatePassword(newPassword)); + verify(mockUserPlatform.updatePassword(newPassword)); }); }); group('updatePhoneNumber()', () { test('should call updatePhoneNumber()', () async { // Necessary as we otherwise get a "null is not a Future" error - when(mockUserPlatform!.updatePhoneNumber(any)).thenAnswer((i) async {}); + when(mockUserPlatform.updatePhoneNumber(any)).thenAnswer((i) async {}); PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.credential( verificationId: 'test', smsCode: 'test', ); - await auth!.currentUser!.updatePhoneNumber(phoneAuthCredential); + await auth.currentUser!.updatePhoneNumber(phoneAuthCredential); - verify(mockUserPlatform!.updatePhoneNumber(phoneAuthCredential)); + verify(mockUserPlatform.updatePhoneNumber(phoneAuthCredential)); }); }); test('updateProfile()', () async { // Necessary as we otherwise get a "null is not a Future" error - when(mockUserPlatform!.updateProfile(any)).thenAnswer((i) async {}); + when(mockUserPlatform.updateProfile(any)).thenAnswer((i) async {}); const String displayName = 'updatedName'; const String photoURL = 'testUrl'; @@ -299,33 +299,33 @@ void main() { 'photoURL': photoURL }; - await auth!.currentUser! + await auth.currentUser! // ignore: deprecated_member_use_from_same_package .updateProfile(displayName: displayName, photoURL: photoURL); - verify(mockUserPlatform!.updateProfile(data)); + verify(mockUserPlatform.updateProfile(data)); }); group('verifyBeforeUpdateEmail()', () { test('should call verifyBeforeUpdateEmail()', () async { // Necessary as we otherwise get a "null is not a Future" error - when(mockUserPlatform!.verifyBeforeUpdateEmail(any, any)) + when(mockUserPlatform.verifyBeforeUpdateEmail(any, any)) .thenAnswer((i) async {}); const newEmail = 'new@email.com'; ActionCodeSettings actionCodeSettings = ActionCodeSettings(url: 'test'); - await auth!.currentUser! + await auth.currentUser! .verifyBeforeUpdateEmail(newEmail, actionCodeSettings); - verify(mockUserPlatform! + verify(mockUserPlatform .verifyBeforeUpdateEmail(newEmail, actionCodeSettings)); }); }); test('toString()', () async { when(mockAuthPlatform.currentUser).thenReturn(TestUserPlatform( - mockAuthPlatform, TestMultiFactorPlatform(mockAuthPlatform), user!)); + mockAuthPlatform, TestMultiFactorPlatform(mockAuthPlatform), user)); const userInfo = 'UserInfo(' 'displayName: Flutter Test User, ' @@ -340,7 +340,7 @@ void main() { 'lastSignInTime: ${DateTime.fromMillisecondsSinceEpoch(kMockLastSignInTimestamp, isUtc: true)})'; expect( - auth!.currentUser.toString(), + auth.currentUser.toString(), 'User(' 'displayName: displayName, ' 'email: null, '