diff --git a/tests/unit/Espo/ORM/Query/Part/ExpressionTest.php b/tests/unit/Espo/ORM/Query/Part/ExpressionTest.php new file mode 100644 index 0000000000..8cc39a071c --- /dev/null +++ b/tests/unit/Espo/ORM/Query/Part/ExpressionTest.php @@ -0,0 +1,434 @@ +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); + } +} diff --git a/tests/unit/Espo/ORM/Query/Part/JoinTest.php b/tests/unit/Espo/ORM/Query/Part/JoinTest.php new file mode 100644 index 0000000000..8730f5504b --- /dev/null +++ b/tests/unit/Espo/ORM/Query/Part/JoinTest.php @@ -0,0 +1,80 @@ +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()); + } + + 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()); + } + + public function testCreate3(): void + { + $join = Join::create('Test', 'testAlias'); + + $this->assertEquals('Test', $join->getTarget()); + $this->assertEquals('testAlias', $join->getAlias()); + } +} diff --git a/tests/unit/Espo/ORM/Query/Part/OrderExpressionTest.php b/tests/unit/Espo/ORM/Query/Part/OrderExpressionTest.php new file mode 100644 index 0000000000..14f769ad74 --- /dev/null +++ b/tests/unit/Espo/ORM/Query/Part/OrderExpressionTest.php @@ -0,0 +1,78 @@ +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()); + } +} diff --git a/tests/unit/Espo/ORM/Query/Part/WhereTest.php b/tests/unit/Espo/ORM/Query/Part/WhereTest.php new file mode 100644 index 0000000000..0d1fb08903 --- /dev/null +++ b/tests/unit/Espo/ORM/Query/Part/WhereTest.php @@ -0,0 +1,493 @@ +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(); + + $expextedRaw = [ + 'id=s' => [ + 'from' => 'Test', + ], + ]; + + $this->assertEquals($expextedRaw, $raw); + } + + public function testNotInQuery(): void + { + $query = (new SelectBuilder)->from('Test')->build(); + + $raw = + Cond::notIn(Expr::column('id'), $query) + ->getRaw(); + + $expextedRaw = [ + 'id!=s' => [ + 'from' => 'Test', + ], + ]; + + $this->assertEquals($expextedRaw, $raw); + } + + public function testInList(): void + { + $raw = + Cond::in(Expr::column('status'), ['1', '2']) + ->getRaw(); + + $expextedRaw = [ + 'status=' => ['1', '2'] + ]; + + $this->assertEquals($expextedRaw, $raw); + } + + public function testNotInList(): void + { + $raw = + Cond::notIn(Expr::column('status'), ['1', '2']) + ->getRaw(); + + $expextedRaw = [ + 'status!=' => ['1', '2'] + ]; + + $this->assertEquals($expextedRaw, $raw); + } +}