Skip to content

Commit cf0e1bc

Browse files
committed
added tests for CALL
1 parent 9d62a70 commit cf0e1bc

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/Clauses/CallClause.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,43 @@ class CallClause extends Clause
1414
/**
1515
* @var Query The query to call.
1616
*/
17-
private Query $query;
17+
private Query $subQuery;
1818

1919
/**
20-
* @param Query $query The query to call.
20+
* @param Query $subQuery The query to call.
2121
*/
22-
public function __construct(Query $query)
22+
public function __construct(Query $subQuery)
2323
{
24-
$this->query = $query;
25-
}
26-
27-
public function toQuery(): string
28-
{
29-
return sprintf('CALL { %s }', $this->getSubject());
24+
$this->subQuery = $subQuery;
3025
}
3126

3227
/**
3328
* Returns the query that is being called.
3429
*
3530
* @return Query
3631
*/
37-
public function getQuery(): Query
32+
public function getSubQuery(): Query
33+
{
34+
return $this->subQuery;
35+
}
36+
37+
public function toQuery(): string
3838
{
39-
return $this->query;
39+
$subQuery = trim($this->subQuery->toQuery());
40+
41+
if ($subQuery === '') {
42+
return '';
43+
}
44+
45+
return sprintf('CALL { %s }', $subQuery);
4046
}
4147

4248
/**
4349
* @inheritDoc
4450
*/
4551
protected function getSubject(): string
4652
{
47-
return $this->query->toQuery();
53+
return '{ ' . $this->subQuery->toQuery() . ' }';
4854
}
4955

5056
/**

tests/Unit/Clauses/CallClauseTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace WikibaseSolutions\CypherDSL\Tests\Unit\Clauses;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use WikibaseSolutions\CypherDSL\Clauses\CallClause;
7+
use WikibaseSolutions\CypherDSL\Query;
8+
9+
class CallClauseTest extends TestCase
10+
{
11+
public function testCallClauseEmpty(): void
12+
{
13+
$query = Query::new();
14+
15+
$clause = new CallClause($query);
16+
17+
$this->assertEquals('', $clause->toQuery());
18+
$this->assertEquals(Query::new(), $clause->getSubQuery());
19+
}
20+
21+
public function testCallClauseFilled(): void
22+
{
23+
$query = Query::new()->match(Query::node('X')->named('x'))->returning(Query::rawExpression('*'));
24+
25+
$clause = new CallClause($query);
26+
27+
$this->assertEquals('CALL { MATCH (x:X) RETURN * }', $clause->toQuery());
28+
$this->assertEquals($query, $clause->getSubQuery());
29+
}
30+
}

0 commit comments

Comments
 (0)