snapshot of https://github.com/espocrm/espocrm.git (master, 2026-09-18; blobs > 131072B omitted)
Some checks failed
Test / Test on PHP 8.3 (push) Failing after 5s
Test / Test on PHP 8.4 (push) Failing after 4s
Test / Test on PHP 8.5 (push) Failing after 3s
TypeScript static analysis / TypeScript check (push) Failing after 14s
Test Integration on PostgreSQL / Test on PHP 8.4 fix (push) Failing after 1s
Test Integration on PostgreSQL / Test on PHP 8.4 master (push) Failing after 1s
Test Integration on MySQL / Test on PHP 8.3 (push) Failing after 2s
Test Integration on MySQL / Test on PHP 8.5 (push) Failing after 0s

This commit is contained in:
upstream-mirror
2026-09-18 04:19:15 +05:00
commit 7cc7e6b4f3
10077 changed files with 900756 additions and 0 deletions

View File

@@ -0,0 +1,450 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\ORM\Query\Part;
use Espo\ORM\Query\Part\Expression as Expr;
use RuntimeException;
class ExpressionTest extends \PHPUnit\Framework\TestCase
{
protected function setUp(): void
{
}
public function testAlias1(): void
{
$actual = Expr::alias('test')->getValue();
$expected = '#test';
$this->assertEquals($expected, $actual);
}
public function testAlias2(): void
{
$actual = Expr::alias('test.someAlias')->getValue();
$expected = 'test.#someAlias';
$this->assertEquals($expected, $actual);
}
public function testColumn1(): void
{
$actual = Expr::column('test')->getValue();
$expected = 'test';
$this->assertEquals($expected, $actual);
}
public function testColumn2(): void
{
$actual = Expr::column('alias.test')->getValue();
$expected = 'alias.test';
$this->assertEquals($expected, $actual);
}
public function testColumn3(): void
{
$actual = Expr::column('@alias1.test')->getValue();
$expected = '@alias1.test';
$this->assertEquals($expected, $actual);
}
public function testColumn4(): void
{
$this->expectException(RuntimeException::class);
Expr::column('@alias@test');
}
public function testColumn5(): void
{
$this->expectException(RuntimeException::class);
Expr::column('^test');
}
public function testValue1(): void
{
$actual = Expr::value('"test"')->getValue();
$expected = "'" . '"test"' . "'";
$this->assertEquals($expected, $actual);
}
public function testValue2(): void
{
$actual = Expr::value("'test'")->getValue();
$expected = "'" . "\\'test\\'" . "'";
$this->assertEquals($expected, $actual);
}
public function testValue3(): void
{
$actual = Expr::value(10)->getValue();
$expected = '10';
$this->assertEquals($expected, $actual);
}
public function testValue4(): void
{
$actual = Expr::value(10.5)->getValue();
$expected = '10.5';
$this->assertEquals($expected, $actual);
}
public function testValue6(): void
{
$actual = Expr::value(true)->getValue();
$expected = 'TRUE';
$this->assertEquals($expected, $actual);
}
public function testValue7(): void
{
$actual = Expr::value(false)->getValue();
$expected = 'FALSE';
$this->assertEquals($expected, $actual);
}
public function testValue8(): void
{
$actual = Expr::value(null)->getValue();
$expected = 'NULL';
$this->assertEquals($expected, $actual);
}
public function testFuncIf1(): void
{
$actual = Expr::if(
Expr::column('test'),
'1',
2
)->getValue();
$expected = "IF:(test, '1', 2)";
$this->assertEquals($expected, $actual);
}
public function testFuncIf2(): void
{
$actual = Expr::if(
Expr::column('test'),
Expr::column('hello.man'),
true
)->getValue();
$expected = "IF:(test, hello.man, TRUE)";
$this->assertEquals($expected, $actual);
}
public function testFuncLike(): void
{
$actual = Expr::like(
Expr::column('test'),
'test%'
)->getValue();
$expected = "LIKE:(test, 'test%')";
$this->assertEquals($expected, $actual);
}
public function testFuncEqual(): void
{
$actual = Expr::equal(
Expr::column('test'),
1
)->getValue();
$expected = "EQUAL:(test, 1)";
$this->assertEquals($expected, $actual);
}
public function testFuncNotEqual(): void
{
$actual = Expr::notEqual(
Expr::column('test'),
1
)->getValue();
$expected = "NOT_EQUAL:(test, 1)";
$this->assertEquals($expected, $actual);
}
public function testIn(): void
{
$actual = Expr::in(
Expr::column('test'),
[
Expr::value(1),
2
]
)->getValue();
$expected = "IN:(test, 1, 2)";
$this->assertEquals($expected, $actual);
}
public function testCoalesce(): void
{
$actual = Expr::coalesce(
Expr::column('test1'),
Expr::column('test2')
)->getValue();
$expected = "COALESCE:(test1, test2)";
$this->assertEquals($expected, $actual);
}
public function testIfNull(): void
{
$actual = Expr::ifNull(
Expr::column('test1'),
''
)->getValue();
$expected = "IFNULL:(test1, '')";
$this->assertEquals($expected, $actual);
}
public function testMonth(): void
{
$actual = Expr::month(
Expr::column('test')
)->getValue();
$expected = "MONTH_NUMBER:(test)";
$this->assertEquals($expected, $actual);
}
public function testWeek0(): void
{
$actual = Expr::week(
Expr::column('test')
)->getValue();
$expected = "WEEK_NUMBER:(test)";
$this->assertEquals($expected, $actual);
}
public function testWeek1(): void
{
$actual = Expr::week(
Expr::column('test'),
1
)->getValue();
$expected = "WEEK_NUMBER_1:(test)";
$this->assertEquals($expected, $actual);
}
public function testDayOfWeek(): void
{
$actual = Expr::dayOfWeek(
Expr::column('test')
)->getValue();
$expected = "DAYOFWEEK:(test)";
$this->assertEquals($expected, $actual);
}
public function testDayOfMonth(): void
{
$actual = Expr::dayOfMonth(
Expr::column('test')
)->getValue();
$expected = "DAYOFMONTH:(test)";
$this->assertEquals($expected, $actual);
}
public function testYear(): void
{
$actual = Expr::year(
Expr::column('test')
)->getValue();
$expected = "YEAR:(test)";
$this->assertEquals($expected, $actual);
}
public function testYearFiscsal(): void
{
$actual = Expr::yearFiscal(
Expr::column('test'),
10
)->getValue();
$expected = "YEAR_10:(test)";
$this->assertEquals($expected, $actual);
}
public function testNow(): void
{
$actual = Expr::now(
)->getValue();
$expected = "NOW:()";
$this->assertEquals($expected, $actual);
}
public function testConvertTimezone(): void
{
$actual = Expr::convertTimezone(
Expr::column('test'),
-10.5
)->getValue();
$expected = "TZ:(test, -10.5)";
$this->assertEquals($expected, $actual);
}
public function testConcat(): void
{
$actual = Expr::concat(
Expr::column('test'),
' ',
'test'
)->getValue();
$expected = "CONCAT:(test, ' ', 'test')";
$this->assertEquals($expected, $actual);
}
public function testReplace(): void
{
$actual = Expr::replace(
Expr::column('test'),
'test',
'hello'
)->getValue();
$expected = "REPLACE:(test, 'test', 'hello')";
$this->assertEquals($expected, $actual);
}
public function testAdd(): void
{
$actual = Expr::add(
Expr::column('test'),
1
)->getValue();
$expected = "ADD:(test, 1)";
$this->assertEquals($expected, $actual);
}
public function testRound(): void
{
$actual = Expr::round(
Expr::column('test'),
1
)->getValue();
$expected = "ROUND:(test, 1)";
$this->assertEquals($expected, $actual);
}
public function testAnd(): void
{
$actual = Expr::and(
Expr::column('test1'),
Expr::column('test2')
)->getValue();
$expected = "AND:(test1, test2)";
$this->assertEquals($expected, $actual);
}
public function testOr(): void
{
$actual = Expr::or(
Expr::column('test1'),
Expr::column('test2')
)->getValue();
$expected = "OR:(test1, test2)";
$this->assertEquals($expected, $actual);
}
public function testNot(): void
{
$actual = Expr::not(
Expr::column('test')
)->getValue();
$expected = "NOT:(test)";
$this->assertEquals($expected, $actual);
}
}

