/**
 * Proxy class for AggregateResult that allows summary data to be mocked in unit tests
 * Derived from Adrian Larson's answer here: https://salesforce.stackexchange.com/questions/173193/is-there-a-way-i-can-mock-an-aggregateresult
 * 
 * Usage: 
 * To create data for the following query 
 * [SELECT AccountId, COUNT(Id) opportunities FROM Opportunity GROUP BY AccountId]
 * 
 * List<Aggregate> mockData = new Aggregate[]{
 *      new Aggregate()
 *          .put('AccountId', fakeAccountId1)
 *          .put('opportunties', 10),
 *      new Aggregate()
 *          .put('AccountId', fakeAccountId2)
 *          .put('opportunties', 15),
 * }
 */
public class Aggregate
{
    public static Aggregate[] convertResults( AggregateResult[] lstResults )
    {
        Aggregate[] lstRecords = new Aggregate[]{};
        for( AggregateResult result : lstResults )
            lstRecords.add( new Aggregate( result ) );
        return lstRecords;
    }

    private final Map<String, Object> data;

    public Aggregate( AggregateResult result )
    {
        data = result.getPopulatedFieldsAsMap();
    }

    @TestVisible
    private Aggregate()
    {
        data = new Map<String, Object>();
    }

    public Object get( String alias )
    {
        return data.get( alias );
    }

    @TestVisible
    private Aggregate put( String alias, Object value )
    {
        data.put( alias, value );
        return this;
    }
}