Skip to content

Commit 4a46843

Browse files
committed
added union tests
1 parent d28bbf2 commit 4a46843

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

src/Clauses/UnionClause.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ public function canBeEmpty(): bool
6969
*/
7070
protected function getSubject(): string
7171
{
72-
return 'UNION';
72+
return $this->all ? 'ALL' : '';
7373
}
7474

7575
/**
7676
* @inheritDoc
7777
*/
7878
protected function getClause(): string
7979
{
80-
return $this->all ? 'ALL' : '';
80+
return 'UNION';
8181
}
8282
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace WikibaseSolutions\CypherDSL\Tests\Unit\Clauses;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use WikibaseSolutions\CypherDSL\Clauses\UnionClause;
7+
use WikibaseSolutions\CypherDSL\Query;
8+
9+
class UnionClauseTest extends TestCase
10+
{
11+
public function testNoCombine(): void
12+
{
13+
$union = new UnionClause();
14+
15+
$this->assertFalse($union->includesAll());
16+
17+
$this->assertEquals('UNION', $union->toQuery());
18+
}
19+
20+
public function testAll(): void
21+
{
22+
$union = new UnionClause(true);
23+
24+
$this->assertTrue($union->includesAll());
25+
26+
$this->assertEquals('UNION ALL', $union->toQuery());
27+
}
28+
29+
public function testUnionFactory(): void
30+
{
31+
$nodeX = Query::node('X')->named('x');
32+
$nodeY = Query::node('Y')->named('y');
33+
34+
$left = Query::new()->match($nodeX)->returning($nodeX->getVariable());
35+
$right = Query::new()->match($nodeY)->returning($nodeY->getVariable());
36+
37+
$query = UnionClause::union($left, $right, false);
38+
39+
$this->assertEquals('MATCH (x:X) RETURN x UNION MATCH (y:Y) RETURN y', $query->toQuery());
40+
}
41+
42+
public function testUnionFactoryAll(): void
43+
{
44+
$nodeX = Query::node('X')->named('x');
45+
$nodeY = Query::node('Y')->named('y');
46+
47+
$left = Query::new()->match($nodeX)->returning($nodeX->getVariable());
48+
$right = Query::new()->match($nodeY)->returning($nodeY->getVariable());
49+
50+
$query = UnionClause::union($left, $right, true);
51+
52+
$this->assertEquals('MATCH (x:X) RETURN x UNION ALL MATCH (y:Y) RETURN y', $query->toQuery());
53+
}
54+
}

tests/Unit/QueryTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use WikibaseSolutions\CypherDSL\Assignment;
2828
use WikibaseSolutions\CypherDSL\Clauses\Clause;
2929
use WikibaseSolutions\CypherDSL\Clauses\MatchClause;
30+
use WikibaseSolutions\CypherDSL\Clauses\UnionClause;
3031
use WikibaseSolutions\CypherDSL\Exists;
3132
use WikibaseSolutions\CypherDSL\ExpressionList;
3233
use WikibaseSolutions\CypherDSL\Literals\Boolean;
@@ -1028,6 +1029,60 @@ public function testWikiExamples(): void
10281029
$this->assertSame("((nineties.released >= 1990) AND (nineties IS NOT NULL))", $expression->toQuery());
10291030
}
10301031

1032+
public function testUnionQueryAll(): void
1033+
{
1034+
$nodeX = Query::node('X')->named('x');
1035+
$nodeY = Query::node('Y')->named('y');
1036+
1037+
$query = Query::new()->match($nodeX)->returning($nodeX->getVariable());
1038+
$right = Query::new()->match($nodeY)->returning($nodeY->getVariable());
1039+
1040+
$query = $query->union($right, true);
1041+
1042+
$this->assertEquals('MATCH (x:X) RETURN x UNION ALL MATCH (y:Y) RETURN y', $query->toQuery());
1043+
}
1044+
1045+
public function testUnionQuery(): void
1046+
{
1047+
$nodeX = Query::node('X')->named('x');
1048+
$nodeY = Query::node('Y')->named('y');
1049+
1050+
$query = Query::new()->match($nodeX)->returning($nodeX->getVariable());
1051+
$right = Query::new()->match($nodeY)->returning($nodeY->getVariable());
1052+
1053+
$query = $query->union($right, false);
1054+
1055+
$this->assertEquals('MATCH (x:X) RETURN x UNION MATCH (y:Y) RETURN y', $query->toQuery());
1056+
}
1057+
1058+
public function testUnionDecorator(): void
1059+
{
1060+
$nodeX = Query::node('X')->named('x');
1061+
1062+
$query = Query::new()->match($nodeX)->returning($nodeX->getVariable());
1063+
1064+
$query = $query->union(function (Query $query) {
1065+
$nodeY = Query::node('Y')->named('y');
1066+
$query->match($nodeY)->returning($nodeY->getVariable());
1067+
});
1068+
1069+
$this->assertEquals('MATCH (x:X) RETURN x UNION MATCH (y:Y) RETURN y', $query->toQuery());
1070+
}
1071+
1072+
public function testUnionDecoratorAll(): void
1073+
{
1074+
$nodeX = Query::node('X')->named('x');
1075+
1076+
$query = Query::new()->match($nodeX)->returning($nodeX->getVariable());
1077+
1078+
$query = $query->union(function (Query $query) {
1079+
$nodeY = Query::node('Y')->named('y');
1080+
$query->match($nodeY)->returning($nodeY->getVariable());
1081+
}, true);
1082+
1083+
$this->assertEquals('MATCH (x:X) RETURN x UNION ALL MATCH (y:Y) RETURN y', $query->toQuery());
1084+
}
1085+
10311086
public function testAutomaticIdentifierGeneration(): void
10321087
{
10331088
$node = Query::node();

0 commit comments

Comments
 (0)