Skip to content

Commit

Permalink
Fix compiler hints
Browse files Browse the repository at this point in the history
  • Loading branch information
killij committed Oct 3, 2023
1 parent c0512f7 commit 8fd23e4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ContentControllerServerTests
{
private CpdTestServerApplication _application;
private HttpClient _httpClient;
private static string ContentUrl = "/";
private static readonly string _contentUrl = "/";

[SetUp]
public void SetUp()
Expand All @@ -40,7 +40,7 @@ public async Task Content_Will_Contain_Warning_If_Data_Is_Self_Referential()
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>()).Returns(contentCollection);

// act
var response = await _httpClient.GetAsync(ContentUrl);
var response = await _httpClient.GetAsync(_contentUrl);
var responseContent = await response.Content.ReadAsStringAsync();

// assert
Expand All @@ -52,13 +52,15 @@ public async Task Content_Will_Contain_Warning_If_Data_Is_Self_Referential()
public async Task Content_Will_Contain_Warning_If_Data_Has_An_Unknown_Content_Type()
{
// arrange
var content = new Content();
content.Items = new List<IContent> { new TestingContentItem() };
var content = new Content
{
Items = new List<IContent> { new TestingContentItem() }
};
var contentCollection = new ContentfulCollection<Content>() { Items = new List<Content>() { content } };
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>()).Returns(contentCollection);

// act
var response = await _httpClient.GetAsync(ContentUrl);
var response = await _httpClient.GetAsync(_contentUrl);
var responseContent = await response.Content.ReadAsStringAsync();

// assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public void SetUp()

_contentfulClient = Substitute.For<ICpdContentfulClient>();

_contentController = new ContentController(_contentfulClient);
_contentController.ControllerContext = controllerContext;
_contentController.TempData = Substitute.For<ITempDataDictionary>();
_contentController = new ContentController(_contentfulClient)
{
ControllerContext = controllerContext,
TempData = Substitute.For<ITempDataDictionary>()
};
}

[Test]
Expand Down Expand Up @@ -124,13 +126,13 @@ public async Task Index_Sets_The_ContextModel_Preferences_Set_Value_Correctly(bo
actual.PreferenceSet.Should().Be(preferenceSet);
}

public static object[] SideMenuContent =
private static readonly object[] _sideMenuContent =
{
new object[] { new SideMenu() },
new object[] { null },
};

[TestCaseSource(nameof(SideMenuContent))]
[TestCaseSource(nameof(_sideMenuContent))]
public async Task Index_Sets_The_ContextModel_UseContainers_From_SideMenu_Value_Correctly(SideMenu sideMenu)
{
// arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ public void SetUp()

_contentfulClient = Substitute.For<ICpdContentfulClient>();

_cookieController = new CookieController(_contentfulClient, new CookieHelper(new ApplicationConfiguration()));
_cookieController.ControllerContext = controllerContext;
_cookieController.TempData = Substitute.For<ITempDataDictionary>();
_cookieController = new CookieController(_contentfulClient, new CookieHelper(new ApplicationConfiguration()))
{
ControllerContext = controllerContext,
TempData = Substitute.For<ITempDataDictionary>()
};
}

[Test]
Expand Down Expand Up @@ -120,13 +122,13 @@ public async Task Cookies_Sets_The_ContextModel_Preferences_Set_Value_Correctly(
actual.PreferenceSet.Should().Be(preferenceSet);
}

public static object[] SideMenuContent =
private static readonly object[] _sideMenuContent =
{
new object[] { new SideMenu() },
new object[] { null },
};

[TestCaseSource(nameof(SideMenuContent))]
[TestCaseSource(nameof(_sideMenuContent))]
public async Task Cookies_Sets_The_ContextModel_UseContainers_Ignoring_The_SideMenu_Value(SideMenu sideMenu)
{
// arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task Search_With_Empty_Query_Returns_View()
SetContent(null, null);

// act
var actual = await _resourcesController.Search(_cancellationTokenSource.Token, query: null) as ViewResult;
var actual = await _resourcesController.Search(query: null, _cancellationTokenSource.Token) as ViewResult;

// assert
actual.Should().BeOfType<ViewResult>();
Expand All @@ -92,14 +92,9 @@ public async Task Search_Page_Resource_Is_Passed_To_View()
// arrange
var content = new Content();
SetContent(content, null);
var query = new ResourcesQuery
{
Page = 1,
Tags = Array.Empty<int>()
};

// act
var actual = (await _resourcesController.Search(_cancellationTokenSource.Token, query: null) as ViewResult)?.Model as ResourcesListViewModel;
var actual = (await _resourcesController.Search(query: null, _cancellationTokenSource.Token) as ViewResult)?.Model as ResourcesListViewModel;

// assert
actual.Content.Should().Be(content);
Expand All @@ -112,7 +107,7 @@ public async Task Search_Sets_The_ViewState_ContextModel()
SetContent(null, null);

// act
await _resourcesController.Search(_cancellationTokenSource.Token, null);
await _resourcesController.Search(null, _cancellationTokenSource.Token);
var actual = _resourcesController.ViewData["ContextModel"] as ContextModel;

// assert
Expand All @@ -134,7 +129,7 @@ public async Task Search_Selected_Tags_Are_Passed_Into_View()
};

// act
var actual = (await _resourcesController.Search(_cancellationTokenSource.Token, query) as ViewResult)?.Model as ResourcesListViewModel;
var actual = (await _resourcesController.Search(query, _cancellationTokenSource.Token) as ViewResult)?.Model as ResourcesListViewModel;

