Skip to content

Commit

Permalink
Fixing compilation issues introduced after refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed Apr 15, 2022
1 parent f527bde commit 8599220
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .idea/illuminatedCloud.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/test-data-framework.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .sfdx/sfdx-config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"defaultusername": "test-data-framework"
"defaultusername": "test-phcf1dmuknes@example.com"
}
Binary file not shown.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ also allows you to create custom builders for different SObjectTypes.

To create a custom builder you will need to **both** extend `SObjectTestDataBuilder` and implement `ITestDataBuilder`.
For the framework to automatically detect your custom builder it also **needs* to have a name that ends in `TestDataBuilder`
(though there is a way to explicitly specify the builder to the framework, explained below):
(though there is a way to explicitly specify the builder to the framework, explained below).

Any custom builder should start with some boilerplate code that looks as follows:

```apex
@IsTest
Expand All @@ -146,6 +148,10 @@ public with sharing class OrderTestDataBuilder extends SObjectTestDataBuilder im
return this.registerSObjectsForInsert(numberOfRecords);
}
public OrderTestDataBuilder withChild(ITestDataBuilder childBuilder, SObjectField relationshipField) {
return (OrderTestDataBuilder)this.withChildData(childBuilder, relationshipField);
}
protected override Map<SObjectField, Object> getDefaultValueMap() {
return new Map<SObjectField, Object> {
Order__c.Customer__c => bindTo(SObjectTestDataBuilder.of(Account.SObjectType))
Expand Down
6 changes: 5 additions & 1 deletion example/tests/AccountTestDataBuilder.cls
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@IsTest
public with sharing class AccountTestDataBuilder extends SObjectTestDataBuilder {
public with sharing class AccountTestDataBuilder extends SObjectTestDataBuilder implements ITestDataBuilder {
public ITestDataBuilder with(SObjectField field, Object value) {
this.withData(field, value);
return this;
Expand Down Expand Up @@ -37,6 +37,10 @@ public with sharing class AccountTestDataBuilder extends SObjectTestDataBuilder
return this;
}

public AccountTestDataBuilder withChild(ITestDataBuilder childBuilder, SObjectField relationshipField) {
return (AccountTestDataBuilder)this.withChildData(childBuilder, relationshipField);
}

protected override Map<SObjectField, Object> getDefaultValueMap() {
return new Map<SObjectField, Object> {
Account.Name => 'ACME'
Expand Down
12 changes: 12 additions & 0 deletions example/tests/IntegrationTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ private class IntegrationTests {
return this.registerSObjectsForInsert(numberOfRecords);
}

public PriceBookEntryBuilder withChild(ITestDataBuilder childBuilder, SObjectField relationshipField) {
return (PriceBookEntryBuilder)this.withChildData(childBuilder, relationshipField);
}

protected override Map<SObjectField, Object> getDefaultValueMap() {
return new Map<SObjectField, Object> {
PricebookEntry.Product2Id => bindTo(SObjectTestDataBuilder.of(Product2.SObjectType)),
Expand All @@ -296,6 +300,10 @@ private class IntegrationTests {
return this.registerSObjectsForInsert(numberOfRecords);
}

public OrderBuilder withChild(ITestDataBuilder childBuilder, SObjectField relationshipField) {
return (OrderBuilder)this.withChildData(childBuilder, relationshipField);
}

protected override Map<SObjectField, Object> getDefaultValueMap() {
return new Map<SObjectField, Object> {
Order.Status => 'Draft',
Expand Down Expand Up @@ -323,6 +331,10 @@ private class IntegrationTests {
return this.registerSObjectsForInsert(numberOfRecords);
}

public OrderItemBuilder withChild(ITestDataBuilder childBuilder, SObjectField relationshipField) {
return (OrderItemBuilder)this.withChildData(childBuilder, relationshipField);
}

protected override Map<SObjectField, Object> getDefaultValueMap() {
return new Map<SObjectField, Object> {
OrderItem.PricebookEntryId => bindTo(SObjectTestDataBuilder.of(PricebookEntry.SObjectType)),
Expand Down
4 changes: 4 additions & 0 deletions example/tests/OrderItemTestDataBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public with sharing class OrderItemTestDataBuilder extends SObjectTestDataBuilde
return this.registerSObjectsForInsert(numberOfRecords);
}

public OrderItemTestDataBuilder withChild(ITestDataBuilder childBuilder, SObjectField relationshipField) {
return (OrderItemTestDataBuilder)this.withChildData(childBuilder, relationshipField);
}

protected override Map<SObjectField, Object> getDefaultValueMap() {
return new Map<SObjectField, Object> {
OrderItem__c.Order__c => bindTo(SObjectTestDataBuilder.of(Order__c.SObjectType)),
Expand Down
4 changes: 4 additions & 0 deletions example/tests/OrderTestDataBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public with sharing class OrderTestDataBuilder extends SObjectTestDataBuilder im
return this.registerSObjectsForInsert(numberOfRecords);
}

public OrderTestDataBuilder withChild(ITestDataBuilder childBuilder, SObjectField relationshipField) {
return (OrderTestDataBuilder)this.withChildData(childBuilder, relationshipField);
}

protected override Map<SObjectField, Object> getDefaultValueMap() {
return new Map<SObjectField, Object> {
Order__c.Customer__c => bindTo(SObjectTestDataBuilder.of(Account.SObjectType))
Expand Down
8 changes: 4 additions & 4 deletions force-app/main/default/classes/SObjectTestDataBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @description Provides base functionality for building SObject records for testing purposes. Any custom TestDataBuilder
* must extend this class as well as implement {@link ITestDataBuilder}.
*/
public abstract class SObjectTestDataBuilder implements ITestDataCallback.ITestDataCallback {
public abstract class SObjectTestDataBuilder implements ITestDataCallback {
private static TestDataBuilderCache builderCache = new TestDataBuilderCache();
private static History testDataHistory = new History();
private static List<LateBinding> allBindings = new List<LateBinding>();
Expand Down Expand Up @@ -397,7 +397,7 @@ public abstract class SObjectTestDataBuilder implements ITestDataCallback.ITestD
}

public void log(List<SObject> insertedRecords, SObject parentRecord, SObjectField relationshipField,
ITestDataCallback.ITestDataCallback callback) {
ITestDataCallback callback) {
for (SObject insertedRecord : insertedRecords) {
HistoryItem item = new HistoryItem(insertedRecord, parentRecord, relationshipField, callback);
log(item);
Expand Down Expand Up @@ -480,10 +480,10 @@ public abstract class SObjectTestDataBuilder implements ITestDataCallback.ITestD
private SObject record;
private SObject parentRecord;
private SObjectField relationshipToParent;
private ITestDataCallback.ITestDataCallback callback;
private ITestDataCallback callback;

public HistoryItem(SObject record, SObject parentRecord, SObjectField relationshipField,
ITestDataCallback.ITestDataCallback callback) {
ITestDataCallback callback) {
this.record = record;
this.parentRecord = parentRecord;
this.relationshipToParent = relationshipField;
Expand Down
6 changes: 3 additions & 3 deletions force-app/main/default/classes/TestDataUnitOfWork.cls
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public class TestDataUnitOfWork {

private class NewSObjectAndCallback {
public SObject Record { get; private set; }
public ITestDataCallback.ITestDataCallback Callback { get; set; }
public ITestDataCallback Callback { get; set; }

public NewSObjectAndCallback(SObject record, ITestDataCallback.ITestDataCallback callback) {
public NewSObjectAndCallback(SObject record, ITestDataCallback callback) {
this.Record = record;
this.Callback = callback;
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public class TestDataUnitOfWork {
* @param record A newly created SObject instance to be inserted during commitWork
* @param callback TestDataCallback.ITestDataCallback to be called before and after record insert.
**/
public void registerNew(SObject record, ITestDataCallback.ITestDataCallback callback) {
public void registerNew(SObject record, ITestDataCallback callback) {
if (record.Id != null) {
throw new UnitOfWorkException('Only new records can be registered as new');
}
Expand Down
34 changes: 20 additions & 14 deletions sfdx-project.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
{
"packageDirectories": [
{
"path": "force-app",
"default": true
},
{
"path": "example",
"default": false
"packageDirectories": [
{
"path": "force-app",
"default": true,
"package": "TestDataBuilder",
"versionName": "ver 0.1",
"versionNumber": "0.1.0.NEXT"
},
{
"path": "example",
"default": false
}
],
"name": "test-data-framework",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "54.0",
"packageAliases": {
"TestDataBuilder": "0Ho5w0000008OULCA2"
}
],
"name": "test-data-framework",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "54.0"
}
}

0 comments on commit 8599220

Please sign in to comment.