1010namespace WikibaseSolutions \CypherDSL \Tests \EndToEnd ;
1111
1212use PHPUnit \Framework \TestCase ;
13+ use WikibaseSolutions \CypherDSL \Clauses \SetClause ;
1314use WikibaseSolutions \CypherDSL \Expressions \Procedures \Procedure ;
1415use 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}
0 commit comments