Exemplo n.º 1
0
 /**
  * Test checks passing 'condition' query param into constructor.
  * 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()
 {
     require 'unit-tests/config.db.php';
     if (empty($configMysql)) {
         $this->markTestSkipped("Test skipped");
         return;
     }
     // ------------- test for setters(classic) way ----------------
     $standardBuilder = new Builder();
     $standardBuilder->from('Robots')->where("year > :min: AND year < :max:", array("min" => '2013-01-01', 'max' => '2100-01-01'), array("min" => PDO::PARAM_STR, 'max' => PDO::PARAM_STR));
     $standardResult = $standardBuilder->getQuery()->execute();
     // --------------- test for single condition ------------------
     $params = array('models' => 'Robots', 'conditions' => array(array("year > :min: AND year < :max:", array("min" => '2013-01-01', 'max' => '2100-01-01'), array("min" => PDO::PARAM_STR, 'max' => PDO::PARAM_STR))));
     $builderWithSingleCondition = new Builder($params);
     $singleConditionResult = $builderWithSingleCondition->getQuery()->execute();
     // ------------- test for multiple conditions ----------------
     $params = array('models' => 'Robots', 'conditions' => array(array("year > :min:", array("min" => '2000-01-01'), array("min" => PDO::PARAM_STR)), array("year < :max:", array('max' => '2100-01-01'), array("max" => PDO::PARAM_STR))));
     // conditions are merged!
     $builderMultipleConditions = new Builder($params);
     $multipleConditionResult = $builderMultipleConditions->getQuery()->execute();
     $expectedPhql = "SELECT [Robots].* FROM [Robots] " . "WHERE year > :min: AND year < :max:";
     /* ------------ ASSERTING --------- */
     $this->assertEquals($expectedPhql, $standardBuilder->getPhql());
     $this->assertInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple", $standardResult);
     $this->assertEquals($expectedPhql, $builderWithSingleCondition->getPhql());
     $this->assertInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple", $singleConditionResult);
     $this->assertEquals($expectedPhql, $builderMultipleConditions->getPhql());
     $this->assertInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple", $multipleConditionResult);
 }
Exemplo n.º 2
0
 /**
  * Test checks passing 'condition' query param into constructor.
  * 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");
     });
 }