Skip to content

Commit 8808a32

Browse files
Additional end-to-end tests in preparation of 5.0 release
1 parent f444148 commit 8808a32

File tree

2 files changed

+241
-23
lines changed

2 files changed

+241
-23
lines changed

tests/end-to-end/ExamplesTest.php

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace WikibaseSolutions\CypherDSL\Tests\EndToEnd;
1111

1212
use PHPUnit\Framework\TestCase;
13+
use WikibaseSolutions\CypherDSL\Clauses\SetClause;
1314
use WikibaseSolutions\CypherDSL\Expressions\Procedures\Procedure;
1415
use WikibaseSolutions\CypherDSL\Query;
1516

@@ -262,6 +263,173 @@ public function testMatchClauseExample5(): void
262263
$this->assertStringMatchesFormat("MATCH (%s:Movie:Person) RETURN %s.name AS name, %s.title AS title", $query);
263264
}
264265

266+
public function testMatchClauseExample6(): void
267+
{
268+
$movie = Query::node();
269+
$query = Query::new()
270+
->match(
271+
Query::node('Person')->withProperties(['name' => 'Oliver Stone'])->relationshipTo($movie)
272+
)
273+
->returning($movie->property('title'))
274+
->build();
275+
276+
$this->assertStringMatchesFormat("MATCH (:Person {name: 'Oliver Stone'})-->(%s) RETURN %s.title", $query);
277+
}
278+
279+
public function testMatchClauseExample7(): void
280+
{
281+
$r = Query::relationshipTo();
282+
$query = Query::new()
283+
->match(Query::node('Person')->withProperties(['name' => 'Oliver Stone'])->relationship($r, Query::node()))
284+
->returning($r)
285+
->build();
286+
287+
$this->assertStringMatchesFormat("MATCH (:Person {name: 'Oliver Stone'})-[%s]->() RETURN %s", $query);
288+
}
289+
290+
public function testMatchClauseExample8(): void
291+
{
292+
$actor = Query::node();
293+
$query = Query::new()
294+
->match(
295+
Query::node('Movie')
296+
->withProperties(['title' => 'Wall Street'])
297+
->relationshipFrom($actor, 'ACTED_IN')
298+
)
299+
->returning($actor->property('name'))
300+
->build();
301+
302+
$this->assertStringMatchesFormat("MATCH (:Movie {title: 'Wall Street'})<-[:ACTED_IN]-(%s) RETURN %s.name", $query);
303+
}
304+
305+
public function testMatchClauseExample9(): void
306+
{
307+
$person = Query::node();
308+
$relationship = Query::relationshipFrom()->withTypes(['ACTED_IN', 'DIRECTED']);
309+
$query = Query::new()
310+
->match(
311+
Query::node('Movie')
312+
->withProperties(['title' => 'Wall Street'])
313+
->relationship($relationship, $person)
314+
)
315+
->returning($person->property('name'))
316+
->build();
317+
318+
$this->assertStringMatchesFormat("MATCH (:Movie {title: 'Wall Street'})<-[:ACTED_IN|DIRECTED]-(%s) RETURN %s.name", $query);
319+
}
320+
321+
public function testMatchClauseExample10(): void
322+
{
323+
$actor = Query::node();
324+
$relationship = Query::relationshipFrom()->withTypes(['ACTED_IN']);
325+
$query = Query::new()
326+
->match(
327+
Query::node('Movie')
328+
->withProperties(['title' => 'Wall Street'])
329+
->relationship($relationship, $actor)
330+
)
331+
->returning($relationship->property('role'))
332+
->build();
333+
334+
$this->assertStringMatchesFormat("MATCH (:Movie {title: 'Wall Street'})<-[%s:ACTED_IN]-() RETURN %s.role", $query);
335+
}
336+
337+
public function testMatchClauseExample11(): void
338+
{
339+
$charlie = Query::node('Person')->withProperties(['name' => 'Charlie Sheen']);
340+
$rob = Query::node('Person')->withProperties(['name' => 'Rob Reiner']);
341+
342+
$query = Query::new()
343+
->match([$charlie, $rob])
344+
->create(
345+
Query::node()
346+
->withVariable($rob->getVariable())
347+
->relationshipTo(
348+
Query::node()->withVariable($charlie->getVariable()), 'TYPE INCLUDING A SPACE')
349+
)
350+
->build();
351+
352+
$this->assertStringMatchesFormat("MATCH (%s:Person {name: 'Charlie Sheen'}), (%s:Person {name: 'Rob Reiner'}) CREATE (%s)-[:`TYPE INCLUDING A SPACE`]->(%s)", $query);
353+
}
354+
355+
public function testMatchClauseExample12(): void
356+
{
357+
$movie = Query::node();
358+
$director = Query::node();
359+
360+
$query = Query::new()
361+
->match(
362+
Query::node()
363+
->withProperties(['name' => 'Charlie Sheen'])
364+
->relationshipTo($movie, 'ACTED_IN')
365+
->relationshipFrom($director, 'DIRECTED'))
366+
->returning([$movie->property('title'), $director->property('name')])
367+
->build();
368+
369+
$this->assertStringMatchesFormat("MATCH ({name: 'Charlie Sheen'})-[:ACTED_IN]->(%s)<-[:DIRECTED]-(%s) RETURN %s.title, %s.name", $query);
370+
}
371+
372+
public function testMatchClauseExample13(): void
373+
{
374+
$movie = Query::node('Movie');
375+
$r = Query::relationshipUni()->addType('ACTED_IN')->withMinHops(1)->withMaxHops(3);
376+
377+
$query = Query::new()
378+
->match(Query::node()->withProperties(['name' => 'Charlie Sheen'])->relationship($r, $movie))
379+
->returning($movie->property('title'))
380+
->build();
381+
382+
$this->assertStringMatchesFormat("MATCH ({name: 'Charlie Sheen'})-[:ACTED_IN*1..3]-(%s:Movie) RETURN %s.title", $query);
383+
}
384+
385+
public function testMatchClauseExample14(): void
386+
{
387+
$p = Query::node()->withProperties(['name' => 'Michael Douglas'])->relationshipTo(Query::node());
388+
389+
$query = Query::new()
390+
->match($p)
391+
->returning($p)
392+
->build();
393+
394+
$this->assertStringMatchesFormat("MATCH %s = ({name: 'Michael Douglas'})-->() RETURN %s", $query);
395+
}
396+
397+
public function testMergeClauseExample1(): void
398+
{
399+
$robert = Query::node('Critic');
400+
401+
$query = Query::new()
402+
->merge($robert)
403+
->returning($robert)
404+
->build();
405+
406+
$this->assertStringMatchesFormat("MERGE (%s:Critic) RETURN %s", $query);
407+
}
408+
409+
public function testMergeClauseExample2(): void
410+
{
411+
$keanu = Query::node('Person')->withProperties(['name' => 'Keanu Reeves']);
412+
413+
$query = Query::new()
414+
->merge($keanu, (new SetClause())->add($keanu->property('created')->replaceWith(Query::procedure()::raw('timestamp'))))
415+
->returning([$keanu->property('name'), $keanu->property('created')])
416+
->build();
417+
418+
$this->assertStringMatchesFormat("MERGE (%s:Person {name: 'Keanu Reeves'}) ON CREATE SET %s.created = timestamp() RETURN %s.name, %s.created", $query);
419+
}
420+
421+
public function testMergeClauseExample3(): void
422+
{
423+
$keanu = Query::node('Person')->withProperties(['name' => 'Keanu Reeves']);
424+
425+
$query = Query::new()
426+
->merge($keanu, null, (new SetClause())->add($keanu->property('created')->replaceWith(Query::procedure()::raw('timestamp'))))
427+
->returning([$keanu->property('name'), $keanu->property('created')])
428+
->build();
429+
430+
$this->assertStringMatchesFormat("MERGE (%s:Person {name: 'Keanu Reeves'}) ON MATCH SET %s.created = timestamp() RETURN %s.name, %s.created", $query);
431+
}
432+
265433
public function testOptionalMatchClauseExample1(): void
266434
{
267435
$movies = Query::node("Movie");
@@ -271,4 +439,42 @@ public function testOptionalMatchClauseExample1(): void
271439

272440
$this->assertSame("OPTIONAL MATCH (:Movie)", $query);
273441
}
442+
443+
public function testCombiningClausesExample1(): void
444+
{
445+
$nineties = Query::node("Movie");
446+
$expression = $nineties->property('released')->gte(1990)->and($nineties->property('released')->lt(2000));
447+
448+
$statement = Query::new()
449+
->match($nineties)
450+
->where($expression)
451+
->returning($nineties->property("title"))
452+
->build();
453+
454+
$this->assertStringMatchesFormat("MATCH (%s:Movie) WHERE ((%s.released >= 1990) AND (%s.released < 2000)) RETURN %s.title", $statement);
455+
}
456+
457+
public function testExpressions1(): void
458+
{
459+
$released = Query::variable("nineties")->property("released");
460+
$expression = $released->gte(1990)->and($released->lt(2000));
461+
462+
$this->assertSame("((nineties.released >= 1990) AND (nineties.released < 2000))", $expression->toQuery());
463+
}
464+
465+
public function testExpressions2(): void
466+
{
467+
$name = Query::variable("actor")->property("name");
468+
$expression = $name->notEquals("Tom Hanks");
469+
470+
$this->assertSame("(actor.name <> 'Tom Hanks')", $expression->toQuery());
471+
}
472+
473+
public function testExpressions3(): void
474+
{
475+
$released = Query::variable("nineties")->property("released");
476+
$expression = $released->gte(1990)->and(Query::rawExpression("(nineties IS NOT NULL)"));
477+
478+
$this->assertSame("((nineties.released >= 1990) AND (nineties IS NOT NULL))", $expression->toQuery());
479+
}
274480
}

