2021-09-27 08:28:40 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AutoFixture;
|
|
|
|
|
using AutoFixture.NUnit3;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Composing;
|
|
|
|
|
using Umbraco.Cms.Core.DependencyInjection;
|
|
|
|
|
using Umbraco.Cms.Core.Events;
|
|
|
|
|
using Umbraco.Cms.Core.Extensions;
|
|
|
|
|
using Umbraco.Cms.Core.Hosting;
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
|
|
|
|
using Umbraco.Cms.Core.Notifications;
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Extensions;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class UmbracoBuilderExtensionsTests
|
2021-09-27 08:28:40 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
[Customization]
|
|
|
|
|
public void AddNotificationsFromAssembly_Should_AddNotificationHandler_To_ServicesCollection(IUmbracoBuilder sut)
|
2021-09-27 08:28:40 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
sut.AddNotificationsFromAssembly<CustomizationAttribute>();
|
2021-09-27 08:28:40 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var expectedHandlerType = typeof(INotificationHandler<ContentPublishedNotification>);
|
|
|
|
|
var handler = sut.Services.SingleOrDefault(x => x.ServiceType == expectedHandlerType);
|
|
|
|
|
Assert.NotNull(handler);
|
|
|
|
|
Assert.That(handler.ImplementationType, Is.EqualTo(typeof(StubNotificationHandler)));
|
|
|
|
|
}
|
2021-09-27 08:28:40 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
[Customization]
|
|
|
|
|
public void AddNotificationsFromAssembly_Should_AddAsyncNotificationHandler_To_ServicesCollection(
|
|
|
|
|
IUmbracoBuilder sut)
|
|
|
|
|
{
|
|
|
|
|
sut.AddNotificationsFromAssembly<CustomizationAttribute>();
|
2021-09-27 08:28:40 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var expectedHandlerType = typeof(INotificationAsyncHandler<ContentPublishedNotification>);
|
|
|
|
|
var handler = sut.Services.SingleOrDefault(x => x.ServiceType == expectedHandlerType);
|
|
|
|
|
Assert.NotNull(handler);
|
|
|
|
|
Assert.That(handler.ImplementationType, Is.EqualTo(typeof(StubNotificationHandler)));
|
|
|
|
|
}
|
2021-09-27 08:28:40 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private class CustomizationAttribute : AutoDataAttribute
|
|
|
|
|
{
|
|
|
|
|
public CustomizationAttribute()
|
|
|
|
|
: base(() =>
|
2021-09-27 08:28:40 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var fixture = new Fixture();
|
2021-09-27 08:28:40 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var stub = new UmbracoBuildStub();
|
|
|
|
|
fixture.Inject((IUmbracoBuilder)stub);
|
2021-09-27 08:28:40 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return fixture;
|
|
|
|
|
})
|
|
|
|
|
{
|
2021-09-27 08:28:40 +02:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class UmbracoBuildStub : IUmbracoBuilder
|
|
|
|
|
{
|
|
|
|
|
public UmbracoBuildStub() => Services = new ServiceCollection();
|
2021-09-27 08:28:40 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IServiceCollection Services { get; }
|
|
|
|
|
|
|
|
|
|
public IConfiguration Config { get; }
|
|
|
|
|
|
|
|
|
|
public TypeLoader TypeLoader { get; }
|
|
|
|
|
|
|
|
|
|
public ILoggerFactory BuilderLoggerFactory { get; }
|
|
|
|
|
|
|
|
|
|
public IHostingEnvironment BuilderHostingEnvironment { get; }
|
|
|
|
|
|
|
|
|
|
public IProfiler Profiler { get; }
|
|
|
|
|
|
|
|
|
|
public AppCaches AppCaches { get; }
|
|
|
|
|
|
|
|
|
|
public TBuilder WithCollectionBuilder<TBuilder>()
|
|
|
|
|
where TBuilder : ICollectionBuilder => default;
|
|
|
|
|
|
|
|
|
|
public void Build()
|
2021-09-27 08:28:40 +02:00
|
|
|
{
|
|
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2021-09-27 08:28:40 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private class StubNotificationHandler
|
|
|
|
|
: INotificationHandler<ContentPublishedNotification>,
|
|
|
|
|
INotificationAsyncHandler<ContentPublishedNotification>
|
|
|
|
|
{
|
|
|
|
|
public Task HandleAsync(ContentPublishedNotification notification, CancellationToken cancellationToken) => Task.CompletedTask;
|
2021-09-27 08:28:40 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public void Handle(ContentPublishedNotification notification)
|
|
|
|
|
{
|
2021-09-27 08:28:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|