Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/ComponentRuntimeTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

71 lines
2.4 KiB
C#

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Runtime;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.Integration
{
[TestFixture]
[UmbracoTest(Boot = true)]
public class ComponentRuntimeTests : UmbracoIntegrationTest
{
// ensure composers are added
protected override void CustomTestSetup(IUmbracoBuilder builder)
{
builder.AddComposers();
}
/// <summary>
/// This will boot up umbraco with components enabled to show they initialize and shutdown
/// </summary>
[Test]
public async Task Start_And_Stop_Umbraco_With_Components_Enabled()
{
IRuntime runtime = Services.GetRequiredService<IRuntime>();
IRuntimeState runtimeState = Services.GetRequiredService<IRuntimeState>();
IMainDom mainDom = Services.GetRequiredService<IMainDom>();
ComponentCollection components = Services.GetRequiredService<ComponentCollection>();
MyComponent myComponent = components.OfType<MyComponent>().First();
Assert.IsTrue(mainDom.IsMainDom);
Assert.IsNull(runtimeState.BootFailedException);
Assert.IsTrue(myComponent.IsInit, "The component was not initialized");
// force stop now
await runtime.StopAsync(CancellationToken.None);
Assert.IsTrue(myComponent.IsTerminated, "The component was not terminated");
}
public class MyComposer : IComposer
{
public void Compose(IUmbracoBuilder builder) => builder.Components().Append<MyComponent>();
}
public class MyComponent : IComponent
{
public bool IsInit { get; private set; }
public bool IsTerminated { get; private set; }
private readonly ILogger<MyComponent> _logger;
public MyComponent(ILogger<MyComponent> logger) => _logger = logger;
public void Initialize() => IsInit = true;
public void Terminate() => IsTerminated = true;
}
}
}