tests/end-to-end/MoviesTest.php

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
*/
2525
final class MoviesTest extends TestCase
2626
{
27+
public function testFindAllMovies(): void
28+
{
29+
$movie = node("Movie");
30+
$query = query()
31+
->match($movie)
32+
->returning($movie)
33+
->build();
34+
35+
$this->assertStringMatchesFormat("MATCH (%s:Movie) RETURN %s", $query);
36+
}
37+
2738
public function testFindActorNamedTomHanks(): void
2839
{
2940
$tom = node()->withProperties([
@@ -32,9 +43,10 @@ public function testFindActorNamedTomHanks(): void
3243

3344
$query = query()
3445
->match($tom)
35-
->returning($tom);
46+
->returning($tom)
47+
->build();
3648

37-
$this->assertStringMatchesFormat('MATCH (%s {name: \'Tom Hanks\'}) RETURN %s', $query->toQuery());
49+
$this->assertStringMatchesFormat("MATCH (%s {name: 'Tom Hanks'}) RETURN %s", $query);
3850
}
3951

4052
public function testFindTheMovieWithTitleCloudAtlas(): void
@@ -45,9 +57,10 @@ public function testFindTheMovieWithTitleCloudAtlas(): void
4557

4658
$query = query()
4759
->match($cloudAtlas)
48-
->returning($cloudAtlas);
60+
->returning($cloudAtlas)
61+
->build();
4962

50-
$this->assertStringMatchesFormat('MATCH (%s {title: \'Cloud Atlas\'}) RETURN %s', $query->toQuery());
63+
$this->assertStringMatchesFormat("MATCH (%s {title: 'Cloud Atlas'}) RETURN %s", $query);
5164
}
5265

5366
public function testFind10People(): void
@@ -57,9 +70,10 @@ public function testFind10People(): void
5770
$query = query()
5871
->match($people)
5972
->returning($people->property('name'))
60-
->limit(10);
73+
->limit(10)
74+
->build();
6175

62-
$this->assertStringMatchesFormat('MATCH (%s:Person) RETURN %s.name LIMIT 10', $query->toQuery());
76+
$this->assertStringMatchesFormat("MATCH (%s:Person) RETURN %s.name LIMIT 10", $query);
6377
}
6478

6579
public function testFindMoviesReleasedInThe1990s(): void
@@ -71,51 +85,49 @@ public function testFindMoviesReleasedInThe1990s(): void
7185
$nineties->property('released')->gte(1990),
7286
$nineties->property('released')->lt(2000),
7387
])
74-
->returning($nineties->property('title'));
88+
->returning($nineties->property('title'))
89+
->build();
7590