View File

@@ -0,0 +1,100 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
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
{
public function testCreate1(): void
{
$conditions = Expr::isNull(
Expr::column('test')
);
$join = Join::createWithRelationTarget('test')
->withAlias('testAlias')
->withConditions($conditions);
$this->assertEquals('test', $join->getTarget());
$this->assertEquals('testAlias', $join->getAlias());
$this->assertEquals($conditions, $join->getConditions());
$this->assertTrue($join->isRelation());
$this->assertFalse($join->isTable());
$this->assertEquals(Join::MODE_RELATION, $join->getMode());
}
public function testCreate2(): void
{
$conditions = Expr::isNull(
Expr::column('test')
);
$join = Join::createWithTableTarget('Test')
->withAlias('testAlias')
->withConditions($conditions);
$this->assertEquals('Test', $join->getTarget());
$this->assertEquals('testAlias', $join->getAlias());
$this->assertEquals($conditions, $join->getConditions());
$this->assertTrue($join->isTable());
$this->assertFalse($join->isRelation());
$this->assertEquals(Join::MODE_TABLE, $join->getMode());
}
public function testCreate3(): void
{
$join = Join::create('Test', 'testAlias');
$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::MODE_SUB_QUERY, $join->getMode());
}
}

View File

@@ -0,0 +1,78 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\ORM\Query\Part;
use Espo\ORM\Query\Part\Order as OrderExpr;
use Espo\ORM\Query\Part\Expression as Expr;
class OrderExpressionTest extends \PHPUnit\Framework\TestCase
{
public function testCreate1(): void
{
$order = OrderExpr::fromString('test')->withDesc();
$this->assertEquals(Expr::create('test'), $order->getExpression());
$this->assertEquals(OrderExpr::DESC, $order->getDirection());
$this->assertEquals(true, $order->isDesc());
}
public function testCreate2(): void
{
$order = OrderExpr::fromString('test');
$this->assertEquals(OrderExpr::ASC, $order->getDirection());
$this->assertEquals(false, $order->isDesc());
}
public function testCreate3(): void
{
$order = OrderExpr::fromString('test')->withAsc();
$this->assertEquals(Expr::create('test'), $order->getExpression());
$this->assertEquals(OrderExpr::ASC, $order->getDirection());
$this->assertEquals(false, $order->isDesc());
}
public function testCreate4(): void
{
$order = OrderExpr::fromString('test')->withDirection(OrderExpr::DESC);
$this->assertEquals(OrderExpr::DESC, $order->getDirection());
}
public function testReverseOrder(): void
{
$order = OrderExpr::fromString('test')
->withDirection(OrderExpr::DESC)
->withReverseDirection();
$this->assertEquals(OrderExpr::ASC, $order->getDirection());
}
}

