We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How can I implement the querybuilder test?

What I use

  • linux(ubuntu)
  • phalcon 3.4.0
  • phalcon/incubator 3.4.3
  • phpunit 7.5.0
  • mockery/mockery 1.2.0

what I want to realize

I want to implement unittest for phalcon queryBuilder.

I know how to set the return value by myself like this. $userMock->shouldReceive('columns') ->andReturn(~~~~);

but I would like to know how to automatically return the results after passing the data.

Code to be tested

class UserController extends \Base\Controlelr {
        public function points Action ()
        {
                .
                .
                .

                $columns = [
                        'User.userName',
                        'Point.userPoint',
                    ];

                    $result = \User::query()
                        ->columns($columns)
                        ->innerJoin('Points', 'Points.userId = User.id')
                        ->where('User.roleId = '10')
                        ->andWhere("User.join >= '{$startOfMonth}'")
                        ->andWhere("User.join < '{$startOfNextMonth}'")
                        ->execute();
                .
                .
                .
        }
    }

Better use this

->where('User.roleId = {roleId:int}')
->andWhere("User.join >= {startOfMonth:str}") // I don't know if you use a string, timestamp, date, etc
->andWhere("BlogUser.join < {startOfNextMonth:str}")
->execute([
    'roleId' => 10,
    'startOfMonth' => '2018-12-01 00:00:00',
    'startOfNextMonth' => '2019-01-01 00:00:00',
])

I hope that can help you. Good luck

edited Dec '18

You can write simple unit tests for builder like this:

https://github.com/phalcon/cphalcon/blob/master/tests/unit/Mvc/Model/Query/BuilderTest.php#L132

Then write integration tests with mysql to test if you have proper results. Because keep in mind it only returns PHQL, not an actual SQL.