remove: deleting serviceprovider implementation
This commit is contained in:
parent
3fbe700c26
commit
51db3705ba
15 changed files with 0 additions and 3752 deletions
|
@ -1,3 +0,0 @@
|
||||||
export 'src/provider/platform_with_providers.dart';
|
|
||||||
export 'src/provider/service_provider.dart';
|
|
||||||
export 'src/provider/service_provider_config.dart';
|
|
|
@ -1,93 +0,0 @@
|
||||||
import 'package:platform_core/core.dart';
|
|
||||||
import 'package:platform_container/container.dart';
|
|
||||||
import 'package:platform_container/mirrors.dart';
|
|
||||||
import 'package:logging/logging.dart';
|
|
||||||
import 'service_provider.dart';
|
|
||||||
import 'service_provider_manager.dart';
|
|
||||||
import 'service_provider_config.dart';
|
|
||||||
import 'provider_discovery.dart';
|
|
||||||
|
|
||||||
class PlatformWithProviders {
|
|
||||||
final Application app;
|
|
||||||
late ServiceProviderManager _serviceProviderManager;
|
|
||||||
late ProviderDiscovery _discovery;
|
|
||||||
late Container container;
|
|
||||||
|
|
||||||
PlatformWithProviders(this.app,
|
|
||||||
{ServiceProviderManager? serviceProviderManager}) {
|
|
||||||
container = Container(MirrorsReflector());
|
|
||||||
app.configure(configureContainer);
|
|
||||||
|
|
||||||
_serviceProviderManager =
|
|
||||||
serviceProviderManager ?? ServiceProviderManager(app, _discovery);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> configureContainer(Application app) async {
|
|
||||||
// Register the container itself
|
|
||||||
container.registerSingleton((c) => container);
|
|
||||||
|
|
||||||
// Configure the container here
|
|
||||||
// For example, you might want to register some services:
|
|
||||||
// container.registerSingleton((c) => SomeService());
|
|
||||||
|
|
||||||
// You might need to set up dependency injection for routes here
|
|
||||||
// app.use((req, res) => container.make<SomeService>().handleRequest(req, res));
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> registerProvidersFromConfig(ServiceProviderConfig config) async {
|
|
||||||
for (var providerType in config.providers) {
|
|
||||||
var provider = _discovery.createInstance(providerType);
|
|
||||||
if (provider != null) {
|
|
||||||
if (config.deferredProviders.containsKey(providerType)) {
|
|
||||||
provider.setDeferred(config.deferredProviders[providerType]!);
|
|
||||||
}
|
|
||||||
if (config.providerConfigs.containsKey(providerType)) {
|
|
||||||
provider.configure(config.providerConfigs[providerType]!);
|
|
||||||
}
|
|
||||||
await _serviceProviderManager.register(provider);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> registerServiceProvider(ServiceProvider provider) async {
|
|
||||||
await _serviceProviderManager.register(provider);
|
|
||||||
}
|
|
||||||
|
|
||||||
void unregisterServiceProvider(Type providerType) {
|
|
||||||
_serviceProviderManager.unregister(providerType);
|
|
||||||
}
|
|
||||||
|
|
||||||
T? getServiceProvider<T extends ServiceProvider>() {
|
|
||||||
return _serviceProviderManager.getProvider<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> bootServiceProviders() async {
|
|
||||||
await _serviceProviderManager.bootAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> bootDeferredServiceProvider(Type providerType) async {
|
|
||||||
await _serviceProviderManager.bootDeferredProvider(providerType);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setServiceProviderLogLevel(Level level) {
|
|
||||||
_serviceProviderManager.setLogLevel(level);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> discoverServiceProviders() async {
|
|
||||||
await _serviceProviderManager.discoverProviders();
|
|
||||||
}
|
|
||||||
|
|
||||||
void registerProviderType(Type type, ServiceProvider Function() factory) {
|
|
||||||
if (_discovery is ManualProviderDiscovery) {
|
|
||||||
(_discovery as ManualProviderDiscovery)
|
|
||||||
.registerProviderType(type, factory);
|
|
||||||
} else {
|
|
||||||
throw UnsupportedError(
|
|
||||||
'Provider type registration is only supported with ManualProviderDiscovery');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
T? getService<T>() {
|
|
||||||
return app.container.make<T>();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,54 +0,0 @@
|
||||||
import 'dart:mirrors' as mirrors;
|
|
||||||
import 'service_provider.dart';
|
|
||||||
|
|
||||||
abstract class ProviderDiscovery {
|
|
||||||
List<Type> discoverProviders();
|
|
||||||
ServiceProvider? createInstance(Type type);
|
|
||||||
}
|
|
||||||
|
|
||||||
class MirrorProviderDiscovery implements ProviderDiscovery {
|
|
||||||
@override
|
|
||||||
List<Type> discoverProviders() {
|
|
||||||
var providers = <Type>[];
|
|
||||||
mirrors.currentMirrorSystem().libraries.values.forEach((lib) {
|
|
||||||
for (var declaration in lib.declarations.values) {
|
|
||||||
if (declaration is mirrors.ClassMirror &&
|
|
||||||
declaration.isSubclassOf(mirrors.reflectClass(ServiceProvider)) &&
|
|
||||||
!declaration.isAbstract) {
|
|
||||||
providers.add(declaration.reflectedType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return providers;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
ServiceProvider? createInstance(Type type) {
|
|
||||||
var classMirror = mirrors.reflectClass(type);
|
|
||||||
if (classMirror.declarations.containsKey(const Symbol(''))) {
|
|
||||||
var ctor =
|
|
||||||
classMirror.declarations[const Symbol('')] as mirrors.MethodMirror?;
|
|
||||||
if (ctor != null && ctor.isConstructor && ctor.parameters.isEmpty) {
|
|
||||||
return classMirror.newInstance(const Symbol(''), []).reflectee
|
|
||||||
as ServiceProvider;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ManualProviderDiscovery implements ProviderDiscovery {
|
|
||||||
final List<Type> _registeredTypes = [];
|
|
||||||
final Map<Type, ServiceProvider Function()> _factories = {};
|
|
||||||
|
|
||||||
void registerProviderType(Type type, ServiceProvider Function() factory) {
|
|
||||||
_registeredTypes.add(type);
|
|
||||||
_factories[type] = factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Type> discoverProviders() => List.unmodifiable(_registeredTypes);
|
|
||||||
|
|
||||||
@override
|
|
||||||
ServiceProvider? createInstance(Type type) => _factories[type]?.call();
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
import 'package:platform_core/core.dart';
|
|
||||||
import 'package:platform_container/container.dart';
|
|
||||||
|
|
||||||
abstract class ServiceProvider {
|
|
||||||
late Application app;
|
|
||||||
late Container container;
|
|
||||||
late final Map<String, dynamic> _config;
|
|
||||||
bool _isEnabled = true;
|
|
||||||
bool _isDeferred = false;
|
|
||||||
|
|
||||||
ServiceProvider([Map<String, dynamic>? config]) : _config = config ?? {};
|
|
||||||
|
|
||||||
Map<String, dynamic> get config => _config;
|
|
||||||
bool get isEnabled => _isEnabled;
|
|
||||||
bool get isDeferred => _isDeferred;
|
|
||||||
|
|
||||||
void configure(Map<String, dynamic> options) {
|
|
||||||
_config.addAll(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setEnabled(bool enabled) {
|
|
||||||
_isEnabled = enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setDeferred(bool deferred) {
|
|
||||||
_isDeferred = deferred;
|
|
||||||
}
|
|
||||||
|
|
||||||
void registerWithContainer(Container container) {
|
|
||||||
this.container = container;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> register();
|
|
||||||
Future<void> beforeBoot() async {}
|
|
||||||
Future<void> boot();
|
|
||||||
Future<void> afterBoot() async {}
|
|
||||||
|
|
||||||
List<Type> get dependencies => [];
|
|
||||||
}
|
|
|
@ -1,82 +0,0 @@
|
||||||
import 'package:platform_core/core.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider.dart';
|
|
||||||
|
|
||||||
class ServiceProviderConfig {
|
|
||||||
final List<Type> providers;
|
|
||||||
final Map<Type, bool> deferredProviders;
|
|
||||||
final Map<Type, Map<String, dynamic>> providerConfigs;
|
|
||||||
|
|
||||||
ServiceProviderConfig({
|
|
||||||
required this.providers,
|
|
||||||
this.deferredProviders = const {},
|
|
||||||
this.providerConfigs = const {},
|
|
||||||
});
|
|
||||||
|
|
||||||
factory ServiceProviderConfig.fromMap(Map<String, dynamic> map) {
|
|
||||||
final providersList =
|
|
||||||
(map['providers'] as List<dynamic>).cast<Map<String, dynamic>>();
|
|
||||||
|
|
||||||
final providers = <Type>[];
|
|
||||||
final deferredProviders = <Type, bool>{};
|
|
||||||
final providerConfigs = <Type, Map<String, dynamic>>{};
|
|
||||||
|
|
||||||
for (var providerMap in providersList) {
|
|
||||||
final type = _getTypeFromString(providerMap['type'] as String);
|
|
||||||
providers.add(type);
|
|
||||||
|
|
||||||
if (providerMap['deferred'] == true) {
|
|
||||||
deferredProviders[type] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (providerMap['config'] != null) {
|
|
||||||
providerConfigs[type] = (providerMap['config'] as Map<String, dynamic>);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ServiceProviderConfig(
|
|
||||||
providers: providers,
|
|
||||||
deferredProviders: deferredProviders,
|
|
||||||
providerConfigs: providerConfigs,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Type _getTypeFromString(String typeName) {
|
|
||||||
// This is a simple implementation. You might need to expand this
|
|
||||||
// to cover all your service provider types.
|
|
||||||
switch (typeName) {
|
|
||||||
case 'TestServiceProvider':
|
|
||||||
return TestServiceProvider;
|
|
||||||
case 'AnotherServiceProvider':
|
|
||||||
return AnotherServiceProvider;
|
|
||||||
// Add cases for all your service provider types
|
|
||||||
default:
|
|
||||||
throw UnimplementedError(
|
|
||||||
'Type $typeName is not implemented in _getTypeFromString');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Example service provider classes (you should replace these with your actual service providers)
|
|
||||||
class TestServiceProvider extends ServiceProvider {
|
|
||||||
@override
|
|
||||||
Future<void> register() async {
|
|
||||||
// Implementation
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> boot() async {
|
|
||||||
// Implementation
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class AnotherServiceProvider extends ServiceProvider {
|
|
||||||
@override
|
|
||||||
Future<void> register() async {
|
|
||||||
// Implementation
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> boot() async {
|
|
||||||
// Implementation
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,131 +0,0 @@
|
||||||
import 'package:platform_core/core.dart';
|
|
||||||
import 'package:platform_container/container.dart';
|
|
||||||
import 'package:logging/logging.dart';
|
|
||||||
import 'service_provider.dart';
|
|
||||||
import 'provider_discovery.dart';
|
|
||||||
|
|
||||||
class ServiceProviderManager {
|
|
||||||
final Application app;
|
|
||||||
final Container container;
|
|
||||||
final Map<Type, ServiceProvider> _providers = {};
|
|
||||||
final Set<Type> _booted = {};
|
|
||||||
final Logger _logger;
|
|
||||||
final ProviderDiscovery _discovery;
|
|
||||||
|
|
||||||
ServiceProviderManager(this.app, this._discovery)
|
|
||||||
: container = app.container,
|
|
||||||
_logger = Logger('ServiceProviderManager');
|
|
||||||
|
|
||||||
void setLogLevel(Level level) {
|
|
||||||
_logger.level = level;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> register(ServiceProvider provider) async {
|
|
||||||
provider.app = app;
|
|
||||||
provider.registerWithContainer(container);
|
|
||||||
_providers[provider.runtimeType] = provider;
|
|
||||||
try {
|
|
||||||
_logger.info('Registering ${provider.runtimeType}');
|
|
||||||
await provider.register();
|
|
||||||
_logger.info('Registered ${provider.runtimeType}');
|
|
||||||
} catch (e) {
|
|
||||||
_logger.severe('Failed to register ${provider.runtimeType}', e);
|
|
||||||
rethrow;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void unregister(Type providerType) {
|
|
||||||
if (_providers.containsKey(providerType)) {
|
|
||||||
_logger.info('Unregistering $providerType');
|
|
||||||
_providers.remove(providerType);
|
|
||||||
_booted.remove(providerType);
|
|
||||||
} else {
|
|
||||||
_logger.warning(
|
|
||||||
'Attempted to unregister non-existent provider: $providerType');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
T? getProvider<T extends ServiceProvider>() {
|
|
||||||
return _providers[T] as T?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> bootAll() async {
|
|
||||||
for (var providerType in _providers.keys) {
|
|
||||||
await _bootProvider(providerType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _bootProvider(Type providerType) async {
|
|
||||||
if (_booted.contains(providerType)) return;
|
|
||||||
|
|
||||||
var provider = _providers[providerType];
|
|
||||||
if (provider == null) {
|
|
||||||
_logger.severe('Provider not found: $providerType');
|
|
||||||
throw ProviderNotFoundException(providerType);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!provider.isEnabled) {
|
|
||||||
_logger.info('Skipping disabled provider: $providerType');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (provider.isDeferred) {
|
|
||||||
_logger.info('Skipping deferred provider: $providerType');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var dependencyType in provider.dependencies) {
|
|
||||||
await _bootProvider(dependencyType);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
_logger.info('Booting ${provider.runtimeType}');
|
|
||||||
await provider.beforeBoot();
|
|
||||||
await provider.boot();
|
|
||||||
await provider.afterBoot();
|
|
||||||
_booted.add(providerType);
|
|
||||||
_logger.info('Booted ${provider.runtimeType}');
|
|
||||||
} catch (e) {
|
|
||||||
_logger.severe('Failed to boot ${provider.runtimeType}', e);
|
|
||||||
throw ProviderBootException(provider, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> bootDeferredProvider(Type providerType) async {
|
|
||||||
var provider = _providers[providerType];
|
|
||||||
if (provider == null || !provider.isDeferred) {
|
|
||||||
throw ProviderNotFoundException(providerType);
|
|
||||||
}
|
|
||||||
await _bootProvider(providerType);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> discoverProviders() async {
|
|
||||||
for (var type in _discovery.discoverProviders()) {
|
|
||||||
if (!_providers.containsKey(type)) {
|
|
||||||
var instance = _discovery.createInstance(type);
|
|
||||||
if (instance != null) {
|
|
||||||
await register(instance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ProviderNotFoundException implements Exception {
|
|
||||||
final Type providerType;
|
|
||||||
|
|
||||||
ProviderNotFoundException(this.providerType);
|
|
||||||
|
|
||||||
@override
|
|
||||||
String toString() => 'Provider not found: $providerType';
|
|
||||||
}
|
|
||||||
|
|
||||||
class ProviderBootException implements Exception {
|
|
||||||
final ServiceProvider provider;
|
|
||||||
final Object error;
|
|
||||||
|
|
||||||
ProviderBootException(this.provider, this.error);
|
|
||||||
|
|
||||||
@override
|
|
||||||
String toString() => 'Failed to boot ${provider.runtimeType}: $error';
|
|
||||||
}
|
|
|
@ -1,55 +0,0 @@
|
||||||
import 'package:test/test.dart';
|
|
||||||
import 'package:mockito/mockito.dart';
|
|
||||||
import 'package:mockito/annotations.dart';
|
|
||||||
import 'package:platform_core/core.dart';
|
|
||||||
import 'package:platform_container/container.dart';
|
|
||||||
import 'package:platform_core/src/provider/platform_with_providers.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider_manager.dart';
|
|
||||||
import 'package:platform_core/src/provider/provider_discovery.dart';
|
|
||||||
|
|
||||||
@GenerateMocks([ServiceProviderManager, ProviderDiscovery])
|
|
||||||
import 'platform_with_providers_test.mocks.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
group('PlatformWithProviders', () {
|
|
||||||
late PlatformWithProviders platformWithProviders;
|
|
||||||
late Application app;
|
|
||||||
late MockServiceProviderManager mockManager;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
app = Application();
|
|
||||||
mockManager = MockServiceProviderManager();
|
|
||||||
|
|
||||||
platformWithProviders =
|
|
||||||
PlatformWithProviders(app, serviceProviderManager: mockManager);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('PlatformWithProviders initializes correctly', () {
|
|
||||||
expect(platformWithProviders, isNotNull);
|
|
||||||
expect(platformWithProviders.app, equals(app));
|
|
||||||
});
|
|
||||||
|
|
||||||
test('container is created', () {
|
|
||||||
expect(platformWithProviders.container, isNotNull);
|
|
||||||
expect(platformWithProviders.container, isA<Container>());
|
|
||||||
});
|
|
||||||
|
|
||||||
test('registerServiceProvider calls manager.register', () async {
|
|
||||||
final provider = MockServiceProvider();
|
|
||||||
await platformWithProviders.registerServiceProvider(provider);
|
|
||||||
|
|
||||||
verify(mockManager.register(provider)).called(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('bootServiceProviders calls manager.bootAll', () async {
|
|
||||||
await platformWithProviders.bootServiceProviders();
|
|
||||||
|
|
||||||
verify(mockManager.bootAll()).called(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add more tests for other methods in PlatformWithProviders
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
class MockServiceProvider extends Mock implements ServiceProvider {}
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,33 +0,0 @@
|
||||||
import 'package:test/test.dart';
|
|
||||||
import 'package:platform_core/src/provider/provider_discovery.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider.dart';
|
|
||||||
|
|
||||||
class TestServiceProvider extends ServiceProvider {
|
|
||||||
@override
|
|
||||||
Future<void> register() async {}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> boot() async {}
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
group('ManualProviderDiscovery', () {
|
|
||||||
late ManualProviderDiscovery discovery;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
discovery = ManualProviderDiscovery();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('registerProviderType adds type and factory', () {
|
|
||||||
discovery.registerProviderType(
|
|
||||||
TestServiceProvider, () => TestServiceProvider());
|
|
||||||
|
|
||||||
expect(discovery.discoverProviders(), contains(TestServiceProvider));
|
|
||||||
expect(discovery.createInstance(TestServiceProvider),
|
|
||||||
isA<TestServiceProvider>());
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Note: Testing MirrorProviderDiscovery is challenging in a unit test environment
|
|
||||||
// due to its reliance on runtime reflection. Consider integration tests for this.
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
import 'package:test/test.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider_config.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
group('ServiceProviderConfig', () {
|
|
||||||
test('fromMap creates correct config', () {
|
|
||||||
final map = {
|
|
||||||
'providers': [
|
|
||||||
{
|
|
||||||
'type': 'TestServiceProvider',
|
|
||||||
'deferred': true,
|
|
||||||
'config': {'key': 'value'}
|
|
||||||
},
|
|
||||||
{'type': 'AnotherServiceProvider', 'deferred': false},
|
|
||||||
]
|
|
||||||
};
|
|
||||||
|
|
||||||
final config = ServiceProviderConfig.fromMap(map);
|
|
||||||
|
|
||||||
expect(config.providers.length, equals(2));
|
|
||||||
expect(config.deferredProviders.length, equals(1));
|
|
||||||
expect(config.providerConfigs.length, equals(1));
|
|
||||||
// Note: This test assumes you've implemented _getTypeFromString to handle these provider types
|
|
||||||
// You might need to adjust this part of the test based on your actual implementation
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1,87 +0,0 @@
|
||||||
import 'package:test/test.dart';
|
|
||||||
import 'package:platform_core/core.dart';
|
|
||||||
import 'package:platform_core/src/provider/platform_with_providers.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider_config.dart';
|
|
||||||
|
|
||||||
class TestService {
|
|
||||||
String getData() => 'Test Data';
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestServiceProvider extends ServiceProvider {
|
|
||||||
@override
|
|
||||||
Future<void> register() async {
|
|
||||||
container.registerSingleton((container) => TestService());
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> boot() async {
|
|
||||||
await Future.delayed(Duration(milliseconds: 100));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class DeferredServiceProvider extends ServiceProvider {
|
|
||||||
@override
|
|
||||||
Future<void> register() async {
|
|
||||||
container.registerSingleton((container) => 'Deferred Service');
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> boot() async {
|
|
||||||
await Future.delayed(Duration(milliseconds: 100));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
late Application app;
|
|
||||||
late PlatformWithProviders platformWithProviders;
|
|
||||||
|
|
||||||
setUp(() async {
|
|
||||||
app = Application();
|
|
||||||
platformWithProviders = PlatformWithProviders(app);
|
|
||||||
// Allow some time for the platform to initialize
|
|
||||||
await Future.delayed(Duration(milliseconds: 100));
|
|
||||||
});
|
|
||||||
|
|
||||||
tearDown(() async {
|
|
||||||
await app.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
group('Service Provider Integration Tests', () {
|
|
||||||
test('Manual provider registration works', () async {
|
|
||||||
await platformWithProviders
|
|
||||||
.registerServiceProvider(TestServiceProvider());
|
|
||||||
await platformWithProviders.bootServiceProviders();
|
|
||||||
|
|
||||||
var testService = platformWithProviders.container.make<TestService>();
|
|
||||||
expect(testService, isNotNull);
|
|
||||||
expect(testService.getData(), equals('Test Data'));
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Deferred provider is not booted initially', () async {
|
|
||||||
var config = ServiceProviderConfig(
|
|
||||||
providers: [DeferredServiceProvider],
|
|
||||||
deferredProviders: {DeferredServiceProvider: true},
|
|
||||||
);
|
|
||||||
await platformWithProviders.registerProvidersFromConfig(config);
|
|
||||||
await platformWithProviders.bootServiceProviders();
|
|
||||||
|
|
||||||
expect(() => platformWithProviders.container.make<String>(),
|
|
||||||
throwsException);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Deferred provider can be booted on demand', () async {
|
|
||||||
var config = ServiceProviderConfig(
|
|
||||||
providers: [DeferredServiceProvider],
|
|
||||||
deferredProviders: {DeferredServiceProvider: true},
|
|
||||||
);
|
|
||||||
await platformWithProviders.registerProvidersFromConfig(config);
|
|
||||||
await platformWithProviders.bootServiceProviders();
|
|
||||||
await platformWithProviders
|
|
||||||
.bootDeferredServiceProvider(DeferredServiceProvider);
|
|
||||||
|
|
||||||
var deferredService = platformWithProviders.container.make<String>();
|
|
||||||
expect(deferredService, equals('Deferred Service'));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1,93 +0,0 @@
|
||||||
import 'package:test/test.dart';
|
|
||||||
import 'package:mockito/mockito.dart';
|
|
||||||
import 'package:mockito/annotations.dart';
|
|
||||||
import 'package:platform_core/core.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider_manager.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider.dart';
|
|
||||||
import 'package:platform_core/src/provider/provider_discovery.dart';
|
|
||||||
import 'package:platform_container/container.dart' as container;
|
|
||||||
import 'service_provider_manager_test.mocks.dart';
|
|
||||||
|
|
||||||
//class MockAngel extends Mock implements Application {}
|
|
||||||
|
|
||||||
//class MockServiceProvider extends Mock implements ServiceProvider {}
|
|
||||||
|
|
||||||
//class MockProviderDiscovery extends Mock implements ProviderDiscovery {}
|
|
||||||
|
|
||||||
// Generate mocks
|
|
||||||
@GenerateMocks(
|
|
||||||
[Application, ServiceProvider, ProviderDiscovery, container.Container])
|
|
||||||
void main() {
|
|
||||||
group('ServiceProviderManager', () {
|
|
||||||
late ServiceProviderManager manager;
|
|
||||||
late Application mockAngel;
|
|
||||||
late MockProviderDiscovery mockDiscovery;
|
|
||||||
late MockContainer mockContainer;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
mockAngel = Application();
|
|
||||||
mockDiscovery = MockProviderDiscovery();
|
|
||||||
mockContainer = MockContainer();
|
|
||||||
when(mockAngel.container).thenReturn(mockContainer);
|
|
||||||
manager = ServiceProviderManager(mockAngel, mockDiscovery);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('register adds provider and calls register method', () async {
|
|
||||||
final provider = MockServiceProvider();
|
|
||||||
await manager.register(provider);
|
|
||||||
|
|
||||||
verify(provider.registerWithContainer(any)).called(1);
|
|
||||||
verify(provider.register()).called(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('bootAll calls boot for all providers', () async {
|
|
||||||
final provider1 = MockServiceProvider();
|
|
||||||
final provider2 = MockServiceProvider();
|
|
||||||
|
|
||||||
when(provider1.isEnabled).thenReturn(true);
|
|
||||||
when(provider2.isEnabled).thenReturn(true);
|
|
||||||
when(provider1.isDeferred).thenReturn(false);
|
|
||||||
when(provider2.isDeferred).thenReturn(false);
|
|
||||||
|
|
||||||
await manager.register(provider1);
|
|
||||||
await manager.register(provider2);
|
|
||||||
|
|
||||||
await manager.bootAll();
|
|
||||||
|
|
||||||
verify(provider1.beforeBoot()).called(1);
|
|
||||||
verify(provider1.boot()).called(1);
|
|
||||||
verify(provider1.afterBoot()).called(1);
|
|
||||||
verify(provider2.beforeBoot()).called(1);
|
|
||||||
verify(provider2.boot()).called(1);
|
|
||||||
verify(provider2.afterBoot()).called(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('bootDeferredProvider boots only the specified provider', () async {
|
|
||||||
final provider1 = MockServiceProvider();
|
|
||||||
final provider2 = MockServiceProvider();
|
|
||||||
|
|
||||||
when(provider1.isDeferred).thenReturn(true);
|
|
||||||
when(provider2.isDeferred).thenReturn(true);
|
|
||||||
|
|
||||||
await manager.register(provider1);
|
|
||||||
await manager.register(provider2);
|
|
||||||
|
|
||||||
await manager.bootDeferredProvider(provider1.runtimeType);
|
|
||||||
|
|
||||||
verify(provider1.beforeBoot()).called(1);
|
|
||||||
verify(provider1.boot()).called(1);
|
|
||||||
verify(provider1.afterBoot()).called(1);
|
|
||||||
verifyNever(provider2.beforeBoot());
|
|
||||||
verifyNever(provider2.boot());
|
|
||||||
verifyNever(provider2.afterBoot());
|
|
||||||
});
|
|
||||||
|
|
||||||
test('unregister removes the provider', () async {
|
|
||||||
final provider = MockServiceProvider();
|
|
||||||
await manager.register(provider);
|
|
||||||
manager.unregister(provider.runtimeType);
|
|
||||||
|
|
||||||
expect(manager.getProvider<MockServiceProvider>(), isNull);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,32 +0,0 @@
|
||||||
import 'package:test/test.dart';
|
|
||||||
import 'package:mockito/mockito.dart';
|
|
||||||
import 'package:platform_core/src/provider/service_provider.dart';
|
|
||||||
|
|
||||||
class MockServiceProvider extends Mock implements ServiceProvider {}
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
group('ServiceProvider', () {
|
|
||||||
late ServiceProvider provider;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
provider = MockServiceProvider();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('setEnabled changes isEnabled', () {
|
|
||||||
when(provider.isEnabled).thenAnswer((_) => true);
|
|
||||||
provider.setEnabled(false);
|
|
||||||
verify(provider.setEnabled(false)).called(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('setDeferred changes isDeferred', () {
|
|
||||||
when(provider.isDeferred).thenAnswer((_) => false);
|
|
||||||
provider.setDeferred(true);
|
|
||||||
verify(provider.setDeferred(true)).called(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('configure adds to config', () {
|
|
||||||
provider.configure({'key': 'value'});
|
|
||||||
verify(provider.configure({'key': 'value'})).called(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1,35 +0,0 @@
|
||||||
import 'package:platform_core/core.dart';
|
|
||||||
import 'package:platform_container/container.dart';
|
|
||||||
import 'package:platform_container/mirrors.dart';
|
|
||||||
import 'package:platform_core/src/provider/platform_with_providers.dart';
|
|
||||||
|
|
||||||
class TestSetup {
|
|
||||||
late Application app;
|
|
||||||
late PlatformWithProviders platformWithProviders;
|
|
||||||
late Container _container;
|
|
||||||
|
|
||||||
Container get container => _container;
|
|
||||||
|
|
||||||
Future<void> initialize() async {
|
|
||||||
app = Application();
|
|
||||||
|
|
||||||
// Create a container with MirrorsReflector
|
|
||||||
_container = Container(MirrorsReflector());
|
|
||||||
|
|
||||||
// Initialize PlatformWithProviders
|
|
||||||
platformWithProviders = PlatformWithProviders(app);
|
|
||||||
|
|
||||||
// Configure the app to use our container
|
|
||||||
app.configure((angel) {
|
|
||||||
// Instead of registering the container, we'll replace Angel's container
|
|
||||||
angel.container = _container;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Allow some time for initialization
|
|
||||||
await Future.delayed(Duration(milliseconds: 100));
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> tearDown() async {
|
|
||||||
await app.close();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue