@@ -67,13 +67,17 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
6767 // Mocking
6868
6969 public interface Mockable {
70- void thenReturn (SObject record );
70+ // SObject
71+ Mockable thenReturn (SObject record );
7172 }
7273
7374 @TestVisible
7475 private static Mockable mock (String mockId ) {
75- queryIdToMock .put (mockId , new SoqlMock ());
76- return queryIdToMock .get (mockId );
76+ if (! SOQLCache .queryIdToMock .containsKey (mockId )) {
77+ SOQLCache .queryIdToMock .put (mockId , new List <SoqlMock >());
78+ }
79+ SOQLCache .queryIdToMock .get (mockId ).add (new SoqlMock ());
80+ return SOQLCache .queryIdToMock .get (mockId ).get (SOQLCache .queryIdToMock .get (mockId ).size () - 1 );
7781 }
7882
7983 public static void removeFromCache (List <SObject > records ) {
@@ -93,12 +97,13 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
9397
9498 @TestVisible // deprecated
9599 private static void setMock (String mockId , SObject record ) {
96- mock (mockId ).thenReturn (record );
100+ SOQLCache .queryIdToMock .put (mockId , new List <SoqlMock >{ new SoqlMock () });
101+ SOQLCache .queryIdToMock .get (mockId ).get (SOQLCache .queryIdToMock .get (mockId ).size () - 1 ).thenReturn (record );
97102 }
98103
99104 // Implementation
100105
101- private static Map <String , SoqlMock > queryIdToMock = new Map <String , SoqlMock >();
106+ private static Map <String , List < SoqlMock >> queryIdToMock = new Map <String , List < SoqlMock > >();
102107
103108 private Executor executor ;
104109 private Cache cache ;
@@ -517,10 +522,12 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
517522 }
518523
519524 public class SoqlMock implements Mockable {
520- private List <SObject > mockedRecords = null ;
525+ private List <SObject > mockedRecords = new List <SObject >();
526+ private Integer countMock ;
521527
522- public void thenReturn (SObject record ) {
523- this .mockedRecords = new List <SObject >{ record };
528+ public Mockable thenReturn (SObject record ) {
529+ this .mockedRecords .add (record );
530+ return this ;
524531 }
525532
526533 public List <SObject > getMockedResult () {
@@ -532,19 +539,19 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
532539 private SOQL.Queryable currentyQuery ;
533540 private Cache cache ;
534541 private AccessType accessType = null ;
535- private SoqlMock mock ;
542+ private List < SoqlMock > mocks = new List < SoqlMock >() ;
536543
537544 public Executor (SOQL.Queryable currentyQuery , Cache cache ) {
538545 this .currentyQuery = currentyQuery ;
539546 this .cache = cache ;
540547 }
541548
542549 public void stripInaccessible (AccessType type ) {
543- accessType = type ;
550+ this . accessType = type ;
544551 }
545552
546- public void mock (SoqlMock mock ) {
547- this .mock = mock ;
553+ public void mock (List < SoqlMock > mocks ) {
554+ this .mocks = mocks ?? new List < SoqlMock >() ;
548555 }
549556
550557 public SObject toObject () {
@@ -562,8 +569,8 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
562569 }
563570
564571 private List <SObject > toList () {
565- if (this .mock != null ) {
566- return this .mock . getMockedResult ();
572+ if (! this .mocks . isEmpty () ) {
573+ return this .getMockedList ();
567574 }
568575
569576 List <SObject > records = this .cache .toList ();
@@ -583,6 +590,13 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
583590
584591 return Security .stripInaccessible (this .accessType , records ).getRecords ();
585592 }
593+
594+ private List <SObject > getMockedList () {
595+ if (this .mocks .size () == 1 ) {
596+ return this .mocks [0 ].getMockedResult ();
597+ }
598+ return this .mocks .remove (0 ).getMockedResult ();
599+ }
586600 }
587601
588602 public class SoqlCacheException extends Exception {}
0 commit comments