Skip to content

Commit 69a9fea

Browse files
Merge pull request apex-enterprise-patterns#23 from OrtooApps/feature/updates-to-support-example-app-repo
Feature/updates to support example app repo
2 parents 3f1ea97 + a1c58d9 commit 69a9fea

File tree

15 files changed

+378
-8
lines changed

15 files changed

+378
-8
lines changed

framework/default/fflib-apex-extensions/default/classes/criteria/fflib_Criteria.cls

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ public virtual with sharing class fflib_Criteria
125125
public virtual fflib_Criteria addOrCriteria(fflib_Criteria subCriteria)
126126
{
127127
subCriteria.orCriteria();
128-
subCriteria.setEmbraced(true);
129-
evaluators.add(subCriteria);
128+
addCriteria( subCriteria );
130129
return this;
131130
}
132131

@@ -152,6 +151,31 @@ public virtual with sharing class fflib_Criteria
152151
public virtual fflib_Criteria addAndCriteria(fflib_Criteria subCriteria)
153152
{
154153
subCriteria.andCriteria();
154+
addCriteria( subCriteria );
155+
return this;
156+
}
157+
158+
/**
159+
* Adds a sub criteria, not altering the join (AND / OR)
160+
*
161+
* @param subCriteria The condition of the sub criteria
162+
*
163+
* @return An instance of itself to enable method chaining
164+
*
165+
* @example
166+
* new fflib_Criteria()
167+
* .orCriteria()
168+
* .equalTo(Account.Name, 'Example')
169+
* .addCriteria(
170+
* new fflib_Criteria()
171+
* .equalTo(Account.AccountNumber, '0001')
172+
* .equalTo(Account.ShippingCountry, 'USA'))
173+
*
174+
* Evaluates:
175+
* Name = 'Example' OR (AccountNumber = '0001' AND ShippingCountry = 'USA')
176+
*/
177+
public fflib_Criteria addCriteria(fflib_Criteria subCriteria)
178+
{
155179
subCriteria.setEmbraced(true);
156180
evaluators.add(subCriteria);
157181
return this;

framework/default/fflib-apex-extensions/default/classes/tests/fflib_CriteriaTest.cls

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,23 @@ private with sharing class fflib_CriteriaTest
462462
);
463463
}
464464

465+
@IsTest
466+
static void itShouldEvaluateAddCondition_First()
467+
{
468+
System.assert(
469+
new fflib_Criteria()
470+
.orCriteria()
471+
.equalTo(Account.Name, A_STRING)
472+
.equalTo(Account.Name, ANOTHER_STRING)
473+
.addCriteria(
474+
new fflib_Criteria()
475+
.equalTo(Account.Name, HELLO_WORLD)
476+
.equalTo(Account.AccountNumber, '002')
477+
)
478+
.evaluate(new Account(Name = ANOTHER_STRING))
479+
);
480+
}
481+
465482
@IsTest
466483
static void itShouldEvaluateAddAndCondition_First()
467484
{

framework/default/ortoo-core/default/classes/fflib-extension/ortoo_Criteria.cls

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,33 @@ public inherited sharing virtual class ortoo_Criteria implements ISearchCriteria
100100
return this;
101101
}
102102

