Skip to content

Commit 989010b

Browse files
committed
#283 Adding initial implementation of removing entry from session or clear the session itself.
TODO: Add xml comments and tests
1 parent e855cda commit 989010b

File tree

8 files changed

+121
-41
lines changed

8 files changed

+121
-41
lines changed

src/MyTested.AspNetCore.Mvc.Session/Builders/Contracts/Data/IAndSessionBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.Session/Builders/Contracts/Data/IAndWithSessionBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
/// <summary>
44
/// Used for adding AndAlso() method to the <see cref="Microsoft.AspNetCore.Http.ISession"/> builder.
55
/// </summary>
6-
public interface IAndSessionBuilder : ISessionBuilder
6+
public interface IAndWithSessionBuilder : IWithSessionBuilder
77
{
88
/// <summary>
99
/// AndAlso method for better readability when building <see cref="Microsoft.AspNetCore.Http.ISession"/>.
1010
/// </summary>
11-
/// <returns>The same <see cref="ISessionBuilder"/>.</returns>
12-
ISessionBuilder AndAlso();
11+
/// <returns>The same <see cref="IWithSessionBuilder"/>.</returns>
12+
IWithSessionBuilder 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.AspNetCore.Http.ISession"/> builder.
5+
/// </summary>
6+
public interface IAndWithoutSessionBuilder : IWithoutSessionBuilder
7+
{
8+
/// <summary>
9+
/// AndAlso method for better readability when building <see cref="Microsoft.AspNetCore.Http.ISession"/>.
10+
/// </summary>
11+
/// <returns>The same <see cref="IWithoutSessionBuilder"/>.</returns>
12+
IWithoutSessionBuilder AndAlso();
13+
}
14+
}

