forked from jamessimone/apex-dml-mocking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMLMock.cls
176 lines (150 loc) · 4.31 KB
/
DMLMock.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
public virtual class DMLMock extends DML {
public static List<SObject> InsertedRecords = new List<SObject>();
public static List<SObject> UpsertedRecords = new List<SObject>();
public static List<SObject> UpdatedRecords = new List<SObject>();
public static List<SObject> DeletedRecords = new List<SObject>();
public static List<SObject> UndeletedRecords = new List<SObject>();
@TestVisible
private DMLMock() {
// @IsTest classes cannot be virtual, so enforce visibility
}
public override List<SObject> doInsert(List<SObject> recordList) {
TestingUtils.generateIds(recordList);
InsertedRecords.addAll(recordList);
return recordList;
}
public override List<SObject> doUpdate(List<SObject> recordList) {
UpdatedRecords.addAll(recordList);
return recordList;
}
public override List<SObject> doUpsert(List<SObject> recordList) {
TestingUtils.generateIds(recordList);
UpsertedRecords.addAll(recordList);
return recordList;
}
public override List<SObject> doUpsert(List<SObject> recordList, Schema.SObjectField field) {
return this.doUpsert(recordList);
}
public override List<SObject> doUndelete(List<SObject> recordList) {
UndeletedRecords.addAll(recordList);
return recordList;
}
public override void doDelete(List<SObject> recordList) {
if (recordList?.isEmpty() == false) {
DeletedRecords.addAll(recordList);
}
}
public override void doHardDelete(List<SObject> recordList) {
this.doDelete(recordList);
}
public static RecordsWrapper Inserted {
get {
return new RecordsWrapper(InsertedRecords);
}
}
public static RecordsWrapper Upserted {
get {
return new RecordsWrapper(UpsertedRecords);
}
}
public static RecordsWrapper Updated {
get {
return new RecordsWrapper(UpdatedRecords);
}
}
public static RecordsWrapper Deleted {
get {
return new RecordsWrapper(DeletedRecords);
}
}
public static RecordsWrapper Undeleted {
get {
return new RecordsWrapper(UndeletedRecords);
}
}
public class RecordsWrapper {
List<SObject> recordList;
RecordsWrapper(List<SObject> recordList) {
this.recordList = recordList;
}
public RecordsWrapper ofType(Schema.SObjectType sObjectType) {
return new RecordsWrapper(this.getRecordsMatchingType(recordList, sObjectType));
}
public RecordsWrapper Accounts {
get {
return this.ofType(Schema.Account.SObjectType);
}
}
public RecordsWrapper Leads {
get {
return this.ofType(Schema.Lead.SObjectType);
}
}
public RecordsWrapper Contacts {
get {
return this.ofType(Schema.Contact.SObjectType);
}
}
public RecordsWrapper Opportunities {
get {
return this.ofType(Schema.Opportunity.SObjectType);
}
}
public RecordsWrapper Tasks {
get {
return this.ofType(Schema.Task.SObjectType);
}
}
public List<SObject> Records {
get {
return recordList;
}
}
public Boolean hasId(Id recordId) {
Boolean exists = false;
for (SObject record : this.recordList) {
if (record.Id == recordId) {
exists = true;
}
}
return exists;
}
public Boolean hasId(Id whatId, SObjectField idField) {
Boolean exists = false;
for (SObject record : this.recordList) {
if (record.get(idField) == whatId) {
exists = true;
}
}
return exists;
}
public Integer size() {
return this.recordList.size();
}
public SObject singleOrDefault {
get {
if (recordList.size() > 1) {
throw new IllegalArgumentException('More than one value in records list');
}
return recordList.size() == 0 ? null : recordList[0];
}
}
public SObject firstOrDefault {
get {
if (recordList.size() > 0) {
return recordList[0];
}
return null;
}
}
private List<SObject> getRecordsMatchingType(List<SObject> records, Schema.SObjectType sObjectType) {
List<SObject> matchingRecords = new List<SObject>();
for (SObject record : records) {
if (record.getSObjectType() == sObjectType) {
matchingRecords.add(record);
}
}
return matchingRecords;
}
}
}