Skip to content

Commit 8b0a9ee

Browse files
authored
Merge pull request #340 - WithoutData, WithoutSession, WithoutMemoryCache implementation added
WithoutData, WithoutSession, WithoutMemoryCache implementation added
2 parents bed3521 + 92ee16b commit 8b0a9ee

File tree

40 files changed

+1868
-245
lines changed

40 files changed

+1868
-245
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data.MemoryCache
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data
22
{
33
/// <summary>
44
/// Used for adding AndAlso() method to the <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> builder.
55
/// </summary>
6-
public interface IAndMemoryCacheBuilder : IMemoryCacheBuilder
6+
public interface IAndWithMemoryCacheBuilder : IWithMemoryCacheBuilder
77
{
88
/// <summary>
99
/// AndAlso method for better readability when building <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/>.
1010
/// </summary>
11-
/// <returns>The same <see cref="IMemoryCacheBuilder"/>.</returns>
12-
IMemoryCacheBuilder AndAlso();
11+
/// <returns>The same <see cref="IWithMemoryCacheBuilder"/>.</returns>
12+
IWithMemoryCacheBuilder AndAlso();
1313
}
1414
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data
2+
{
3+
/// <summary>
4+
/// Used for adding AndAlso() method to the <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> builder.
5+
/// </summary>
6+
public interface IAndWithoutMemoryCacheBuilder : IWithoutMemoryCacheBuilder
7+
{
8+
/// <summary>
9+
/// AndAlso method for better readability when building <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/>.
10+
/// </summary>
11+
/// <returns>The same <see cref="IWithoutMemoryCacheBuilder"/>.</returns>
12+
IWithoutMemoryCacheBuilder AndAlso();
13+
}
14+
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data.MemoryCache
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data
22
{
33
using System;
44
using System.Collections.Generic;
55
using Microsoft.Extensions.Caching.Memory;
6+
using Contracts.Data.MemoryCache;
67

78
/// <summary>
89
/// Used for building mocked <see cref="IMemoryCache"/>.
910
/// </summary>
10-
public interface IMemoryCacheBuilder
11+
public interface IWithMemoryCacheBuilder
1112
{
1213
/// <summary>
1314
/// Adds cache entry to the mocked <see cref="IMemoryCache"/>.
1415
/// </summary>
1516
/// <param name="key">Key of the cache entry.</param>
1617
/// <param name="value">Value of the cache entry.</param>
17-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
18-
IAndMemoryCacheBuilder WithEntry(object key, object value);
18+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
19+
IAndWithMemoryCacheBuilder WithEntry(object key, object value);
1920

2021
/// <summary>
2122
/// Adds cache entry to the mocked <see cref="IMemoryCache"/>.
2223
/// </summary>
2324
/// <param name="key">Key of the cache entry.</param>
2425
/// <param name="value">Value of the cache entry.</param>
2526
/// <param name="options"><see cref="MemoryCacheEntryOptions"/> of the cache entry.</param>
26-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
27-
IAndMemoryCacheBuilder WithEntry(object key, object value, MemoryCacheEntryOptions options);
27+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
28+
IAndWithMemoryCacheBuilder WithEntry(object key, object value, MemoryCacheEntryOptions options);
2829

2930
/// <summary>
3031
/// Adds cache entry to the mocked <see cref="IMemoryCache"/>.
3132
/// </summary>
3233
/// <param name="memoryCacheEntryBuilder">Builder for creating cache entry.</param>
33-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
34-
IAndMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memoryCacheEntryBuilder);
34+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
35+
IAndWithMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memoryCacheEntryBuilder);
3536

3637
/// <summary>
3738
/// Adds cache entries to the mocked <see cref="IMemoryCache"/>.
3839
/// </summary>
3940
/// <param name="entries">Dictionary of cache entries.</param>
40-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
41-
IAndMemoryCacheBuilder WithEntries(IDictionary<object, object> entries);
41+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
42+
IAndWithMemoryCacheBuilder WithEntries(IDictionary<object, object> entries);
4243
}
4344
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data
2+
{
3+
using System.Collections.Generic;
4+
using Microsoft.Extensions.Caching.Memory;
5+
6+
/// <summary>
7+
/// Used for building mocked <see cref="IMemoryCache"/>.
8+
/// </summary>
9+
public interface IWithoutMemoryCacheBuilder
10+
{
11+
/// <summary>
12+
/// Remove cache entry to the mocked <see cref="IMemoryCache"/>.
13+
/// </summary>
14+
/// <param name="key">Key of the cache entry.</param>
15+
/// <returns>The same <see cref="IAndWithoutMemoryCacheBuilder"/>.</returns>
16+
IAndWithoutMemoryCacheBuilder WithoutEntry(object key);
17+
18+
/// <summary>
19+
/// Remove cache entries to the mocked <see cref="IMemoryCache"/>.
20+
/// </summary>
21+
/// <param name="keys">Keys of the cache entries.</param>
22+
/// <returns>The same <see cref="IAndWithoutMemoryCacheBuilder"/>.</returns>
23+
IAndWithoutMemoryCacheBuilder WithoutEntries(IEnumerable<object> keys);
24+
25+
/// <summary>
26+
/// Remove cache params to the mocked <see cref="IMemoryCache"/>.
27+
/// </summary>
28+
/// <param name="keys">Keys of the cache entries.</param>
29+
/// <returns>The same <see cref="IAndWithoutMemoryCacheBuilder"/>.</returns>
30+
IAndWithoutMemoryCacheBuilder WithoutEntries(params object[] keys);
31+
32+
/// <summary>
33+
/// Clear all entries persisted into the <see cref="IMemoryCache"/>.
34+
/// </summary>
35+
/// <returns>The same <see cref="IAndWithoutMemoryCacheBuilder"/>.</returns>
36+
IAndWithoutMemoryCacheBuilder WithoutAllEntries();
37+
}
38+
}

src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/IMemoryCacheTestBuilder.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,38 @@ public interface IMemoryCacheTestBuilder
1313
/// Tests whether the <see cref="IMemoryCache"/> contains entry with the provided key.
1414
/// </summary>
1515
/// <param name="key">Key of the cache entry.</param>
16-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
16+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
1717
IAndMemoryCacheTestBuilder ContainingEntryWithKey(object key);
1818

1919
/// <summary>
2020
/// Tests whether the <see cref="IMemoryCache"/> contains entry with the provided value.
2121
/// </summary>
2222
/// <typeparam name="TValue">Type of the cache entry value.</typeparam>
2323
/// <param name="value">Value of the cache entry.</param>
24-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
24+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
2525
IAndMemoryCacheTestBuilder ContainingEntryWithValue<TValue>(TValue value);
2626

2727
/// <summary>
2828
/// Tests whether the <see cref="IMemoryCache"/> contains entry with value of the provided type.
2929
/// </summary>
3030
/// <typeparam name="TValue">Type of the cache entry value.</typeparam>
31-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
31+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
3232
IAndMemoryCacheTestBuilder ContainingEntryOfType<TValue>();
3333

3434
/// <summary>
3535
/// Tests whether the <see cref="IMemoryCache"/> contains entry with the provided key and corresponding value.
3636
/// </summary>
3737
/// <param name="key">Key of the cache entry.</param>
3838
/// <param name="value">Value of the cache entry.</param>
39-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
39+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
4040
IAndMemoryCacheTestBuilder ContainingEntry(object key, object value);
4141

4242
/// <summary>
4343
/// Tests whether the <see cref="IMemoryCache"/> contains entry with value of the provided type and the given key.
4444
/// </summary>
4545
/// <typeparam name="TValue">Type of the cache entry value.</typeparam>
4646
/// <param name="key">Key of the cache entry.</param>
47-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
47+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
4848
IAndMemoryCacheTestBuilder ContainingEntryOfType<TValue>(object key);
4949

5050
/// <summary>
@@ -53,21 +53,21 @@ public interface IMemoryCacheTestBuilder
5353
/// <param name="key">Key of the cache entry.</param>
5454
/// <param name="value">Value of the cache entry.</param>
5555
/// <param name="options"><see cref="MemoryCacheEntryOptions"/> of the cache entry.</param>
56-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
56+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
5757
IAndMemoryCacheTestBuilder ContainingEntry(object key, object value, MemoryCacheEntryOptions options);
5858

5959
/// <summary>
6060
/// Tests whether the <see cref="IMemoryCache"/> contains specific entry by using a builder.
6161
/// </summary>
6262
/// <param name="memoryCacheEntryTestBuilder">Builder for setting specific cache entry tests.</param>
63-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
63+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
6464
IAndMemoryCacheTestBuilder ContainingEntry(Action<IMemoryCacheEntryKeyTestBuilder> memoryCacheEntryTestBuilder);
6565

6666
/// <summary>
6767
/// Tests whether the <see cref="IMemoryCache"/> contains the provided entries.
6868
/// </summary>
6969
/// <param name="entries">Dictionary of cache entries.</param>
70-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
70+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
7171
IAndMemoryCacheTestBuilder ContainingEntries(IDictionary<object, object> entries);
7272
}
7373
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Data
2+
{
3+
using System;
4+
using Microsoft.Extensions.Caching.Memory;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
/// <summary>
8+
/// Used for building mocked <see cref="IMemoryCache"/>.
9+
/// </summary>
10+
public abstract class BaseMemoryCacheBuilder
11+
{
12+
/// <summary>
13+
/// Abstract <see cref="BaseMemoryCacheBuilder"/> class.
14+
/// </summary>
15+
/// <param name="services"><see cref="IServiceProvider"/> providing the current <see cref="IMemoryCache"/>.</param>
16+
public BaseMemoryCacheBuilder(IServiceProvider services)
17+
=> this.MemoryCache = services.GetRequiredService<IMemoryCache>();
18+
19+
protected IMemoryCache MemoryCache { get; private set; }
20+
}
21+
}
Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,41 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.Data.MemoryCache
1+
namespace MyTested.AspNetCore.Mvc.Builders.Data
22
{
33
using System;
44
using System.Collections.Generic;
5-
using Contracts.Data.MemoryCache;
5+
using Contracts.Data;
66
using Microsoft.Extensions.Caching.Memory;
7-
using Microsoft.Extensions.DependencyInjection;
7+
using Contracts.Data.MemoryCache;
8+
using Data.MemoryCache;
89
using Utilities.Extensions;
910

10-
/// <summary>
11-
/// Used for building mocked <see cref="IMemoryCache"/>.
12-
/// </summary>
13-
public class MemoryCacheBuilder : IAndMemoryCacheBuilder
11+
/// <inheritdoc />4
12+
public class WithMemoryCacheBuilder : BaseMemoryCacheBuilder, IAndWithMemoryCacheBuilder
1413
{
1514
/// <summary>
16-
/// Initializes a new instance of the <see cref="MemoryCacheBuilder"/> class.
15+
/// Initializes a new instance of the <see cref="WithMemoryCacheBuilder"/> class.
1716
/// </summary>
1817
/// <param name="services"><see cref="IServiceProvider"/> providing the current <see cref="IMemoryCache"/>.</param>
19-
public MemoryCacheBuilder(IServiceProvider services)
18+
public WithMemoryCacheBuilder(IServiceProvider services)
19+
: base(services)
2020
{
21-
this.MemoryCache = services.GetRequiredService<IMemoryCache>();
2221
}
2322

24-
/// <summary>
25-
/// Gets the mocked <see cref="IMemoryCache"/>.
26-
/// </summary>
27-
/// <value>Built <see cref="IMemoryCache"/>.</value>
28-
protected IMemoryCache MemoryCache { get; private set; }
29-
3023
/// <inheritdoc />
31-
public IAndMemoryCacheBuilder WithEntry(object key, object value)
24+
public IAndWithMemoryCacheBuilder WithEntry(object key, object value)
3225
{
3326
this.MemoryCache.Set(key, value);
3427
return this;
3528
}
3629

3730
/// <inheritdoc />
38-
public IAndMemoryCacheBuilder WithEntry(object key, object value, MemoryCacheEntryOptions options)
31+
public IAndWithMemoryCacheBuilder WithEntry(object key, object value, MemoryCacheEntryOptions options)
3932
{
4033
this.MemoryCache.Set(key, value, options);
4134
return this;
4235
}
4336

4437
/// <inheritdoc />
45-
public IAndMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memoryCacheEntryBuilder)
38+
public IAndWithMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memoryCacheEntryBuilder)
4639
{
4740
var newMemoryCacheEntryBuilder = new MemoryCacheEntryBuilder();
4841
memoryCacheEntryBuilder(newMemoryCacheEntryBuilder);
@@ -58,13 +51,13 @@ public IAndMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memo
5851
}
5952