103+
/**
104+
* Adds a sub-criteria
105+
*
106+
* @param ortoo_Criteria The condition of the sub criteria
107+
* @return ortoo_Criteria Itself, allowing for a fluent interface
108+
*
109+
* @example
110+
* new ortoo_Criteria()
111+
* .orCriteria()
112+
* .equalTo(Account.Name, 'Example')
113+
* .addCriteria(
114+
* new ortoo_Criteria()
115+
* .equalTo(Account.AccountNumber, '0001')
116+
* .equalTo(Account.ShippingCountry, 'USA'))
117+
*
118+
* Evaluates:
119+
* Name = 'Example' OR (AccountNumber = '0001' AND ShippingCountry = 'USA')
120+
*/
121+
public ortoo_Criteria addCriteria( ortoo_Criteria subCriteria )
122+
{
123+
Contract.requires( subCriteria != null, 'addCriteria called with a null subCriteria' );
124+
Contract.requires( subCriteria.criteria != null, 'addCriteria called with a subCriteria that has no criteria defined' );
125+
126+
criteria.addCriteria( subCriteria.criteria );
127+
return this;
128+
}
129+
103130
/**
104131
* Adds a sub-criteria with AND comparator
105132
*

framework/default/ortoo-core/default/classes/fflib-extension/tests/ortoo_CriteriaTest.cls

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,48 @@ private without sharing class ortoo_CriteriaTest
9494
System.assertEquals( expected, got, 'andAndCriteria, when given sub criteria, will add the subcriteria set as AND criteria' );
9595
}
9696

97+
@isTest
98+
private static void andCriteria_whenGivenSubCriteria_addsTheSubCriteriaSetAsOriginalSpec() // NOPMD: Test method name format
99+
{
100+
ortoo_Criteria criteria = new ortoo_Criteria();
101+
102+
Test.startTest();
103+
String got = criteria
104+
.equalTo(Account.Name, 'Example')
105+
.addCriteria(
106+
new ortoo_Criteria()
107+
.andCriteria()
108+
.equalTo(Account.AccountNumber, '0001')
109+
.equalTo(Account.AccountNumber, '0002'))
110+
.toSoql();
111+
Test.stopTest();
112+
113+
String expected = 'Name=\'Example\' AND (AccountNumber=\'0001\' AND AccountNumber=\'0002\')';
114+
115+
System.assertEquals( expected, got, 'andCriteria, when given sub criteria, will add the subcriteria set as originally configured (was and)' );
116+
}
117+
118+
@isTest
119+
private static void andCriteria_whenGivenSubCriteria_addsTheSubCriteriaSetAsOriginalSpec_2() // NOPMD: Test method name format
120+
{
121+
ortoo_Criteria criteria = new ortoo_Criteria();
122+
123+
Test.startTest();
124+
String got = criteria
125+
.equalTo(Account.Name, 'Example')
126+
.addCriteria(
127+
new ortoo_Criteria()
128+
.orCriteria()
129+
.equalTo(Account.AccountNumber, '0001')
130+
.equalTo(Account.AccountNumber, '0002'))
131+
.toSoql();
132+
Test.stopTest();
133+
134+
String expected = 'Name=\'Example\' AND (AccountNumber=\'0001\' OR AccountNumber=\'0002\')';
135+
136+
System.assertEquals( expected, got, 'andCriteria, when given sub criteria, will add the subcriteria set as originally configured (was or)' );
137+
}
138+
97139
@isTest
98140
private static void formulaCriteria_whenGivenCriteria_addsTheCriteriaInLineWithTheFormula() // NOPMD: Test method name format
99141
{

framework/default/ortoo-core/default/classes/services/dml/DmlChildContext.cls

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,10 @@ public inherited sharing class DmlChildContext
105105
uow.registerRelationship( child, relatedByField, parent );
106106
}
107107
}
108+
109+
@testVisible
110+
private SobjectField getRelatedByField()
111+
{
112+
return relatedByField;
113+
}
108114
}

framework/default/ortoo-core/default/classes/services/dml/DmlDefiner.cls

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ public inherited sharing class DmlDefiner
188188
return sobjects;
189189
}
190190

191+
/**
192+
* States if this definer is configured to ensure that other records are deleted
193+
*
194+
* @return Boolean Will other records be deleted
195+
*/
196+
public Boolean getWillDeleteOtherRecords() {
197+
Contract.assert( options != null, 'getWillDeleteOtherRecords was called when options was null' );
198+
return options.getWillDeleteOtherRecords();
199+
}
200+
191201
@testVisible
192202
private List<DmlRecord> getDmlRecords() {
193203
return recordsToDml;

framework/default/ortoo-core/default/classes/services/dml/DmlDefinerOptions.cls

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ public inherited sharing class DmlDefinerOptions
4343
return new DmlDefinerOptions()
4444
.setOtherRecordsMode( OtherRecordsOption.IGNORE_RECORDS );
4545
}
46+
47+
/**
48+
* States if this definer will instruct to delete other records
49+
*
50+
* @return Boolean Should other records be deleted?
51+
*/
52+
public Boolean getWillDeleteOtherRecords()
53+
{
54+
return getOtherRecordsMode() == OtherRecordsOption.DELETE_RECORDS;
55+
}
4656
}

