using AutoMapper; using Dotnet.Samples.AspNetCore.WebApi.Controllers; using Dotnet.Samples.AspNetCore.WebApi.Models; using Dotnet.Samples.AspNetCore.WebApi.Repositories; using Dotnet.Samples.AspNetCore.WebApi.Services; using FluentValidation; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Moq; namespace Dotnet.Samples.AspNetCore.WebApi.Tests.Utilities { /// /// A Mock is a dynamic test double that not only provides responses but /// also records interactions. Mocks are pre‑programmed with expectations /// (like “this method should be called once with these parameters”) and can /// later verify that the expected calls occurred. Frameworks such as Moq /// make it easy to set up and verify these interactions. /// public static class PlayerMocks { /// /// Initializes mocks for PlayerController dependencies. /// /// A tuple containing mocked service, logger, and validator. public static ( Mock service, Mock> logger, Mock> validator ) InitControllerMocks() { var service = new Mock(); var logger = new Mock>(); var validator = new Mock>(); return (service, logger, validator); } /// /// Creates a mock IUrlHelper configured to return any string for Action calls. /// /// A configured Mock of IUrlHelper. public static Mock SetupUrlHelperMock() { var mock = new Mock(); mock.Setup(url => url.Action(It.IsAny())).Returns(It.IsAny()); return mock; } /// /// Initializes mocks for PlayerService dependencies. /// /// Optional cache value to return from memory cache. /// The environment name (defaults to "Development"). /// A tuple containing mocked repository, logger, cache, mapper, and environment. public static ( Mock repository, Mock> logger, Mock memoryCache, Mock mapper, Mock environment ) InitServiceMocks(object? cacheValue = null, string environmentName = "Development") { var repository = new Mock(); var logger = new Mock>(); var memoryCache = SetupMemoryCacheMock(cacheValue ?? It.IsAny()); var mapper = new Mock(); var environment = new Mock(); environment.Setup(env => env.EnvironmentName).Returns(environmentName); return (repository, logger, memoryCache, mapper, environment); } /// /// Creates a mock IMemoryCache with configurable behavior. /// First TryGetValue call returns false, subsequent calls return true with the specified value. /// /// The value to return from cache after the first call. /// A configured Mock of IMemoryCache. public static Mock SetupMemoryCacheMock(object? value) { var cachedValue = false; var mock = new Mock(); mock.Setup(cache => cache.TryGetValue(It.IsAny(), out It.Ref.IsAny!)) .Callback( new TryGetValueDelegate( (object _, out object? output) => { output = cachedValue ? value : null; } ) ) .Returns(() => { bool hasValue = cachedValue; cachedValue = true; // Subsequent invocations will return true return hasValue; }); mock.Setup(cache => cache.CreateEntry(It.IsAny())) .Returns(Mock.Of); mock.Setup(cache => cache.Remove(It.IsAny())); return mock; } private delegate void TryGetValueDelegate(object key, out object? value); } }