6053
/// <inheritdoc />
61-
public IAndMemoryCacheBuilder WithEntries(IDictionary<object, object> entries)
54+
public IAndWithMemoryCacheBuilder WithEntries(IDictionary<object, object> entries)
6255
{
6356
entries.ForEach(e => this.WithEntry(e.Key, e.Value));
6457
return this;
6558
}
6659

6760
/// <inheritdoc />
68-
public IMemoryCacheBuilder AndAlso() => this;
61+
public IWithMemoryCacheBuilder AndAlso() => this;
6962
}
7063
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Data
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using Contracts.Data;
6+
using Microsoft.Extensions.Caching.Memory;
7+
using Utilities.Extensions;
8+
9+
/// <inheritdoc />
10+
public class WithoutMemoryCacheBuilder : BaseMemoryCacheBuilder, IAndWithoutMemoryCacheBuilder
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="WithoutMemoryCacheBuilder"/> class.
14+
/// </summary>
15+
/// <param name="services"><see cref="IServiceProvider"/> providing the current <see cref="IMemoryCache"/>.</param>
16+
public WithoutMemoryCacheBuilder(IServiceProvider services)
17+
: base(services)
18+
{
19+
}
20+
21+
/// <inheritdoc />
22+
public IAndWithoutMemoryCacheBuilder WithoutEntry(object key)
23+
{
24+
this.MemoryCache.Remove(key);
25+
return this;
26+
}
27+
28+
/// <inheritdoc />
29+
public IAndWithoutMemoryCacheBuilder WithoutEntries(IEnumerable<object> keys)
30+
{
31+
this.MemoryCache.AsMemoryCacheMock().RemoveKeys(keys);
32+
return this;
33+
}
34+
35+
/// <inheritdoc />
36+
public IAndWithoutMemoryCacheBuilder WithoutAllEntries()
37+
{
38+
this.MemoryCache.AsMemoryCacheMock().ClearCache();
39+
return this;
40+
}
41+
42+
/// <inheritdoc />
43+
public IAndWithoutMemoryCacheBuilder WithoutEntries(params object[] keys)
44+
{
45+
this.MemoryCache.AsMemoryCacheMock().RemoveKeys(keys);
46+
return this;
47+
}
48+
49+
/// <inheritdoc />
50+
public IWithoutMemoryCacheBuilder AndAlso() => this;
51+
}
52+
}

