AnsweredAssumed Answered

Writting unittest for SugarQuery

Question asked by Shivam Tailor on Jun 6, 2019

How to write unitest for the function that using SugarQuery object?

 

<?php
function getAccounts() {
   $sugarQuery = new SugarQuery();
   $sugarQuery->from(BeanFactory::newBean('Accounts'));
   $sugarQuery->select()->addField('status_c');
   $sugarQuery->joinTable('accounts_cstm', array('joinType' => 'LEFT', 'alias' => 'acc_c'))->on()->equalsField('accounts.id', 'acc_c.id_c')->equals('accounts.deleted',0);
   $sugarQuery->where()->equals('acc_c.status_c', 'active');
   $sugarQuery->orderByRaw('acc_c.effective_date_c', 'DESC');
   $sugarQuery->limit('1');
   $result = $sugarQuery->execute();
   return $result;
}

 

I found a code by which we can mock the SugarQuery object,

$sugarQuery = $this->createMock(SugarQuery::class);
$sugarQuery->expects($this->once())
            ->method('execute')
            ->willReturn([['id' => 'id-1', 'name' => 'test name']]);

But don't know how to exactly replicate it using createMock method.

Outcomes