Skip to content

Commit

Permalink
Check in of related to an attempt to remove the dependency on fflib_S…
Browse files Browse the repository at this point in the history
…ObjectMocks class

In the end, this attempt is not feasible because relying on "inner class implementations of interfaces" fails with a `System.TypeException` where `Test.createStub()` cannot be called with inner Apex classes
  • Loading branch information
ImJohnMDaniel committed May 8, 2022
1 parent c7ee215 commit 24b46dc
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 391 deletions.
60 changes: 16 additions & 44 deletions sfdx-source/apex-common/main/classes/fflib_Application.cls
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ public virtual class fflib_Application
protected List<SObjectType> m_objectTypes;
protected fflib_ISObjectUnitOfWork m_mockUow;

/**
* Constructs a Unit Of Work factory
**/
public UnitOfWorkFactory() { }

/**
* Constructs a Unit Of Work factory
*
Expand All @@ -61,10 +56,7 @@ public virtual class fflib_Application
**/
public virtual fflib_ISObjectUnitOfWork newInstance()
{
// Mock?
if(m_mockUow!=null)
return m_mockUow;
return new fflib_SObjectUnitOfWork(m_objectTypes);
return m_mockUow!=null ? m_mockUow : new fflib_SObjectUnitOfWork(m_objectTypes);
}

/**
Expand All @@ -74,10 +66,7 @@ public virtual class fflib_Application
**/
public virtual fflib_ISObjectUnitOfWork newInstance(fflib_SObjectUnitOfWork.IDML dml)
{
// Mock?
if(m_mockUow!=null)
return m_mockUow;
return new fflib_SObjectUnitOfWork(m_objectTypes, dml);
return m_mockUow!=null ? m_mockUow : new fflib_SObjectUnitOfWork(m_objectTypes, dml);
}

/**
Expand All @@ -90,10 +79,7 @@ public virtual class fflib_Application
**/
public virtual fflib_ISObjectUnitOfWork newInstance(List<SObjectType> objectTypes)
{
// Mock?
if(m_mockUow!=null)
return m_mockUow;
return new fflib_SObjectUnitOfWork(objectTypes);
return m_mockUow!=null ? m_mockUow : new fflib_SObjectUnitOfWork(objectTypes);
}

/**
Expand All @@ -106,10 +92,7 @@ public virtual class fflib_Application
**/
public virtual fflib_ISObjectUnitOfWork newInstance(List<SObjectType> objectTypes, fflib_SObjectUnitOfWork.IDML dml)
{
// Mock?
if(m_mockUow!=null)
return m_mockUow;
return new fflib_SObjectUnitOfWork(objectTypes, dml);
return m_mockUow!=null ? m_mockUow : new fflib_SObjectUnitOfWork(objectTypes, dml);
}

@TestVisible
Expand All @@ -128,11 +111,6 @@ public virtual class fflib_Application

protected Map<Type, Object> m_serviceInterfaceTypeByMockService;

/**
* Constructs a simple Service Factory
**/
public ServiceFactory() { }

/**
* Constructs a simple Service Factory,
* using a Map of Apex Interfaces to Apex Classes implementing the interface
Expand Down Expand Up @@ -183,11 +161,6 @@ public virtual class fflib_Application
protected Map<SObjectType, Type> m_sObjectBySelectorType;
protected Map<SObjectType, fflib_ISObjectSelector> m_sObjectByMockSelector;

/**
* Constructs a simple Selector Factory
**/
public SelectorFactory() { }

/**
* Consturcts a Selector Factory linking SObjectType's with Apex Classes implement the fflib_ISObjectSelector interface
* Note that the factory does not check the given Apex Classes implement the interface
Expand Down Expand Up @@ -215,7 +188,7 @@ public virtual class fflib_Application

// Determine Apex class for Selector class
Type selectorClass = m_sObjectBySelectorType.get(sObjectType);
if(selectorClass==null)
if( selectorClass == null )
throw new DeveloperException('Selector class not found for SObjectType ' + sObjectType);

// Construct Selector class and query by Id for the records
Expand Down Expand Up @@ -288,11 +261,6 @@ public virtual class fflib_Application

protected Map<Object, fflib_IDomain> mockDomainByObject;

/**
* Constructs a Domain factory
**/
public DomainFactory() { }

/**
* Constructs a Domain factory, using an instance of the Selector Factory
* and a map of Apex classes implementing fflib_ISObjectDomain by SObjectType
Expand Down Expand Up @@ -412,16 +380,20 @@ public virtual class fflib_Application
);
}

@TestVisible
protected virtual void setMock(fflib_ISObjectDomain mockDomain)
{
mockDomainByObject.put((Object) mockDomain.sObjectType(), (fflib_IDomain) mockDomain);
}

@TestVisible
protected virtual void setMock(fflib_IDomain mockDomain)
{
mockDomainByObject.put(mockDomain.getType(), mockDomain);
if ( mockDomain != null )
{
if ( mockDomain instanceof fflib_ISObjectDomain )
{
mockDomainByObject.put(((fflib_ISObjectDomain) mockDomain).sObjectType(), (fflib_IDomain) mockDomain);
}
else
{
mockDomainByObject.put(mockDomain.getType(), mockDomain);
}
}
}

protected virtual Map<Object, Type> getConstructorTypeByObject(Map<SObjectType, Type> constructorTypeBySObjectType)
Expand Down
Loading

0 comments on commit 24b46dc

Please sign in to comment.