From 7b00a9158076b4bc7f47d62f6b85f8f8ea813be0 Mon Sep 17 00:00:00 2001 From: mcmuzzle Date: Wed, 12 Mar 2025 22:46:41 +0100 Subject: [PATCH] ajout de la commande de creation de match --- Giants.sln | 7 ++++ .../Src/MatchRepositoryMock.cs | 28 +++++++++++++- .../Src/Commands/BaseCommandResult.cs | 11 ++++++ Src/Giants.Core/Src/Commands/CommandBase.cs | 17 +++++++++ .../Src/Commands/NewMatchCommand.cs | 38 +++++++++++++++++++ Src/Giants.Core/Src/Entities/Match.cs | 1 + .../Src/Interfaces/IMatchRepository.cs | 6 +++ .../ApplicationTests.cs | 32 ++++++++++++++++ .../Giants.Application.Tests.csproj | 27 +++++++++++++ Tests/Giants.Core.Tests/ApplicationTests.cs | 12 ------ .../Commands/NewMatchCommandTest.cs | 34 +++++++++++++++++ 11 files changed, 199 insertions(+), 14 deletions(-) create mode 100644 Src/Giants.Core/Src/Commands/BaseCommandResult.cs create mode 100644 Src/Giants.Core/Src/Commands/CommandBase.cs create mode 100644 Src/Giants.Core/Src/Commands/NewMatchCommand.cs create mode 100644 Tests/Giants.Application.Tests/ApplicationTests.cs create mode 100644 Tests/Giants.Application.Tests/Giants.Application.Tests.csproj delete mode 100644 Tests/Giants.Core.Tests/ApplicationTests.cs create mode 100644 Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs diff --git a/Giants.sln b/Giants.sln index 2c9bfe6..a4d29da 100644 --- a/Giants.sln +++ b/Giants.sln @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Giants.Infrastructure", "Sr EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Giants.Application", "Src\Giants.Application\Giants.Application.csproj", "{1EF04517-2D31-4130-A687-7954A5431312}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Giants.Application.Tests", "Tests\Giants.Application.Tests\Giants.Application.Tests.csproj", "{7A23A70F-68C9-4463-A980-E7597A621FDD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -40,11 +42,16 @@ Global {1EF04517-2D31-4130-A687-7954A5431312}.Debug|Any CPU.Build.0 = Debug|Any CPU {1EF04517-2D31-4130-A687-7954A5431312}.Release|Any CPU.ActiveCfg = Release|Any CPU {1EF04517-2D31-4130-A687-7954A5431312}.Release|Any CPU.Build.0 = Release|Any CPU + {7A23A70F-68C9-4463-A980-E7597A621FDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7A23A70F-68C9-4463-A980-E7597A621FDD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7A23A70F-68C9-4463-A980-E7597A621FDD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7A23A70F-68C9-4463-A980-E7597A621FDD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {A7B8B752-2AE9-4FB2-8F51-733F96C4CFD0} = {824D17BD-3CFE-498A-967D-3F42DF5EE92D} {30032CBC-0B7B-4BF3-9682-C506460F0FE6} = {F34871C1-07BB-4012-A310-3FFDD6DC7DBA} {E6B2C76F-9CE2-4ADA-97F9-CEE24D48B65F} = {824D17BD-3CFE-498A-967D-3F42DF5EE92D} {1EF04517-2D31-4130-A687-7954A5431312} = {824D17BD-3CFE-498A-967D-3F42DF5EE92D} + {7A23A70F-68C9-4463-A980-E7597A621FDD} = {F34871C1-07BB-4012-A310-3FFDD6DC7DBA} EndGlobalSection EndGlobal diff --git a/Src/Giants.Application/Src/MatchRepositoryMock.cs b/Src/Giants.Application/Src/MatchRepositoryMock.cs index 461a1a1..e71a5d9 100644 --- a/Src/Giants.Application/Src/MatchRepositoryMock.cs +++ b/Src/Giants.Application/Src/MatchRepositoryMock.cs @@ -10,6 +10,9 @@ namespace Giants.Application; public class MatchRepositoryMock : IMatchRepository { private readonly IHexagonalGrid _gridLayout; + private static int _lastId = 1; + + private Dictionary _matchs = new Dictionary(); /// /// constructeur @@ -22,10 +25,31 @@ public class MatchRepositoryMock : IMatchRepository /// public Match? GetMatch(int matchId) { - Match result = new Match(_gridLayout); + if (_matchs.TryGetValue(matchId, out Match? found)) + { + return found; + } + return null; + } + + /// + public Match CreateMatch() + { + Match result = new Match(_gridLayout) + { + Id = _lastId++ + }; + + AddStaticData(result); + + _matchs.Add(result.Id, result); - // Ajout des données statiques return result; } + private void AddStaticData(Match match) + { + + } + } \ No newline at end of file diff --git a/Src/Giants.Core/Src/Commands/BaseCommandResult.cs b/Src/Giants.Core/Src/Commands/BaseCommandResult.cs new file mode 100644 index 0000000..10ecb46 --- /dev/null +++ b/Src/Giants.Core/Src/Commands/BaseCommandResult.cs @@ -0,0 +1,11 @@ +namespace Giants.Core.Commands; + +/// +/// Class de base des resultats de commande. Chaque commande doit retourner un resultat quand elle +/// s'execute et les données de ce resultat est propre a la commande +/// +public abstract class BaseCommandResult +{ + /// indique si le resultat est un success (true) ou un echec(false) + public bool Success { get; init; } = false; +} \ No newline at end of file diff --git a/Src/Giants.Core/Src/Commands/CommandBase.cs b/Src/Giants.Core/Src/Commands/CommandBase.cs new file mode 100644 index 0000000..38c62e9 --- /dev/null +++ b/Src/Giants.Core/Src/Commands/CommandBase.cs @@ -0,0 +1,17 @@ +namespace Giants.Core.Commands; + +/// +/// Base des commande permettant d'agir sur le modele +/// +/// Le type de resultat que retourne cette commande +public abstract class BaseCommand where T : BaseCommandResult +{ + protected abstract T LocalExecute(); + + /// Executer la commande, si nécessaire il faut avoir rempli les parametres en amont + public T Execute() + { + // C'est ici que sera validé l'exécution + return LocalExecute(); + } +} \ No newline at end of file diff --git a/Src/Giants.Core/Src/Commands/NewMatchCommand.cs b/Src/Giants.Core/Src/Commands/NewMatchCommand.cs new file mode 100644 index 0000000..d643f48 --- /dev/null +++ b/Src/Giants.Core/Src/Commands/NewMatchCommand.cs @@ -0,0 +1,38 @@ +using Giants.Core.Interfaces; + +namespace Giants.Core.Commands; + +/// +/// Commande permettant de créer un nouveau match +/// +public class NewMatchCommand : BaseCommand +{ + private readonly IMatchRepository _repo; + + /// + /// constructeur + /// + /// le repository de match + public NewMatchCommand(IMatchRepository repo) + { + _repo = repo; + } + + /// + protected override sealed NewMatchResult LocalExecute() + { + var match = _repo.CreateMatch(); + + return new NewMatchResult() + { + Match = match, + Success = match != null + }; + } +} + +public class NewMatchResult : BaseCommandResult +{ + /// Le match créé si success + public Match? Match { get; init; } +} \ No newline at end of file diff --git a/Src/Giants.Core/Src/Entities/Match.cs b/Src/Giants.Core/Src/Entities/Match.cs index e95bd53..2976233 100644 --- a/Src/Giants.Core/Src/Entities/Match.cs +++ b/Src/Giants.Core/Src/Entities/Match.cs @@ -11,6 +11,7 @@ namespace Giants.Core; public class Match { #region données statiques + public int Id { get; init; } IHexagonalGrid _grid; #endregion diff --git a/Src/Giants.Core/Src/Interfaces/IMatchRepository.cs b/Src/Giants.Core/Src/Interfaces/IMatchRepository.cs index 0f548e2..4879a2b 100644 --- a/Src/Giants.Core/Src/Interfaces/IMatchRepository.cs +++ b/Src/Giants.Core/Src/Interfaces/IMatchRepository.cs @@ -11,4 +11,10 @@ public interface IMatchRepository /// L'id unique du match a récupérer /// le match s'il existe ou null dans le cas contraire Match? GetMatch(int matchId); + + /// + /// Créer un match avec un ID unique + /// + /// le nouveau match a son état initial + Match? CreateMatch(); } \ No newline at end of file diff --git a/Tests/Giants.Application.Tests/ApplicationTests.cs b/Tests/Giants.Application.Tests/ApplicationTests.cs new file mode 100644 index 0000000..1f713de --- /dev/null +++ b/Tests/Giants.Application.Tests/ApplicationTests.cs @@ -0,0 +1,32 @@ +namespace Giants.Core.Tests; + +using Giants.Application; +using Giants.Core.Commands; +using Giants.Core.Interfaces; +using Microsoft.Extensions.DependencyInjection; + +public class ApplicationTests +{ + [Fact] + public void ApplicationCreationExample() + { + GiantApplication app = new GiantApplication(); + } + + [Fact] + public void TryingServiceInjection() + { + // creation des instances + IMatchRepository matchRepository = new MatchRepositoryMock(); + + var serviceCollection = new ServiceCollection(); + serviceCollection.AddScoped(repo => matchRepository); + + // Ajout des commandes disponibles + serviceCollection.AddScoped(); + + //Test d'injection + var serviceProvider = serviceCollection.BuildServiceProvider(); + NewMatchCommand? command = serviceProvider.GetService(); + } +} diff --git a/Tests/Giants.Application.Tests/Giants.Application.Tests.csproj b/Tests/Giants.Application.Tests/Giants.Application.Tests.csproj new file mode 100644 index 0000000..3a54fb0 --- /dev/null +++ b/Tests/Giants.Application.Tests/Giants.Application.Tests.csproj @@ -0,0 +1,27 @@ + + + + net9.0 + enable + enable + false + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/Giants.Core.Tests/ApplicationTests.cs b/Tests/Giants.Core.Tests/ApplicationTests.cs deleted file mode 100644 index 344445a..0000000 --- a/Tests/Giants.Core.Tests/ApplicationTests.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Giants.Core.Tests; - -using Giants.Application; - -public class ApplicationTests -{ - [Fact] - public void ApplicationCreationExample() - { - GiantApplication app = new GiantApplication(); - } -} diff --git a/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs b/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs new file mode 100644 index 0000000..b0d28cd --- /dev/null +++ b/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs @@ -0,0 +1,34 @@ +namespace Giants.Core.Tests; + +using Giants.Application; +using Giants.Core.Commands; +using Giants.Core.Interfaces; + +public class NewMatchCommandTest +{ + private readonly IMatchRepository _repo = new MatchRepositoryMock(); + + [Fact] + public void CreateBasicMatch() + { + NewMatchCommand command = new NewMatchCommand(_repo); + NewMatchResult result = command.Execute(); + Assert.True(result.Success); + } + + [Fact] + public void TwoNewMatchShouldHaveDifferentID() + { + NewMatchCommand command = new NewMatchCommand(_repo); + NewMatchResult result = command.Execute(); + Assert.True(result.Success); + Assert.NotNull(result.Match); + + NewMatchCommand command2 = new NewMatchCommand(_repo); + NewMatchResult result2 = command2.Execute(); + Assert.True(result2.Success); + Assert.NotNull(result2.Match); + + Assert.NotEqual(result.Match.Id, result2.Match.Id); + } +}