View File

@@ -0,0 +1,489 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\ORM\Query\Part;
use Espo\ORM\{
Query\Part\Where\AndGroup,
Query\Part\Where\OrGroup,
Query\Part\WhereClause,
Query\Part\Where\Comparison as Comp,
Query\Part\Expression as Expr,
Query\Part\Condition as Cond,
Query\SelectBuilder,
};
class WhereTest extends \PHPUnit\Framework\TestCase
{
protected function setUp(): void
{
}
public function testAndGroup1(): void
{
$raw =
AndGroup::createBuilder()
->add(
WhereClause::fromRaw([
'test1' => '1',
])
)
->build()
->getRaw();
$expextedRaw = [
'AND' => [
'test1' => '1',
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testAndGroup2(): void
{
$raw =
AndGroup::createBuilder()
->add(
WhereClause::fromRaw([
'test1' => '1',
'test1a' => '1a',
])
)
->add(
WhereClause::fromRaw([
'test2' => '2',
])
)
->build()
->getRaw();
$expextedRaw = [
'AND' => [
['test1' => '1', 'test1a' => '1a'],
['test2' => '2'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testAndGroup3(): void
{
$raw =
AndGroup::createBuilder()
->add(
WhereClause::fromRaw([
'test1' => '1',
])
)
->add(
OrGroup::createBuilder()
->add(
WhereClause::fromRaw([
'o1' => '1',
])
)
->add(
WhereClause::fromRaw([
'o2' => '2',
])
)
->build()
)
->build()
->getRaw();
$expextedRaw = [
'AND' => [
['test1' => '1'],
[
'OR' => [
['o1' => '1'],
['o2' => '2'],
],
],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testOrGroup1(): void
{
$raw =
OrGroup::createBuilder()
->add(
WhereClause::fromRaw([
'test1' => '1',
])
)
->build()
->getRaw();
$expextedRaw = [
'OR' => [
['test1' => '1'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testOrGroup2(): void
{
$raw =
OrGroup::createBuilder()
->add(
WhereClause::fromRaw([
'test1' => '1',
'test2' => '2',
])
)
->build()
->getRaw();
$expextedRaw = [
'OR' => [
[
'test1' => '1',
'test2' => '2',
],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testAndGroupMerge1(): void
{
$group1 =
AndGroup::createBuilder()
->add(
WhereClause::fromRaw([
'test1' => '1',
])
)
->build();
$group =
AndGroup::createBuilder()
->merge($group1)
->add(
WhereClause::fromRaw([
'test2' => '2',
])
)
->build();
$raw = $group->getRaw();
$expextedRaw = [
'AND' => [
['test1' => '1'],
['test2' => '2'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testOrGroupMerge1(): void
{
$orGroup1 =
OrGroup::createBuilder()
->add(
WhereClause::fromRaw([
'test1' => '1',
])
)
->build();
$orGroup =
OrGroup::createBuilder()
->merge($orGroup1)
->add(
WhereClause::fromRaw([
'test2' => '2',
])
)
->build();
$raw = $orGroup->getRaw();
$expextedRaw = [
'OR' => [
['test1' => '1'],
['test2' => '2'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testOrGroupMerge2(): void
{
$orGroup1 =
OrGroup::createBuilder()
->add(
WhereClause::fromRaw([
'test1' => '1',
])
)
->build();
$orGroup =
OrGroup::createBuilder()
->add(
WhereClause::fromRaw([
'test2' => '2',
])
)
->merge($orGroup1)
->build();
$raw = $orGroup->getRaw();
$expextedRaw = [
'OR' => [
['test2' => '2'],
['test1' => '1'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testAndGroupCreate1(): void
{
$raw = AndGroup
::create(
WhereClause::fromRaw(['test1' => '1']),
WhereClause::fromRaw(['test2' => '2'])
)
->getRaw();
$expextedRaw = [
'AND' => [
['test1' => '1'],
['test2' => '2'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testOrGroupCreate1(): void
{
$raw = OrGroup
::create(
WhereClause::fromRaw(['test1' => '1']),
WhereClause::fromRaw(['test2' => '2'])
)
->getRaw();
$expextedRaw = [
'OR' => [
['test1' => '1'],
['test2' => '2'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testAndGroupCreateWithCmp(): void
{
$raw = AndGroup
::create(
Comp::equal(Expr::column('test1'), '1'),
Comp::equal(Expr::column('test2'), '2')
)
->getRaw();
$expextedRaw = [
'AND' => [
['test1=' => '1'],
['test2=' => '2'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testAll(): void
{
$raw =
Cond::and(
Cond::equal(Cond::column('test1'), '1'),
Cond::notEqual(Expr::column('test2'), '2'),
Cond::greater(Expr::column('test3'), 3),
Expr::greater(Expr::column('test4'), 4),
Cond::equal(Expr::column('test5'), Expr::column('hello5')),
Comp::equal(Expr::column('test6'), Expr::column('hello6')),
Cond::equal(Expr::column('test9'), Expr::column('hello9')),
Comp::greaterOrEqual(Expr::column('test10'), Expr::column('hello10'))
)
->getRaw();
$expextedRaw = [
'AND' => [
['test1=' => '1'],
['test2!=' => '2'],
['test3>' => 3],
['GREATER_THAN:(test4, 4):' => null],
['test5=:' => 'hello5'],
['test6=:' => 'hello6'],
['test9=:' => 'hello9'],
['test10>=:' => 'hello10'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testAny(): void
{
$raw =
Cond::or(
Cond::equal(Expr::column('test1'), '1'),
Cond::equal(Expr::column('test2'), '2')
)
->getRaw();
$expextedRaw = [
'OR' => [
['test1=' => '1'],
['test2=' => '2'],
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testNot1(): void
{
$raw =
Cond::not(
Cond::or(
Cond::equal(Expr::column('test1'), '1'),
Cond::equal(Expr::column('test2'), '2')
)
)
->getRaw();
$expextedRaw = [
'NOT' => [
'OR' => [
['test1=' => '1'],
['test2=' => '2'],
]
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testNot2(): void
{
$raw =
Cond::not(
Cond::equal(Expr::column('test1'), '1')
)
->getRaw();
$expextedRaw = [
'NOT' => [
'test1=' => '1',
]
];
$this->assertEquals($expextedRaw, $raw);
}
public function testInQuery(): void
{
$query = (new SelectBuilder)->from('Test')->build();
$raw =
Cond::in(Expr::column('id'), $query)
->getRaw();
$expectedRaw = [
'id=s' => $query
];
$this->assertEquals($expectedRaw, $raw);
}
public function testNotInQuery(): void
{
$query = (new SelectBuilder)->from('Test')->build();
$raw =
Cond::notIn(Expr::column('id'), $query)
->getRaw();
$expectedRaw = [
'id!=s' => $query
];
$this->assertEquals($expectedRaw, $raw);
}
public function testInList(): void
{
$raw =
Cond::in(Expr::column('status'), ['1', '2'])
->getRaw();
$expectedRaw = [
'status=' => ['1', '2']
];
$this->assertEquals($expectedRaw, $raw);
}
public function testNotInList(): void
{
$raw =
Cond::notIn(Expr::column('status'), ['1', '2'])
->getRaw();
$expectedRaw = [
'status!=' => ['1', '2']
];
$this->assertEquals($expectedRaw, $raw);
}
}

View File

@@ -0,0 +1,698 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\ORM\Query;
use PHPUnit\Framework\TestCase;
use Espo\ORM\Query\Part\Condition as Cond;
use Espo\ORM\Query\Part\Expression as Expr;
use Espo\ORM\Query\Part\Join;
use Espo\ORM\Query\Part\Order;
use Espo\ORM\Query\Part\Selection;
use Espo\ORM\Query\Part\WhereClause;
use Espo\ORM\Query\SelectBuilder;
use RuntimeException;
class SelectBuilderTest extends TestCase
{
/**
* @var SelectBuilder
*/
private $builder;
protected function setUp(): void
{
$this->builder = new SelectBuilder();
}
public function testFrom()
{
$params = $this->builder
->from('Test')
->build()
->getRaw();
$this->assertEquals('Test', $params['from']);
}
public function testSelect1()
{
$select = $this->builder
->from('Test')
->select(['id', 'name'])
->select('test')
->build();
$this->assertEquals(
[
Selection::fromString('id'),
Selection::fromString('name'),
Selection::fromString('test'),
],
$select->getSelect()
);
}
public function testSelect2()
{
$select = $this->builder
->from('Test')
->select('test')
->select(['id', 'name'])
->build();
$this->assertEquals(
[
Selection::fromString('id'),
Selection::fromString('name'),
],
$select->getSelect()
);
}
public function testSelect3()
{
$select = $this->builder
->from('Test')
->select('test', 'hello')
->build();
$this->assertEquals(
[
Selection::fromString('test')->withAlias('hello'),
],
$select->getSelect()
);
}
public function testSelect4()
{
$select = $this->builder
->from('Test')
->select(Expr::create('test'), 'hello')
->build();
$this->assertEquals(
[
Selection::fromString('test')->withAlias('hello'),
],
$select->getSelect()
);
}
public function testSelect5()
{
$select = $this->builder
->from('Test')
->select('test')
->select([Expr::create('id'), Expr::create('name')])
->build();
$this->assertEquals(
[
Selection::fromString('id'),
Selection::fromString('name'),
],
$select->getSelect()
);
}
public function testSelect6()
{
$select = $this->builder
->from('Test')
->select('test')
->select([
[Expr::create('id'), 'id'],
[Expr::create('name'), 'name'],
])
->build();
$this->assertEquals(
[
Selection::fromString('id')->withAlias('id'),
Selection::fromString('name')->withAlias('name'),
],
$select->getSelect()
);
}
public function testSelect7()
{
$select = $this->builder
->from('Test')
->select([
'id',
Selection::create(Expr::create('name'))
])
->select(Selection::fromString('test')->withAlias('testAlias'))
->build();
$this->assertEquals(
[
Selection::fromString('id'),
Selection::fromString('name'),
Selection::fromString('test')->withAlias('testAlias'),
],
$select->getSelect()
);
}
public function testCloneNotSame()
{
$builder = new SelectBuilder();
$select = $builder
->from('Test')
->build();
$builder = new SelectBuilder();
$selectCloned = $builder
->clone($select)
->build();
$this->assertNotSame($selectCloned, $select);
}
public function testWhereNull1()
{
$select = $this->builder
->from('Test')
->where(['test' => null])
->build();
$raw = $select->getRaw();
$this->assertEquals(['test' => null], $raw['whereClause']);
}
public function testWhereNull2()
{
$select = $this->builder
->from('Test')
->where('test', null)
->build();
$raw = $select->getRaw();
$this->assertEquals(['test' => null], $raw['whereClause']);
}
public function testGroupBy1()
{
$select = $this->builder
->from('Test')
->having(['test' => null])
->group(['test'])
->build();
$raw = $select->getRaw();
$this->assertEquals(['test' => null], $raw['havingClause']);
$this->assertEquals(['test'], $raw['groupBy']);
}
public function testGroupBy2()
{
$select = $this->builder
->from('Test')
->having(Cond::equal(Expr::column('test'), null))
->group(Expr::create('test'))
->build();
$raw = $select->getRaw();
$this->assertEquals(['test'], $raw['groupBy']);
}
public function testGroupBy3()
{
$select = $this->builder
->from('Test')
->group([
Expr::create('test1'),
Expr::create('test2'),
])
->build();
$this->assertEquals(
[
Expr::create('test1'),
Expr::create('test2'),
],
$select->getGroup()
);
}
public function testHaving1()
{
$select = $this->builder
->from('Test')
->having(Cond::equal(Expr::column('test'), null))
->group(Expr::create('test'))
->build();
$this->assertEquals(
Cond::equal(Expr::column('test'), null)->getRaw(),
$select->getHaving()->getRaw()
);
}
public function testOrder1()
{
$select = $this->builder
->from('Test')
->order(Expr::create('test'))
->build();
$raw = $select->getRaw();
$this->assertEquals([['test', 'ASC']], $raw['orderBy']);
}
public function testOrder2()
{
$select = $this->builder
->from('Test')
->order(Expr::create('test'))
->build();
$raw = $select->getRaw();
$this->assertEquals([['test', 'ASC']], $raw['orderBy']);
}
public function testOrder3()
{
$select = $this->builder
->from('Test')
->order([Expr::create('test')], 'DESC')
->build();
$raw = $select->getRaw();
$this->assertEquals([['test', 'DESC']], $raw['orderBy']);
}
public function testOrder4()
{
$select = $this->builder
->from('Test')
->order([[Expr::create('test'), 'DESC']], 'ASC')
->build();
$raw = $select->getRaw();
$this->assertEquals([['test', 'DESC']], $raw['orderBy']);
}
public function testOrder5()
{
$select = $this->builder
->from('Test')
->order(Order::fromString('test')->withDesc())
->order('hello', true)
->build();
$this->assertEquals(
[
Order::fromString('test')->withDesc(),
Order::fromString('hello')->withDesc(),
],
$select->getOrder()
);
}
public function testOrder6()
{
$select = $this->builder
->from('Test')
->order([
Order::fromString('test')->withDesc(),
['hello', true],
])
->build();
$this->assertEquals(
[
Order::fromString('test')->withDesc(),
Order::fromString('hello')->withDesc(),
],
$select->getOrder()
);
}
public function testClone()
{
$builder = new SelectBuilder();
$select = $builder
->from('Test')
->where('test1', '1')
->build();
$builder = new SelectBuilder();
$selectCloned = $builder
->clone($select)
->distinct()
->where('test2', '2')
->build();
$params = $select->getRaw();
$paramsCloned = $selectCloned->getRaw();
$this->assertTrue($paramsCloned['distinct']);
$this->assertFalse($params['distinct'] ?? false);
$this->assertEquals(['test1' =>'1'], $params['whereClause']);
$this->assertEquals(['test1' => '1', 'test2' => '2'], $paramsCloned['whereClause']);
}
public function testCloneException()
{
$builder = new SelectBuilder();
$select = $builder
->from('Test')
->where('test1', '1')
->build();
$builder = new SelectBuilder();
$this->expectException(RuntimeException::class);
$builder
->from('Test')
->clone($select);
}
public function testWhereSameKeys1()
{
$builder = new SelectBuilder();
$select = $builder
->from('Test')
->where(['test' => '1'])
->where(['test' => '2'])
->build();
$raw = $select->getRaw();
$expected = [
'test' => '1',
['test' => '2'],
];
$this->assertEquals($expected, $raw['whereClause']);
}
public function testWhereSameKeys2()
{
$builder = new SelectBuilder();
$select = $builder
->from('Test')
->where([
'OR' => [
'test' => '1'
],
])
->where([
'OR' => [
'test' => '2'
],
])
->build();
$raw = $select->getRaw();
$expected = [
'OR' => [
'test' => '1'
],
[
'OR' => [
'test' => '2'
],
]
];
$this->assertEquals($expected, $raw['whereClause']);
}
public function testLeftJoin1()
{
$params = $this->builder
->from('Test')
->leftJoin('link1')
->leftJoin('link1')
->leftJoin('link2')
->build();
$this->assertEquals(
[
Join::create('link1')
->withLeft(),
Join::create('link2')
->withLeft()
],
$params->getJoins()
);
}
public function testLeftJoin2()
{
$query = $this->builder
->from('Test')
->leftJoin('link1', 'alias1', ['name' => 'test'])
->leftJoin('link2', 'alias2')
->build();
$this->assertEquals(
[
Join::create('link1', 'alias1')
->withConditions(WhereClause::fromRaw(['name' => 'test']))
->withLeft(),
Join::create('link2', 'alias2')
->withLeft(),
],
$query->getJoins()
);
}
public function testLeftJoin3()
{
$query = $this->builder
->from('Test')
->leftJoin(
Join::create('link1', 'alias1')
->withConditions(
WhereClause::fromRaw(['name' => 'test'])
)
)
->leftJoin(
Join::create('link2', 'alias2')
)
->build();
$this->assertEquals(
[
Join::create('link1', 'alias1')
->withConditions(WhereClause::fromRaw(['name' => 'test']))
->withLeft(),
Join::create('link2', 'alias2')
->withLeft(),
],
$query->getJoins()
);
}
public function testJoin1()
{
$params = $this->builder
->from('Test')
->join('link1')
->join('link1')
->join('link2')
->build()
->getRaw();
$this->assertEquals(
[
['link1', null, null, ['type' => null]],
['link2', null, null, ['type' => null]],
],
$params['joins']
);
}
public function testJoin2()
{
$query = $this->builder
->from('Test')
->join('link1', 'alias1', ['name' => 'test'])
->join('link2', 'alias2')
->build();
$this->assertEquals(
[
Join::create('link1', 'alias1')
->withConditions(WhereClause::fromRaw(['name' => 'test']))
->withInner(),
Join::create('link2', 'alias2')
->withInner(),
],
$query->getJoins()
);
}
public function testJoin3()
{
$query = $this->builder
->from('Test')
->join(
Join::create('link1', 'alias1')
->withConditions(WhereClause::fromRaw(['name' => 'test']))
)
->join(Join::create('link2', 'alias2'))
->build();
$this->assertEquals(
[
Join::create('link1', 'alias1')
->withConditions(WhereClause::fromRaw(['name' => 'test']))
->withInner(),
Join::create('link2', 'alias2')
->withInner(),
],
$query->getJoins()
);
}
public function testWhereItemUsage1()
{
$query = $this->builder
->from('Test')
->where(
Cond::or(
Cond::equal(Expr::column('test'), '1'),
Cond::equal(Expr::column('test'), '2')
)
)
->join(
'Table1',
'table1',
Cond::equal(Expr::column('table1.testId'), Expr::column('id'))
)
->leftJoin(
'Table2',
'table2',
Cond::equal(Expr::column('table2.testId'), Expr::column('id'))
)
->build();
$raw = $query->getRaw();
$expectedWhere = [
'OR' => [
['test=' => '1'],
['test=' => '2'],
]
];
$expectedJoins = [
[
'Table1',
'table1',
[
'table1.testId=:' => 'id'
],
['noLeftAlias' => true, 'type' => null]
],
[
'Table2',
'table2',
[
'table2.testId=:' => 'id'
],
['noLeftAlias' => true, 'type' => Join\JoinType::left]
]
];
$this->assertEquals($expectedWhere, $raw['whereClause']);
$this->assertEquals($expectedJoins, $raw['joins']);
}
public function testExists1(): void
{
$query = (new SelectBuilder())
->select('id')
->from('Test', 'test')
->where(
Cond::exists(
(new SelectBuilder())
->select('id')
->from('Test', 'sq')
->where(
Cond::equal(
Cond::column('test.id'),
Cond::column('sq.id')
)
)
->build()
)
)
->build();
$expected = [
'select' => ['id'],
'from' => 'Test',
'fromAlias' => 'test',
'whereClause' => [
'EXISTS' => (new SelectBuilder())
->select('id')
->from('Test', 'sq')
->where(
Cond::equal(
Cond::column('test.id'),
Cond::column('sq.id')
)
)
->build()
],
];
$this->assertEquals($expected, $query->getRaw());
}
}

View File

@@ -0,0 +1,73 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\ORM\Query;
use Espo\ORM\Query\SelectBuilder;
class SelectTest extends \PHPUnit\Framework\TestCase
{
/**
* @var SelectBuilder
*/
private $builder;
protected function setUp(): void
{
$this->builder = new SelectBuilder();
}
public function testGetWhere1(): void
{
$query = $this->builder
->from('Test')
->where([
'test' => 'hello'
])
->build();
$this->assertEquals(
[
'test' => 'hello'
],
$query->getWhere()->getRaw()
);
}
public function testGetWhere2(): void
{
$query = $this->builder
->from('Test')
->build();
$this->assertNull(
$query->getWhere()
);
}
}