Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

criteria: fix issue #2157 and added tests #2220

Merged
merged 2 commits into from
Mar 31, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
criteria: added tests for left and right joins
  • Loading branch information
maxgalbu committed Mar 28, 2014
commit 2c635e31823b12ee1b0713fbfbbac9fe187acf83
64 changes: 60 additions & 4 deletions unit-tests/ModelsCriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function testModelsMysql()
$this->_executeTestsRenamed($di);
$this->_executeTestsFromInput($di);
$this->_executeTestIssues2131($di);
$this->_executeJoinTests($di, "mysql");
}

public function testModelsPostgresql()
Expand All @@ -96,6 +97,7 @@ public function testModelsPostgresql()
$this->_executeTestsRenamed($di);
$this->_executeTestsFromInput($di);
$this->_executeTestIssues2131($di);
$this->_executeJoinTests($di, "postgresql");
}

public function testModelsSQLite()
Expand All @@ -117,11 +119,12 @@ public function testModelsSQLite()
$this->_executeTestsRenamed($di);
$this->_executeTestsFromInput($di);
$this->_executeTestIssues2131($di);
$this->_executeJoinTests($di, "sqlite");
}

protected function _executeTestsNormal($di)
{

//Where
$personas = Personas::query()->where("estado='I'")->execute();
$people = People::find("estado='I'");
$this->assertEquals(count($personas), count($people));
Expand All @@ -144,7 +147,7 @@ protected function _executeTestsNormal($di)
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);

//Order + limit
//Where + Order + limit
$personas = Personas::query()
->where("estado='A'")
->orderBy("nombres")
Expand All @@ -161,7 +164,7 @@ protected function _executeTestsNormal($di)
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);

//Bind params + Limit
//Where with bind params + order + Limit
$personas = Personas::query()
->where("estado=?1")
->bind(array(1 => "A"))
Expand All @@ -181,7 +184,7 @@ protected function _executeTestsNormal($di)
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);

//Limit + Offset
//Where with bind params + order + limit + Offset
$personas = Personas::query()
->where("estado=?1")
->bind(array(1 => "A"))
Expand All @@ -201,6 +204,7 @@ protected function _executeTestsNormal($di)
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);

//Where with named bind params + order + limit
$personas = Personas::query()
->where("estado=:estado:")
->bind(array("estado" => "A"))
Expand All @@ -219,7 +223,59 @@ protected function _executeTestsNormal($di)
$somePersona = $personas->getFirst();
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);
}

protected function _executeJoinTests($di, $dbtype)
{
//Left join with Simple resultset
$robotparts = RobotsParts::query()
->columns("Robots.id, Robots.name, RobotsParts.id robotpart_id")
->leftJoin("Robots", "Robots.id = RobotsParts.robots_id")
->execute();
$this->assertTrue(is_object($robotparts));
$this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $robotparts);
$this->assertNotNull($robotparts->getFirst()->id);
$this->assertNotNull($robotparts->getFirst()->name);
$this->assertNotNull($robotparts->getFirst()->robotpart_id);

//Two left joins with Simple resultset
$robotparts = RobotsParts::query()
->columns("RobotsParts.id, r.id robot_id, p.id part_id")
->leftJoin("Robots", "r.id = RobotsParts.robots_id", "r")
->leftJoin("Parts", "p.id = RobotsParts.parts_id", "p")
->execute();
$this->assertTrue(is_object($robotparts));
$this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $robotparts);
$this->assertNotNull($robotparts->getFirst()->id);
$this->assertNotNull($robotparts->getFirst()->robot_id);
$this->assertNotNull($robotparts->getFirst()->part_id);

//Right join not supported in sqlite
if ($dbtype != "sqlite")
{
//Right join with Simple resultset
$robotparts = RobotsParts::query()
->columns("Robots.id, Robots.name, RobotsParts.id robotpart_id")
->rightJoin("Robots", "Robots.id = RobotsParts.robots_id")
->execute();
$this->assertTrue(is_object($robotparts));
$this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $robotparts);
$this->assertNotNull($robotparts->getFirst()->id);
$this->assertNotNull($robotparts->getFirst()->name);
$this->assertNotNull($robotparts->getFirst()->robotpart_id);

//Two right joins with Simple resultset
$robotparts = RobotsParts::query()
->columns("RobotsParts.id, r.id robot_id, p.id part_id")
->rightJoin("Robots", "r.id = RobotsParts.robots_id", "r")
->rightJoin("Parts", "p.id = RobotsParts.parts_id", "p")
->execute();
$this->assertTrue(is_object($robotparts));
$this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $robotparts);
$this->assertNotNull($robotparts->getFirst()->id);
$this->assertNotNull($robotparts->getFirst()->robot_id);
$this->assertNotNull($robotparts->getFirst()->part_id);
}
}

protected function _executeTestsRenamed($di)
Expand Down