// assert
actual.SelectedTags.Should().Equal(query.Tags);
Expand All @@ -159,7 +154,7 @@ public async Task Search_Page_Set_To_Be_In_Bounds()
};

// act
var actual = (await _resourcesController.Search(_cancellationTokenSource.Token, query) as ViewResult)?.Model as ResourcesListViewModel;
var actual = (await _resourcesController.Search(query, _cancellationTokenSource.Token) as ViewResult)?.Model as ResourcesListViewModel;

// assert
actual.CurrentPage.Should().Be(1);
Expand All @@ -178,9 +173,9 @@ public async Task Search_Invalid_Tags_Logs_Warning()
};

// act
await _resourcesController.Search(_cancellationTokenSource.Token, query);
await _resourcesController.Search(query, _cancellationTokenSource.Token);

//assert
_logger.ReceivedWithAnyArgs(1).LogWarning(default, args: default);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Childrens_Social_Care_CPD_Tests;

internal class CpdTestServerApplication : WebApplicationFactory<Program>
{
private ICpdContentfulClient _cpdContentfulClient;
private readonly ICpdContentfulClient _cpdContentfulClient;
private ILoggerFactory _loggerFactory;

public CpdTestServerApplication()
Expand Down
8 changes: 4 additions & 4 deletions Childrens-Social-Care-CPD/Controllers/CookieController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CookieController : Controller
{
private readonly ICpdContentfulClient _cpdClient;
private readonly ICookieHelper _cookieHelper;
private const string PageName = "cookies";
private const string _pageName = "cookies";

public CookieController(ICpdContentfulClient cpdClient, ICookieHelper cookieHelper)
{
Expand Down Expand Up @@ -39,11 +39,11 @@ public IActionResult SetPreferences(string consentValue, string sourcePage = nul
[Route("/cookies")]
public async Task<IActionResult> Cookies(CancellationToken cancellationToken, string sourcePage = null, bool preferenceSet = false)
{
sourcePage = sourcePage ?? string.Empty;
sourcePage ??= string.Empty;

var queryBuilder = QueryBuilder<Content>.New
.ContentTypeIs("content")
.FieldEquals("fields.id", PageName)
.FieldEquals("fields.id", _pageName)
.Include(10);

var result = await _cpdClient.GetEntries(queryBuilder, cancellationToken);
Expand All @@ -60,7 +60,7 @@ public async Task<IActionResult> Cookies(CancellationToken cancellationToken, st
}

var consentState = _cookieHelper.GetRequestAnalyticsCookieState(HttpContext);
var contextModel = new ContextModel(pageContent.Id, pageContent.Title, PageName, pageContent.Category, true, preferenceSet, true);
var contextModel = new ContextModel(pageContent.Id, pageContent.Title, _pageName, pageContent.Category, true, preferenceSet, true);
ViewData["ContextModel"] = contextModel;

var model = new CookiesAndAnalyticsConsentModel
Expand Down
10 changes: 5 additions & 5 deletions Childrens-Social-Care-CPD/Controllers/ResourcesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private Task<ContentfulCollection<Content>> FetchResourcesContentAsync(Cancellat
return _cpdClient.GetEntries(queryBuilder, cancellationToken);
}

private Task<ContentfulCollection<Resource>> FetchResourceSearchResultsAsync(CancellationToken cancellationToken, int[] tags, int skip = 0, int limit = 5)
private Task<ContentfulCollection<Resource>> FetchResourceSearchResultsAsync(int[] tags, CancellationToken cancellationToken, int skip = 0, int limit = 5)
{
var queryBuilder = QueryBuilder<Resource>.New
.ContentTypeIs("resource")
Expand All @@ -75,10 +75,10 @@ private Task<ContentfulCollection<Resource>> FetchResourceSearchResultsAsync(Can
return _cpdClient.GetEntries(queryBuilder, cancellationToken);
}

private async Task<Tuple<Content, ContentfulCollection<Resource>>> GetContentAsync(CancellationToken cancellationToken, int[] tags, int skip = 0, int limit = 5)
private async Task<Tuple<Content, ContentfulCollection<Resource>>> GetContentAsync(int[] tags, CancellationToken cancellationToken, int skip = 0, int limit = 5)
{
var pageContentTask = FetchResourcesContentAsync(cancellationToken);
var searchContentTask = FetchResourceSearchResultsAsync(cancellationToken, tags, skip, limit);
var searchContentTask = FetchResourceSearchResultsAsync(tags, cancellationToken, skip, limit);

await Task.WhenAll(pageContentTask, searchContentTask);
return Tuple.Create(pageContentTask.Result?.FirstOrDefault(), searchContentTask.Result);
Expand All @@ -105,13 +105,13 @@ private static string GetPagingFormatString(int[] tags)

[Route("resources", Name = "Resource")]
[HttpGet]
public async Task<IActionResult> Search(CancellationToken cancellationToken, [FromQuery] ResourcesQuery query, bool preferencesSet = false)
public async Task<IActionResult> Search([FromQuery] ResourcesQuery query, CancellationToken cancellationToken, bool preferencesSet = false)
{
query ??= new ResourcesQuery();
query.Tags ??= Array.Empty<int>();

(var page, var skip, var pageSize) = CalculatePaging(query);
(var pageContent, var contentCollection) = await GetContentAsync(cancellationToken, query.Tags, skip, pageSize);
(var pageContent, var contentCollection) = await GetContentAsync(query.Tags, cancellationToken, skip, pageSize);

var totalPages = (int)Math.Ceiling((decimal)contentCollection.Total / pageSize);
page = Math.Min(page, totalPages);
Expand Down

0 comments on commit 8fd23e4

Please sign in to comment.