From 7d8452ed2359e313b1e05f2677bbb48bb7af4533 Mon Sep 17 00:00:00 2001 From: Jordi Date: Tue, 12 Sep 2023 17:47:43 +0200 Subject: [PATCH 1/4] Updated build scripts, added new helper method to create new instances. --- CHANGELOG.md | 2 ++ FakeXrmEasy.Core.sln | 1 + build.ps1 | 11 +++------ clean.ps1 | 7 ++++++ src/FakeXrmEasy.Core/XrmFakedContext.cs | 33 +++++++++++++++++++++---- 5 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 clean.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 01778839..b0c63bd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Changed + - Updated build scripts so that it actually deletes bin folders as opposed to doing dotnet clean - https://github.com/DynamicsValue/fake-xrm-easy/issues/76 + - Introduced new NewEntityRecord method to easily create instances of entity records based on the current use of early-bound or late-bound entities - Resolves an issue with query evaluation and MultiOptionSets when using late bound entities or if type information is not present. - https://github.com/DynamicsValue/fake-xrm-easy/issues/66 ## [2.3.2] diff --git a/FakeXrmEasy.Core.sln b/FakeXrmEasy.Core.sln index f22ccbc9..182a84b0 100644 --- a/FakeXrmEasy.Core.sln +++ b/FakeXrmEasy.Core.sln @@ -14,6 +14,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{ED506DF3-19B6-420B-9172-13D54CC1A020}" ProjectSection(SolutionItems) = preProject CHANGELOG.md = CHANGELOG.md + build.ps1 = build.ps1 EndProjectSection EndProject Global diff --git a/build.ps1 b/build.ps1 index 2aca4f94..052c13f2 100644 --- a/build.ps1 +++ b/build.ps1 @@ -30,13 +30,10 @@ else } Write-Host " -> Cleaning..." -ForegroundColor Yellow -if($targetFrameworks -eq "all") -{ - dotnet clean /p:Configuration=$configuration /p:PackTests=$packTests --verbosity quiet -} -else { - dotnet clean /p:Configuration=$configuration /p:PackTests=$packTests /p:TargetFrameworks=$targetFrameworks --verbosity quiet -} +./clean.ps1 -folderPath "./src/FakeXrmEasy.Core/bin" +./clean.ps1 -folderPath "./src/FakeXrmEasy.Core/obj" +./clean.ps1 -folderPath "./tests/FakeXrmEasy.Core.Tests/bin" +./clean.ps1 -folderPath "./tests/FakeXrmEasy.Core.Tests/obj" if($targetFrameworks -eq "all") { diff --git a/clean.ps1 b/clean.ps1 new file mode 100644 index 00000000..567928df --- /dev/null +++ b/clean.ps1 @@ -0,0 +1,7 @@ +param ( + [string]$folderPath = "./src/FakeXrmEasy.Core/bin" +) + +if (Test-Path -Path $folderPath) { + Get-ChildItem -Path $folderPath -Include * -File -Recurse | foreach { $_.Delete()} +} diff --git a/src/FakeXrmEasy.Core/XrmFakedContext.cs b/src/FakeXrmEasy.Core/XrmFakedContext.cs index 7a3e60e8..6a823bca 100644 --- a/src/FakeXrmEasy.Core/XrmFakedContext.cs +++ b/src/FakeXrmEasy.Core/XrmFakedContext.cs @@ -72,12 +72,11 @@ public IEnumerable ProxyTypesAssemblies /// /// /// - [Obsolete("Please use ProxyTypesAssemblies to retrieve assemblies and EnableProxyTypes to add new ones")] + [Obsolete("Please use ProxyTypesAssemblies to retrieve assemblies and EnableProxyTypes to add new ones. This method is solely maintained for making a smoother transition to the latest versions from v1")] public Assembly ProxyTypesAssembly { get { - // TODO What we should do when ProxyTypesAssemblies contains multiple assemblies? One shouldn't throw exceptions from properties. return _proxyTypesAssemblies.FirstOrDefault(); } set @@ -310,12 +309,12 @@ public virtual void Initialize(IEnumerable entities) } /// - /// + /// Initializes the context with a single entity record /// /// - public void Initialize(Entity e) + public void Initialize(Entity entity) { - this.Initialize(new List() { e }); + this.Initialize(new List() { entity }); } /// @@ -486,5 +485,29 @@ protected IOrganizationService GetFakedOrganizationService(XrmFakedContext conte return context._service; } + /// + /// Creates a new entity record that is consistent with the current use of early-bound or late-bound entities by the current context + /// + /// The entity logical name of the entity + /// An early-bound record dif the context is already using early-bound entity records, a late bound entity otherwise + internal Entity NewEntityRecord(string logicalName) + { + if (_proxyTypesAssemblies.Any()) + { + var subClassType = FindReflectedType(logicalName); + if (subClassType != null) + { + var instance = Activator.CreateInstance(subClassType); + return (Entity) instance; + } + } + + return new Entity + { + LogicalName = logicalName, + Id = Guid.Empty + }; + } + } } \ No newline at end of file From 5ab74392ab2302f067efb79a2e727c161a4fbb79 Mon Sep 17 00:00:00 2001 From: Jordi Date: Tue, 12 Sep 2023 23:59:50 +0200 Subject: [PATCH 2/4] Add unit tests for new NewEntityRecord --- .github/workflows/CI-2x.yml | 6 ++--- .github/workflows/CI-PullRequest-2x.yml | 6 ++--- CHANGELOG.md | 1 + .../FakeContextTests/NewEntityRecordTests.cs | 26 +++++++++++++++++++ 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 tests/FakeXrmEasy.Core.Tests/FakeContextTests/NewEntityRecordTests.cs diff --git a/.github/workflows/CI-2x.yml b/.github/workflows/CI-2x.yml index 8bdc2377..bde26188 100644 --- a/.github/workflows/CI-2x.yml +++ b/.github/workflows/CI-2x.yml @@ -45,10 +45,10 @@ jobs: name: Sonar Quality Gate runs-on: windows-latest steps: - - name: Set up JDK 11 - uses: actions/setup-java@v1 + - name: Set up JDK 17 + uses: actions/setup-java@v3 with: - java-version: 1.11 + java-version: '17' - uses: actions/checkout@v2 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis diff --git a/.github/workflows/CI-PullRequest-2x.yml b/.github/workflows/CI-PullRequest-2x.yml index c7d8cdf2..acc8b3ae 100644 --- a/.github/workflows/CI-PullRequest-2x.yml +++ b/.github/workflows/CI-PullRequest-2x.yml @@ -45,10 +45,10 @@ jobs: name: Sonar Quality Gate runs-on: windows-latest steps: - - name: Set up JDK 11 - uses: actions/setup-java@v1 + - name: Set up JDK 17 + uses: actions/setup-java@v3 with: - java-version: 1.11 + java-version: '17' - uses: actions/checkout@v2 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis diff --git a/CHANGELOG.md b/CHANGELOG.md index b0c63bd7..f6c25c20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Changed +- Upgraded GitHub Actions to update Java major version to run SonarCloud analysis - https://github.com/DynamicsValue/fake-xrm-easy/issues/110 - Updated build scripts so that it actually deletes bin folders as opposed to doing dotnet clean - https://github.com/DynamicsValue/fake-xrm-easy/issues/76 - Introduced new NewEntityRecord method to easily create instances of entity records based on the current use of early-bound or late-bound entities - Resolves an issue with query evaluation and MultiOptionSets when using late bound entities or if type information is not present. - https://github.com/DynamicsValue/fake-xrm-easy/issues/66 diff --git a/tests/FakeXrmEasy.Core.Tests/FakeContextTests/NewEntityRecordTests.cs b/tests/FakeXrmEasy.Core.Tests/FakeContextTests/NewEntityRecordTests.cs new file mode 100644 index 00000000..989c1cbd --- /dev/null +++ b/tests/FakeXrmEasy.Core.Tests/FakeContextTests/NewEntityRecordTests.cs @@ -0,0 +1,26 @@ +using System; +using Crm; +using FakeXrmEasy.Tests; +using Xunit; + +namespace FakeXrmEasy.Core.Tests.FakeContextTests +{ + + public class NewEntityRecordTests: FakeXrmEasyTestsBase + { + [Fact] + public void Should_use_late_bound_if_no_early_bound_entity_has_been_used() + { + var contact = ((XrmFakedContext)_context).NewEntityRecord("contact"); + Assert.IsNotType(contact); + } + + [Fact] + public void Should_use_early_bound_if_the_context_is_using_early_bound() + { + _context.Initialize(new Contact() { Id = Guid.NewGuid()}); + var contact = ((XrmFakedContext)_context).NewEntityRecord("contact"); + Assert.IsType(contact); + } + } +} \ No newline at end of file From 148f073e57a2e4161236bb7934df0eb2f4f512af Mon Sep 17 00:00:00 2001 From: Jordi Date: Wed, 13 Sep 2023 00:16:33 +0200 Subject: [PATCH 3/4] Add distribution parameter DynamicsValue/fake-xrm-easy#110 --- .github/workflows/CI-2x.yml | 1 + .github/workflows/CI-PullRequest-2x.yml | 1 + .github/workflows/PushNuget-2x.yml | 7 ++++--- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/CI-2x.yml b/.github/workflows/CI-2x.yml index bde26188..89476161 100644 --- a/.github/workflows/CI-2x.yml +++ b/.github/workflows/CI-2x.yml @@ -48,6 +48,7 @@ jobs: - name: Set up JDK 17 uses: actions/setup-java@v3 with: + distribution: 'temurin' java-version: '17' - uses: actions/checkout@v2 with: diff --git a/.github/workflows/CI-PullRequest-2x.yml b/.github/workflows/CI-PullRequest-2x.yml index acc8b3ae..a78c505f 100644 --- a/.github/workflows/CI-PullRequest-2x.yml +++ b/.github/workflows/CI-PullRequest-2x.yml @@ -48,6 +48,7 @@ jobs: - name: Set up JDK 17 uses: actions/setup-java@v3 with: + distribution: 'temurin' java-version: '17' - uses: actions/checkout@v2 with: diff --git a/.github/workflows/PushNuget-2x.yml b/.github/workflows/PushNuget-2x.yml index 26555288..3e6a395b 100644 --- a/.github/workflows/PushNuget-2x.yml +++ b/.github/workflows/PushNuget-2x.yml @@ -42,10 +42,11 @@ jobs: name: Sonar Quality Gate runs-on: windows-latest steps: - - name: Set up JDK 11 - uses: actions/setup-java@v1 + - name: Set up JDK 17 + uses: actions/setup-java@v3 with: - java-version: 1.11 + distribution: 'temurin' + java-version: '17' - uses: actions/checkout@v2 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis From f8032396de13c97cabdf20c83150bcea8dab562a Mon Sep 17 00:00:00 2001 From: Jordi Date: Thu, 14 Sep 2023 01:09:52 +0200 Subject: [PATCH 4/4] Make the method public so it can be used by other extension packages (or custom client-implementations) --- src/FakeXrmEasy.Core/XrmFakedContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FakeXrmEasy.Core/XrmFakedContext.cs b/src/FakeXrmEasy.Core/XrmFakedContext.cs index 6a823bca..e37282da 100644 --- a/src/FakeXrmEasy.Core/XrmFakedContext.cs +++ b/src/FakeXrmEasy.Core/XrmFakedContext.cs @@ -490,7 +490,7 @@ protected IOrganizationService GetFakedOrganizationService(XrmFakedContext conte /// /// The entity logical name of the entity /// An early-bound record dif the context is already using early-bound entity records, a late bound entity otherwise - internal Entity NewEntityRecord(string logicalName) + public Entity NewEntityRecord(string logicalName) { if (_proxyTypesAssemblies.Any()) {