framework/default/ortoo-core/default/classes/services/dml/DmlRecord.cls

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,27 @@ public virtual inherited sharing class DmlRecord
159159
@testVisible
160160
protected DmlDefiner getChildDefiner( String childRecordType )
161161
{
162+
Contract.requires( childRecordType != null, 'getChildDefiner called with a null childRecordType' );
163+
162164
checkTypeIsValid( childRecordType );
163165
return this.childRecordsByType.get( childRecordType );
164166
}
165167

168+
169+
/**
170+
* Returns the Sobjects that represent the child records for the given relationship
171+
*
172+
* @param String The 'Type' that will identify the child relationship for which to return
173+
* @return List<Sobject> The List representing the child records currently on this DmlRecord
174+
*/
175+
@testVisible
176+
protected List<Sobject> getChildSobjects( String childRecordType )
177+
{
178+
Contract.requires( childRecordType != null, 'getChildSobjects called with a null childRecordType' );
179+
180+
return getChildDefiner( childRecordType ).getSobjects();
181+
}
182+
166183
/**
167184
* Sets the DmlDefinerOptions for the given child relationship.
168185
*
@@ -266,4 +283,10 @@ public virtual inherited sharing class DmlRecord
266283
checkTypeIsValid( childRecordType );
267284
return childContextsByType.get( childRecordType );
268285
}
286+
287+
@testVisible
288+
private Boolean getWillDeleteOtherRecords( String childRecordType )
289+
{
290+
return getChildDefiner( childRecordType )?.getWillDeleteOtherRecords();
291+
}
269292
}

framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerOptionsTest.cls

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,30 @@ private without sharing class DmlDefinerOptionsTest
3939

4040
ortoo_Asserts.assertContains( 'setOtherRecordsMode called with a null otherRecordsMode', exceptionMessage, 'setOtherRecordsMode, when called with null, will throw an exception' );
4141
}
42+
43+
@isTest
44+
private static void getWillDeleteOtherRecords_whenRecordModeIsDelete_returnsTrue() // NOPMD: Test method name format
45+
{
46+
DmlDefinerOptions options = new DmlDefinerOptions();
47+
48+
Test.startTest();
49+
options.setOtherRecordsMode( DmlDefinerOptions.OtherRecordsOption.DELETE_RECORDS );
50+
Boolean got = options.getWillDeleteOtherRecords();
51+
Test.stopTest();
52+
53+
System.assertEquals( true, got, 'getWillDeleteOtherRecords, when the record mode is DELETE_RECORDS, will return true' );
54+
}
55+
56+
@isTest
57+
private static void getWillDeleteOtherRecords_whenRecordModeIsIgnore_returnsFalse() // NOPMD: Test method name format
58+
{
59+
DmlDefinerOptions options = new DmlDefinerOptions();
60+
61+
Test.startTest();
62+
options.setOtherRecordsMode( DmlDefinerOptions.OtherRecordsOption.IGNORE_RECORDS );
63+
Boolean got = options.getWillDeleteOtherRecords();
64+
Test.stopTest();
65+
66+
System.assertEquals( false, got, 'getWillDeleteOtherRecords, when the record mode is IGNORE_RECORDS, will return false' );
67+
}
4268
}

framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerTest.cls

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,24 @@ private without sharing class DmlDefinerTest
311311

312312
ortoo_Asserts.assertContains( 'addPreSaveAction called with a null action', exceptionMessage, 'addPreSaveAction, when given null, will throw an exception' );
313313
}
314+
315+
@isTest
316+
private static void getWillDeleteOtherRecords_willReturnTheValueFromTheDefiner() // NOPMD: Test method name format
317+
{
318+
DmlDefiner dmlDefiner = new DmlDefiner();
319+
320+
Amoss_Instance optionsController = new Amoss_Instance( DmlDefinerOptions.class );
321+
optionsController
322+
.expects( 'getWillDeleteOtherRecords' )
323+
.returning( true );
324+
325+
Test.startTest();
326+
dmlDefiner.setOptions( (DmlDefinerOptions)optionsController.generateDouble() );
327+
Boolean got = dmlDefiner.getWillDeleteOtherRecords();
328+
Test.stopTest();
329+
330+
optionsController.verify();
331+
332+
System.assertEquals( true, got, 'getWillDeleteOtherRecords, will call getWillDeleteOtherRecords on the options and return the result' );
333+
}
314334
}

0 commit comments

Comments
 (0)