src/MyTested.AspNetCore.Mvc.Session/Builders/Contracts/Data/ISessionBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.Session/Builders/Contracts/Data/IWithSessionBuilder.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,65 @@
55
/// <summary>
66
/// Used for building <see cref="Microsoft.AspNetCore.Http.ISession"/>.
77
/// </summary>
8-
public interface ISessionBuilder
8+
public interface IWithSessionBuilder
99
{
1010
/// <summary>
1111
/// Sets session ID to the build <see cref="Microsoft.AspNetCore.Http.ISession"/>.
1212
/// </summary>
1313
/// <param name="sessionId">Session ID to set as string.</param>
14-
/// <returns>The same <see cref="IAndSessionBuilder"/>.</returns>
15-
IAndSessionBuilder WithId(string sessionId);
14+
/// <returns>The same <see cref="IAndWithSessionBuilder"/>.</returns>
15+
IAndWithSessionBuilder WithId(string sessionId);
1616

1717
/// <summary>
1818
/// Adds byte array session entry to the built <see cref="Microsoft.AspNetCore.Http.ISession"/>.
1919
/// </summary>
2020
/// <param name="key">Key to set as string.</param>
2121
/// <param name="value">Value to set as byte array.</param>
22-
/// <returns>The same <see cref="IAndSessionBuilder"/>.</returns>
23-
IAndSessionBuilder WithEntry(string key, byte[] value);
22+
/// <returns>The same <see cref="IAndWithSessionBuilder"/>.</returns>
23+
IAndWithSessionBuilder WithEntry(string key, byte[] value);
2424

2525
/// <summary>
2626
/// Adds string session entry to the built <see cref="Microsoft.AspNetCore.Http.ISession"/>.
2727
/// </summary>
2828
/// <param name="key">Key to set as string.</param>
2929
/// <param name="value">Value to set as string.</param>
30-
/// <returns>The same <see cref="IAndSessionBuilder"/>.</returns>
31-
IAndSessionBuilder WithEntry(string key, string value);
30+
/// <returns>The same <see cref="IAndWithSessionBuilder"/>.</returns>
31+
IAndWithSessionBuilder WithEntry(string key, string value);
3232

3333
/// <summary>
3434
/// Adds integer session entry to the built <see cref="Microsoft.AspNetCore.Http.ISession"/>.
3535
/// </summary>
3636
/// <param name="key">Key to set as string.</param>
3737
/// <param name="value">Value to set as integer.</param>
38-
/// <returns>The same <see cref="IAndSessionBuilder"/>.</returns>
39-
IAndSessionBuilder WithEntry(string key, int value);
38+
/// <returns>The same <see cref="IAndWithSessionBuilder"/>.</returns>
39+
IAndWithSessionBuilder WithEntry(string key, int value);
4040

4141
/// <summary>
4242
/// Adds session entries to the built <see cref="Microsoft.AspNetCore.Http.ISession"/>.
4343
/// </summary>
4444
/// <param name="entries">Session entries as anonymous object.</param>
45-
/// <returns>The same <see cref="IAndSessionBuilder"/>.</returns>
46-
IAndSessionBuilder WithEntries(object entries);
45+
/// <returns>The same <see cref="IAndWithSessionBuilder"/>.</returns>
46+
IAndWithSessionBuilder WithEntries(object entries);
4747

4848
/// <summary>
4949
/// Adds byte array session entries to the built <see cref="Microsoft.AspNetCore.Http.ISession"/>.
5050
/// </summary>
5151
/// <param name="entries">Byte array session entries as dictionary.</param>
52-
/// <returns>The same <see cref="IAndSessionBuilder"/>.</returns>
53-
IAndSessionBuilder WithEntries(IDictionary<string, byte[]> entries);
52+
/// <returns>The same <see cref="IAndWithSessionBuilder"/>.</returns>
53+
IAndWithSessionBuilder WithEntries(IDictionary<string, byte[]> entries);
5454

5555
/// <summary>
5656
/// Adds string session entries to the built <see cref="Microsoft.AspNetCore.Http.ISession"/>.
5757
/// </summary>
5858
/// <param name="entries">String session entries as dictionary.</param>
59-
/// <returns>The same <see cref="IAndSessionBuilder"/>.</returns>
60-
IAndSessionBuilder WithEntries(IDictionary<string, string> entries);
59+
/// <returns>The same <see cref="IAndWithSessionBuilder"/>.</returns>
60+
IAndWithSessionBuilder WithEntries(IDictionary<string, string> entries);
6161

6262
/// <summary>
6363
/// Adds integer session entries to the built <see cref="Microsoft.AspNetCore.Http.ISession"/>.
6464
/// </summary>
6565
/// <param name="entries">Integer session entries as dictionary.</param>
66-
/// <returns>The same <see cref="IAndSessionBuilder"/>.</returns>
67-
IAndSessionBuilder WithEntries(IDictionary<string, int> entries);
66+
/// <returns>The same <see cref="IAndWithSessionBuilder"/>.</returns>
67+
IAndWithSessionBuilder WithEntries(IDictionary<string, int> entries);
6868
}
6969
}
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+
using System.Collections.Generic;
4+
5+
/// <summary>
6+
/// Used for building <see cref="Microsoft.AspNetCore.Http.ISession"/>.
7+
/// </summary>
8+
public interface IWithoutSessionBuilder
9+
{
10+
IAndWithoutSessionBuilder WithoutEntry(string key);
11+
12+
IAndWithoutSessionBuilder ClearSession();
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Data
2+
{
3+
using Microsoft.AspNetCore.Http;
4+
5+
public abstract class SessionBaseBuilder
6+
{
7+
/// <summary>
8+
/// Gets the <see cref="ISession"/>.
9+
/// </summary>
10+
/// <value>The built <see cref="ISession"/>.</value>
11+
protected ISession Session { get; }
12+
13+
public SessionBaseBuilder(ISession session) => this.Session = session;
14+
}
15+
}

src/MyTested.AspNetCore.Mvc.Session/Builders/Data/SessionBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.Session/Builders/Data/WithSessionBuilder.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@
1111
/// <summary>
1212
/// Used for building <see cref="ISession"/>.
1313
/// </summary>
14-
public class SessionBuilder : IAndSessionBuilder
14+
public class WithSessionBuilder : SessionBaseBuilder, IAndWithSessionBuilder
1515
{
1616
/// <summary>
17-
/// Initializes a new instance of the <see cref="SessionBuilder"/> class.
17+
/// Initializes a new instance of the <see cref="WithSessionBuilder"/> class.
1818
/// </summary>
1919
/// <param name="session"><see cref="ISession"/> to built.</param>
20-
public SessionBuilder(ISession session) => this.Session = session;
21-
22-
/// <summary>
23-
/// Gets the <see cref="ISession"/>.
24-
/// </summary>
25-
/// <value>The built <see cref="ISession"/>.</value>
26-
protected ISession Session { get; }
20+
public WithSessionBuilder(ISession session) : base(session) { }
2721

2822
/// <inheritdoc />
29-
public IAndSessionBuilder WithId(string sessionId)
23+
public IAndWithSessionBuilder WithId(string sessionId)
3024
{
3125
if (!(this.Session is ISessionMock mockedSession))
3226
{
@@ -38,28 +32,28 @@ public IAndSessionBuilder WithId(string sessionId)
3832
}
3933

4034
/// <inheritdoc />
41-
public IAndSessionBuilder WithEntry(string key, byte[] value)
35+
public IAndWithSessionBuilder WithEntry(string key, byte[] value)
4236
{
4337
this.Session.Set(key, value);
4438
return this;
4539
}
4640

4741
/// <inheritdoc />
48-
public IAndSessionBuilder WithEntry(string key, string value)
42+
public IAndWithSessionBuilder WithEntry(string key, string value)
4943
{
5044
this.Session.SetString(key, value);
5145
return this;
5246
}
5347

5448
/// <inheritdoc />
55-
public IAndSessionBuilder WithEntry(string key, int value)
49+
public IAndWithSessionBuilder WithEntry(string key, int value)
5650
{
5751
this.Session.SetInt32(key, value);
5852
return this;
5953
}
6054

6155
/// <inheritdoc />
62-
public IAndSessionBuilder WithEntries(object entries)
56+
public IAndWithSessionBuilder WithEntries(object entries)
6357
{
6458
var entriesAsDictionary = new RouteValueDictionary(entries);
6559
entriesAsDictionary.ForEach(e =>
@@ -83,27 +77,27 @@ public IAndSessionBuilder WithEntries(object entries)
8377
}
8478

8579
/// <inheritdoc />
86-
public IAndSessionBuilder WithEntries(IDictionary<string, byte[]> entries)
80+
public IAndWithSessionBuilder WithEntries(IDictionary<string, byte[]> entries)
8781
{
8882
entries.ForEach(e => this.WithEntry(e.Key, e.Value));
8983
return this;
9084
}
9185

9286
/// <inheritdoc />
93-
public IAndSessionBuilder WithEntries(IDictionary<string, string> entries)
87+
public IAndWithSessionBuilder WithEntries(IDictionary<string, string> entries)
9488
{
9589
entries.ForEach(e => this.WithEntry(e.Key, e.Value));
9690
return this;
9791
}
9892

9993
/// <inheritdoc />
100-
public IAndSessionBuilder WithEntries(IDictionary<string, int> entries)
94+
public IAndWithSessionBuilder WithEntries(IDictionary<string, int> entries)
10195
{
10296
entries.ForEach(e => this.WithEntry(e.Key, e.Value));
10397
return this;
10498
}
10599

106100
/// <inheritdoc />
107-
public ISessionBuilder AndAlso() => this;
101+
public IWithSessionBuilder AndAlso() => this;
108102
}
109103
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Data
2+
{
3+
using Contracts.Data;
4+
using Microsoft.AspNetCore.Http;
5+
6+
public class WithoutSessionBuilder : SessionBaseBuilder, IAndWithoutSessionBuilder
7+
{
8+
public WithoutSessionBuilder(ISession session) : base(session)
9+
{
10+
}
11+
12+
public IAndWithoutSessionBuilder WithoutEntry(string key)
13+
{
14+
this.Session.Remove(key);
15+
return this;
16+
}
17+
18+
public IAndWithoutSessionBuilder ClearSession()
19+
{
20+
this.Session.Clear();
21+
return this;
22+
}
23+
24+
public IWithoutSessionBuilder AndAlso() => this;
25+
}
26+
}

src/MyTested.AspNetCore.Mvc.Session/ComponentBuilderSessionExtensions.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,33 @@ public static class ComponentBuilderSessionExtensions
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="sessionBuilder">Action setting the <see cref="Microsoft.AspNetCore.Http.ISession"/> values by using <see cref="ISessionBuilder"/>.</param>
19+
/// <param name="sessionBuilder">Action setting the <see cref="Microsoft.AspNetCore.Http.ISession"/> values by using <see cref="IWithSessionBuilder"/>.</param>
2020
/// <returns>The same component builder.</returns>
2121
public static TBuilder WithSession<TBuilder>(
2222
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
23-
Action<ISessionBuilder> sessionBuilder)
23+
Action<IWithSessionBuilder> sessionBuilder)
2424
where TBuilder : IBaseTestBuilder
2525
{
2626
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
2727

28-
sessionBuilder(new SessionBuilder(actualBuilder.TestContext.Session));
28+
sessionBuilder(new WithSessionBuilder(actualBuilder.TestContext.Session));
29+
30+
return actualBuilder.Builder;
31+
}
32+
33+
public static TBuilder WithoutSession<TBuilder>(
34+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder)
35+
where TBuilder : IBaseTestBuilder
36+
=> builder.WithoutSession(session => session.ClearSession());
37+
38+
public static TBuilder WithoutSession<TBuilder>(
39+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
40+
Action<IWithoutSessionBuilder> sessionBuilder)
41+
where TBuilder : IBaseTestBuilder
42+
{
43+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
44+
45+
sessionBuilder(new WithoutSessionBuilder(actualBuilder.TestContext.Session));
2946

3047
return actualBuilder.Builder;
3148
}

0 commit comments

Comments
 (0)