mirror of
https://github.com/espocrm/espocrm.git
synced 2026-07-01 08:26:04 +00:00
@@ -52,6 +52,7 @@ use Espo\ORM\Query\Part\Condition;
|
||||
use Espo\ORM\Query\Select;
|
||||
use Espo\ORM\Query\Update;
|
||||
|
||||
use LogicException;
|
||||
use RuntimeException;
|
||||
|
||||
require_once 'tests/unit/testData/DB/Entities.php';
|
||||
@@ -986,6 +987,89 @@ class MysqlQueryComposerTest extends \PHPUnit\Framework\TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testJoinSubQuery1(): void
|
||||
{
|
||||
$sql =
|
||||
"SELECT post.id AS `id` FROM `post` " .
|
||||
"JOIN (SELECT post.id AS `id` FROM `post` WHERE post.deleted = 0) AS `a` ON a.id = post.id " .
|
||||
"WHERE post.deleted = 0";
|
||||
|
||||
$select = SelectBuilder::create()
|
||||
->select('id')
|
||||
->from('Post')
|
||||
->join(
|
||||
Join
|
||||
::createWithSubQuery(
|
||||
SelectBuilder::create()
|
||||
->select('id')
|
||||
->from('Post')
|
||||
->build(),
|
||||
'a'
|
||||
)
|
||||
->withConditions(
|
||||
WhereClause::create(
|
||||
Condition::equal(
|
||||
Expression::column('a.id'),
|
||||
Expression::column('post.id')
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
->build();
|
||||
|
||||
$this->assertEquals(
|
||||
$sql,
|
||||
$this->query->composeSelect($select)
|
||||
);
|
||||
}
|
||||
|
||||
public function testJoinSubQuery2(): void
|
||||
{
|
||||
$sql =
|
||||
"SELECT post.id AS `id` FROM `post` " .
|
||||
"JOIN (SELECT post.id AS `id` FROM `post` WHERE post.deleted = 0) AS `a` ON a.id = post.id " .
|
||||
"WHERE post.deleted = 0";
|
||||
|
||||
$select = SelectBuilder::create()
|
||||
->select('id')
|
||||
->from('Post')
|
||||
->join(
|
||||
SelectBuilder::create()
|
||||
->select('id')
|
||||
->from('Post')
|
||||
->build(),
|
||||
'a',
|
||||
Condition::equal(
|
||||
Expression::column('a.id'),
|
||||
Expression::column('post.id')
|
||||
)
|
||||
)
|
||||
->build();
|
||||
|
||||
$this->assertEquals(
|
||||
$sql,
|
||||
$this->query->composeSelect($select)
|
||||
);
|
||||
}
|
||||
|
||||
public function testJoinSubQueryException1(): void
|
||||
{
|
||||
$this->expectException(LogicException::class);
|
||||
|
||||
$select = SelectBuilder::create()
|
||||
->select('id')
|
||||
->from('Post')
|
||||
->join(
|
||||
SelectBuilder::create()
|
||||
->select('id')
|
||||
->from('Post')
|
||||
->build(),
|
||||
)
|
||||
->build();
|
||||
|
||||
$this->query->composeSelect($select);
|
||||
}
|
||||
|
||||
public function testWhereNotValue1()
|
||||
{
|
||||
$sql = $this->query->compose(Select::fromRaw([
|
||||
|
||||
@@ -29,8 +29,10 @@
|
||||
|
||||
namespace tests\unit\Espo\ORM\Query\Part;
|
||||
|
||||
use Espo\ORM\Query\Part\Expression;
|
||||
use Espo\ORM\Query\Part\Join;
|
||||
use Espo\ORM\Query\Part\Expression as Expr;
|
||||
use Espo\ORM\Query\SelectBuilder;
|
||||
|
||||
class JoinTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
@@ -50,6 +52,8 @@ class JoinTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertTrue($join->isRelation());
|
||||
$this->assertFalse($join->isTable());
|
||||
|
||||
$this->assertEquals(Join::TYPE_RELATION, $join->getType());
|
||||
}
|
||||
|
||||
public function testCreate2(): void
|
||||
@@ -68,6 +72,8 @@ class JoinTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertTrue($join->isTable());
|
||||
$this->assertFalse($join->isRelation());
|
||||
|
||||
$this->assertEquals(Join::TYPE_TABLE, $join->getType());
|
||||
}
|
||||
|
||||
public function testCreate3(): void
|
||||
@@ -77,4 +83,18 @@ class JoinTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals('Test', $join->getTarget());
|
||||
$this->assertEquals('testAlias', $join->getAlias());
|
||||
}
|
||||
|
||||
public function testCreate4(): void
|
||||
{
|
||||
$join = Join::createWithSubQuery(
|
||||
SelectBuilder::create()
|
||||
->select(Expression::value(true))
|
||||
->build()
|
||||
,
|
||||
'a'
|
||||
);
|
||||
|
||||
$this->assertTrue($join->isSubQuery());
|
||||
$this->assertEquals(Join::TYPE_SUB_QUERY, $join->getType());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user