From b51e571803d652bbc06eb1e618bca482f809bd04 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 19 Jan 2017 18:25:45 +1100 Subject: [PATCH] Adds unit tests and fixes others --- .../Services/RepositoryService.cs | 13 --- .../Services/ScopeRepositoryService.cs | 21 +++++ src/Umbraco.Core/Umbraco.Core.csproj | 1 + .../Scoping/NoScopedEventManagerTests.cs | 67 ++++++++++++++ src/Umbraco.Tests/Scoping/ScopeTests.cs | 1 + .../Scoping/ScopedEventManagerTests.cs | 90 +++++++++++++++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 2 + .../Web/Mvc/UmbracoViewPageTests.cs | 2 +- 8 files changed, 183 insertions(+), 14 deletions(-) create mode 100644 src/Umbraco.Core/Services/ScopeRepositoryService.cs create mode 100644 src/Umbraco.Tests/Scoping/NoScopedEventManagerTests.cs create mode 100644 src/Umbraco.Tests/Scoping/ScopedEventManagerTests.cs diff --git a/src/Umbraco.Core/Services/RepositoryService.cs b/src/Umbraco.Core/Services/RepositoryService.cs index b1e0cf7d21..88807f0291 100644 --- a/src/Umbraco.Core/Services/RepositoryService.cs +++ b/src/Umbraco.Core/Services/RepositoryService.cs @@ -6,19 +6,6 @@ using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Services { - public abstract class ScopeRepositoryService : RepositoryService - { - protected ScopeRepositoryService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory, ILogger logger, IEventMessagesFactory eventMessagesFactory) - : base(provider, repositoryFactory, logger, eventMessagesFactory) - { - var scopeUow = provider as IScopeUnitOfWorkProvider; - if (scopeUow == null) throw new NotSupportedException("The provider type passed in: " + provider.GetType() + " is not of type " + typeof(IScopeUnitOfWorkProvider)); - UowProvider = scopeUow; - } - - internal new IScopeUnitOfWorkProvider UowProvider { get; private set; } - } - /// /// Base service class /// diff --git a/src/Umbraco.Core/Services/ScopeRepositoryService.cs b/src/Umbraco.Core/Services/ScopeRepositoryService.cs new file mode 100644 index 0000000000..0942aefd6f --- /dev/null +++ b/src/Umbraco.Core/Services/ScopeRepositoryService.cs @@ -0,0 +1,21 @@ +using System; +using Umbraco.Core.Events; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.UnitOfWork; + +namespace Umbraco.Core.Services +{ + public abstract class ScopeRepositoryService : RepositoryService + { + protected ScopeRepositoryService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory, ILogger logger, IEventMessagesFactory eventMessagesFactory) + : base(provider, repositoryFactory, logger, eventMessagesFactory) + { + var scopeUow = provider as IScopeUnitOfWorkProvider; + if (scopeUow == null) throw new NotSupportedException("The provider type passed in: " + provider.GetType() + " is not of type " + typeof(IScopeUnitOfWorkProvider)); + UowProvider = scopeUow; + } + + internal new IScopeUnitOfWorkProvider UowProvider { get; private set; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index cb4a157d18..88555db603 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -574,6 +574,7 @@ + diff --git a/src/Umbraco.Tests/Scoping/NoScopedEventManagerTests.cs b/src/Umbraco.Tests/Scoping/NoScopedEventManagerTests.cs new file mode 100644 index 0000000000..6975b578ab --- /dev/null +++ b/src/Umbraco.Tests/Scoping/NoScopedEventManagerTests.cs @@ -0,0 +1,67 @@ +using System; +using System.Linq; +using Moq; +using NUnit.Framework; +using Umbraco.Core.Events; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Core.Scoping; + +namespace Umbraco.Tests.Scoping +{ + [TestFixture] + public class NoScopedEventManagerTests + { + + [Test] + public void Does_Support_Event_Cancellation() + { + var scopeProvider = new ScopeProvider(Mock.Of()); + Assert.IsTrue(scopeProvider.AmbientOrNoScope.EventManager.SupportsEventCancellation); + } + + [Test] + public void Does_Immediately_Raise_Events() + { + var counter = 0; + + this.DoThing1 += (sender, args) => + { + counter++; + }; + + this.DoThing2 += (sender, args) => + { + counter++; + }; + + this.DoThing3 += (sender, args) => + { + counter++; + }; + + var scopeProvider = new ScopeProvider(Mock.Of()); + scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing1, this, new EventArgs()); + scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing2, this, new EventArgs()); + scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing3, this, new EventArgs()); + + Assert.AreEqual(3, counter); + + } + + [Test] + public void Can_Not_Raise_Events_Later() + { + var scopeProvider = new ScopeProvider(Mock.Of()); + scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing1, this, new EventArgs()); + scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing2, this, new EventArgs()); + scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing3, this, new EventArgs()); + + Assert.AreEqual(0, scopeProvider.AmbientOrNoScope.EventManager.GetEvents().Count()); + } + + public event EventHandler DoThing1; + public event EventHandler DoThing2; + public event TypedEventHandler DoThing3; + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Scoping/ScopeTests.cs b/src/Umbraco.Tests/Scoping/ScopeTests.cs index f6a9ab71df..c8ec425bca 100644 --- a/src/Umbraco.Tests/Scoping/ScopeTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopeTests.cs @@ -4,6 +4,7 @@ using Umbraco.Core; using Umbraco.Core.Persistence; using Umbraco.Core.Scoping; using Umbraco.Tests.TestHelpers; +using Umbraco.Core.Events; namespace Umbraco.Tests.Scoping { diff --git a/src/Umbraco.Tests/Scoping/ScopedEventManagerTests.cs b/src/Umbraco.Tests/Scoping/ScopedEventManagerTests.cs new file mode 100644 index 0000000000..3ddd0f7e08 --- /dev/null +++ b/src/Umbraco.Tests/Scoping/ScopedEventManagerTests.cs @@ -0,0 +1,90 @@ +using System; +using Moq; +using NUnit.Framework; +using Umbraco.Core.Events; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Core.Scoping; + +namespace Umbraco.Tests.Scoping +{ + [TestFixture] + public class ScopedEventManagerTests + { + + [Test] + public void Does_Not_Support_Event_Cancellation() + { + var provider = new PetaPocoUnitOfWorkProvider(new ScopeProvider(Mock.Of())); + using (var uow = provider.GetUnitOfWork()) + { + Assert.IsFalse(uow.EventManager.SupportsEventCancellation); + } + } + + [Test] + public void Does_Not_Immediately_Raise_Events() + { + this.DoThing1 += OnDoThingFail; + this.DoThing2 += OnDoThingFail; + this.DoThing3 += OnDoThingFail; + + var provider = new PetaPocoUnitOfWorkProvider(new ScopeProvider(Mock.Of())); + using (var uow = provider.GetUnitOfWork()) + { + uow.EventManager.TrackEvent(DoThing1, this, new EventArgs()); + uow.EventManager.TrackEvent(DoThing2, this, new EventArgs()); + uow.EventManager.TrackEvent(DoThing3, this, new EventArgs()); + + Assert.Pass(); + } + } + + [Test] + public void Can_Raise_Events_Later() + { + var counter = 0; + + this.DoThing1 += (sender, args) => + { + counter++; + }; + + this.DoThing2 += (sender, args) => + { + counter++; + }; + + this.DoThing3 += (sender, args) => + { + counter++; + }; + + var provider = new PetaPocoUnitOfWorkProvider(new ScopeProvider(Mock.Of())); + using (var uow = provider.GetUnitOfWork()) + { + uow.EventManager.TrackEvent(DoThing1, this, new EventArgs()); + uow.EventManager.TrackEvent(DoThing2, this, new EventArgs()); + uow.EventManager.TrackEvent(DoThing3, this, new EventArgs()); + + Assert.AreEqual(0, counter); + + foreach (var e in uow.EventManager.GetEvents()) + { + e.RaiseEvent(); + } + + Assert.AreEqual(3, counter); + } + } + + private void OnDoThingFail(object sender, EventArgs eventArgs) + { + Assert.Fail(); + } + + public event EventHandler DoThing1; + public event EventHandler DoThing2; + public event TypedEventHandler DoThing3; + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index bdfcac3420..45cfc6cfd7 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -170,6 +170,8 @@ + + diff --git a/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs b/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs index 3ba6e06c7c..f83b8a6369 100644 --- a/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs @@ -396,7 +396,7 @@ namespace Umbraco.Tests.Web.Mvc new Mock().Object), new Mock().Object, new RelationService( - new Mock().Object, + new Mock().Object, new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), logger, Mock.Of(), umbracoSettings), logger, new TransientMessagesFactory(),