76-
$this->assertStringMatchesFormat('MATCH (%s:Movie) WHERE ((%s.released >= 1990) AND (%s.released < 2000)) RETURN %s.title', $query->toQuery());
91+
$this->assertStringMatchesFormat("MATCH (%s:Movie) WHERE ((%s.released >= 1990) AND (%s.released < 2000)) RETURN %s.title", $query);
7792
}
7893

7994
public function testListAllTomHanksMovies(): void
8095
{
8196
$movies = node();
82-
$tom = node('Person')->withProperties([
83-
'name' => 'Tom Hanks',
84-
]);
97+
$tom = node('Person')->withProperties(['name' => 'Tom Hanks']);
8598

8699
$query = query()
87100
->match($tom->relationshipTo($movies, 'ACTED_IN'))
88-
->returning([$tom, $movies]);
101+
->returning([$tom, $movies])
102+
->build();
89103

90-
$this->assertStringMatchesFormat('MATCH (%s:Person {name: \'Tom Hanks\'})-[:ACTED_IN]->(%s) RETURN %s, %s', $query->toQuery());
104+
$this->assertStringMatchesFormat("MATCH (%s:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(%s) RETURN %s, %s", $query);
91105
}
92106

93107
public function testWhoDirectedCloudAtlas(): void
94108
{
95109
$directors = node();
96-
$cloudAtlas = node()->withProperties([
97-
'title' => 'Cloud Atlas',
98-
]);
110+
$cloudAtlas = node()->withProperties(['title' => 'Cloud Atlas',]);
99111

100112
$query = query()
101113
->match($cloudAtlas->relationshipFrom($directors, 'DIRECTED'))
102-
->returning($directors->property('name'));
114+
->returning($directors->property('name'))
115+
->build();
103116

104-
$this->assertStringMatchesFormat('MATCH ({title: \'Cloud Atlas\'})<-[:DIRECTED]-(%s) RETURN %s.name', $query->toQuery());
117+
$this->assertStringMatchesFormat("MATCH ({title: 'Cloud Atlas'})<-[:DIRECTED]-(%s) RETURN %s.name", $query);
105118
}
106119

107120
public function testTomHanksCoActors(): void
108121
{
109122
$coActors = node();
110-
$tom = node('Person')->withProperties([
111-
'name' => 'Tom Hanks',
112-
]);
123+
$tom = node('Person')->withProperties(['name' => 'Tom Hanks']);
113124

114125
$query = query()
115126
->match($tom->relationshipTo(node(), 'ACTED_IN')->relationshipFrom($coActors, 'ACTED_IN'))
116-
->returning($coActors->property('name'));
127+
->returning($coActors->property('name'))
128+
->build();
117129

118-
$this->assertStringMatchesFormat('MATCH (:Person {name: \'Tom Hanks\'})-[:ACTED_IN]->()<-[:ACTED_IN]-(%s) RETURN %s.name', $query->toQuery());
130+
$this->assertStringMatchesFormat("MATCH (:Person {name: 'Tom Hanks'})-[:ACTED_IN]->()<-[:ACTED_IN]-(%s) RETURN %s.name", $query);
119131
}
120132

121133
public function testMoviesAndActorsUpTo4HopsAwayFromKevinBacon(): void

0 commit comments

Comments
 (0)