Phalcon\Test\Unit\Mvc\Model\Query\BuilderTest::testConstructorConditions PHP Method

testConstructorConditions() public method

Conditions can now be passed as an string(as before) and as an array of 3 elements: - condition string for example "age > :age: AND created > :created:" - bind params for example array('age' => 18, 'created' => '2013-09-01') - bind types for example array('age' => PDO::PARAM_INT, 'created' => PDO::PARAM_STR) First two params are REQUIRED, bind types are optional.
    public function testConstructorConditions()
    {
        $this->specify("Query Builders don't work with conditions specified in the constructor params", function () {
            // ------------- test for setters(classic) way ----------------
            $standardBuilder = new Builder();
            $standardBuilder->from(Robots::class)->where("year > :min: AND year < :max:", ["min" => "2013-01-01", "max" => "2100-01-01"], ["min" => \PDO::PARAM_STR, "max" => \PDO::PARAM_STR]);
            $standardResult = $standardBuilder->getQuery()->execute();
            // --------------- test for single condition ------------------
            $params = ["models" => Robots::class, "conditions" => [["year > :min: AND year < :max:", ["min" => "2013-01-01", "max" => "2100-01-01"], ["min" => \PDO::PARAM_STR, "max" => \PDO::PARAM_STR]]]];
            $builderWithSingleCondition = new Builder($params);
            $singleConditionResult = $builderWithSingleCondition->getQuery()->execute();
            // ------------- test for multiple conditions ----------------
            $params = ["models" => Robots::class, "conditions" => [["year > :min:", ["min" => "2000-01-01"], ["min" => \PDO::PARAM_STR]], ["year < :max:", ["max" => "2100-01-01"], ["max" => \PDO::PARAM_STR]]]];
            // conditions are merged!
            $builderMultipleConditions = new Builder($params);
            $multipleConditionResult = $builderMultipleConditions->getQuery()->execute();
            $expectedPhql = "SELECT [" . Robots::class . "].* FROM [" . Robots::class . "] WHERE year > :min: AND year < :max:";
            /* ------------ ASSERTING --------- */
            expect($standardBuilder->getPhql())->equals($expectedPhql);
            expect($standardResult)->isInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple");
            expect($builderWithSingleCondition->getPhql())->equals($expectedPhql);
            expect($singleConditionResult)->isInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple");
            expect($builderMultipleConditions->getPhql())->equals($expectedPhql);
            expect($multipleConditionResult)->isInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple");
        });
    }