src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderMemoryCacheExtensions.cs renamed to src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderCachingWithExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22
{
33
using System;
44
using Builders.Contracts.Base;
5+
using Builders.Contracts.Data;
6+
using Builders.Data;
57
using Builders.Base;
6-
using Builders.Contracts.Data.MemoryCache;
7-
using Builders.Data.MemoryCache;
88

99
/// <summary>
1010
/// Contains <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> extension methods for <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/>.
1111
/// </summary>
12-
public static class ComponentBuilderMemoryCacheExtensions
12+
public static class ComponentBuilderCachingWithExtensions
1313
{
1414
/// <summary>
1515
/// Sets initial values to the <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> service.
1616
/// </summary>
1717
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
1818
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.</param>
19-
/// <param name="memoryCacheBuilder">Action setting the <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> values by using <see cref="IMemoryCacheBuilder"/>.</param>
19+
/// <param name="memoryCacheBuilder">Action setting the <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> values by using <see cref="IWithMemoryCacheBuilder"/>.</param>
2020
/// <returns>The same component builder.</returns>
2121
public static TBuilder WithMemoryCache<TBuilder>(
2222
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
23-
Action<IMemoryCacheBuilder> memoryCacheBuilder)
23+
Action<IWithMemoryCacheBuilder> memoryCacheBuilder)
2424
where TBuilder : IBaseTestBuilder
2525
{
2626
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
2727

28-
memoryCacheBuilder(new MemoryCacheBuilder(actualBuilder.TestContext.HttpContext.RequestServices));
28+
memoryCacheBuilder(new WithMemoryCacheBuilder(actualBuilder.TestContext.HttpContext.RequestServices));
2929

3030
return actualBuilder.Builder;
3131
}

0 commit comments

Comments
 (0)