Skip to content

Commit da5b5e9

Browse files
Add tests for Functions and CALL and MATCH
1 parent 59c1463 commit da5b5e9

File tree

7 files changed

+313
-90
lines changed

7 files changed

+313
-90
lines changed

src/Clauses/MatchClause.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* This class represents a MATCH clause.
2929
*
3030
* @see https://neo4j.com/docs/cypher-manual/current/clauses/match/
31+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 57)
3132
*/
3233
class MatchClause extends Clause
3334
{
@@ -38,20 +39,6 @@ class MatchClause extends Clause
3839
*/
3940
private array $patterns = [];
4041

41-
/**
42-
* Sets the patterns of the MATCH clause. This overwrites any previously added patterns.
43-
*
44-
* @param MatchablePattern[] $patterns
45-
* @return $this
46-
*/
47-
public function setPatterns(array $patterns): self
48-
{
49-
self::assertClassArray('patterns', MatchablePattern::class, $patterns);
50-
$this->patterns = $patterns;
51-
52-
return $this;
53-
}
54-
5542
/**
5643
* Add one or more patterns to the MATCH clause.
5744
*

src/Expressions/Literals/Map.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ final class Map implements MapType
4141
* @param AnyType[] $elements Associative array of the elements that this map should have
4242
* @internal This method is not covered by the backwards compatibility promise of php-cypher-dsl
4343
*/
44-
public function __construct(array $properties = [])
44+
public function __construct(array $elements = [])
4545
{
46-
self::assertClassArray('properties', AnyType::class, $properties);
47-
$this->properties = $properties;
46+
self::assertClassArray('properties', AnyType::class, $elements);
47+
$this->elements = $elements;
4848
}
4949

5050
/**

src/Query.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,21 +428,21 @@ public function callProcedure(string $procedure, $arguments = [], $yields = []):
428428
/**
429429
* Creates the MATCH clause.
430430
*
431-
* @param PathType|NodeType|PathType[]|NodeType[] $patterns A single pattern or a list of patterns
431+
* @param MatchablePattern|MatchablePattern[] $patterns A single pattern or a list of patterns
432432
*
433433
* @return $this
434434
*
435435
* @see https://neo4j.com/docs/cypher-manual/current/clauses/match/
436+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 57)
436437
*/
437438
public function match($patterns): self
438439
{
439-
$matchClause = new MatchClause();
440-
441440
if (!is_array($patterns)) {
442441
$patterns = [$patterns];
443442
}
444443

445-
$matchClause->setPatterns($patterns);
444+
$matchClause = new MatchClause();
445+
$matchClause->addPattern(...$patterns);
446446

447447
$this->clauses[] = $matchClause;
448448

src/functions.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ function query(): Query
2929
return Query::new();
3030
}
3131

32+
/**
33+
* @see Query::new()
34+
*/
35+
function _(): Query
36+
{
37+
return Query::new();
38+
}
39+
3240
/**
3341
* @see Query::node()
3442
*/
@@ -124,11 +132,3 @@ function raw(string $expression): RawExpression
124132
{
125133
return Query::rawExpression($expression);
126134
}
127-
128-
/**
129-
* @see Query::exists()
130-
*/
131-
function exists($match, $where = null, bool $insertParentheses = false): Exists
132-
{
133-
return Query::exists($match, $where, $insertParentheses);
134-
}

tests/Unit/FunctionsTest.php

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<?php
2+
3+
namespace WikibaseSolutions\CypherDSL\Tests\Unit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use TypeError;
7+
use WikibaseSolutions\CypherDSL\Expressions\Functions\Func;
8+
use WikibaseSolutions\CypherDSL\Expressions\Literals\Boolean;
9+
use WikibaseSolutions\CypherDSL\Expressions\Literals\Float_;
10+
use WikibaseSolutions\CypherDSL\Expressions\Literals\Integer;
11+
use WikibaseSolutions\CypherDSL\Expressions\Literals\List_;
12+
use WikibaseSolutions\CypherDSL\Expressions\Literals\Literal;
13+
use WikibaseSolutions\CypherDSL\Expressions\Literals\Map;
14+
use WikibaseSolutions\CypherDSL\Expressions\Literals\String_;
15+
use WikibaseSolutions\CypherDSL\Expressions\RawExpression;
16+
use WikibaseSolutions\CypherDSL\Expressions\Variable;
17+
use WikibaseSolutions\CypherDSL\Patterns\Node;
18+
use WikibaseSolutions\CypherDSL\Patterns\Relationship;
19+
use WikibaseSolutions\CypherDSL\Query;
20+
use function WikibaseSolutions\CypherDSL\float;
21+
use function WikibaseSolutions\CypherDSL\function_;
22+
use function WikibaseSolutions\CypherDSL\integer;
23+
use function WikibaseSolutions\CypherDSL\list_;
24+
use function WikibaseSolutions\CypherDSL\literal;
25+
use function WikibaseSolutions\CypherDSL\map;
26+
use function WikibaseSolutions\CypherDSL\node;
27+
use function WikibaseSolutions\CypherDSL\query;
28+
use function WikibaseSolutions\CypherDSL\raw;
29+
use function WikibaseSolutions\CypherDSL\relationship;
30+
use function WikibaseSolutions\CypherDSL\string;
31+
use function WikibaseSolutions\CypherDSL\variable;
32+
33+
/**
34+
* Tests the functions in "functions.php".
35+
*
36+
* @coversNothing
37+
*/
38+
class FunctionsTest extends TestCase
39+
{
40+
public function testQueryReturnsQuery()
41+
{
42+
$query = query();
43+
44+
$this->assertInstanceOf(Query::class, $query);
45+
}
46+
47+
public function testNodeReturnsNode()
48+
{
49+
$node = node('label');
50+
51+
$this->assertInstanceOf(Node::class, $node);
52+
$this->assertSame('(:label)', $node->toQuery());
53+
}
54+
55+
public function testNodeAcceptsEmptyLabel()
56+
{
57+
$node = node();
58+
59+
$this->assertInstanceOf(Node::class, $node);
60+
$this->assertSame('()', $node->toQuery());
61+
}
62+
63+
public function testNodeOnlyAcceptsStringLabel()
64+
{
65+
$this->expectException(TypeError::class);
66+
67+
node([]);
68+
}
69+
70+
public function testRelationshipReturnsRelationship()
71+
{
72+
$relationship = relationship(Relationship::DIR_RIGHT);
73+
74+
$this->assertInstanceOf(Relationship::class, $relationship);
75+
$this->assertSame('-->', $relationship->toQuery());
76+
}
77+
78+
public function testVariableReturnsVariable()
79+
{
80+
$variable = variable('foobar');
81+
82+
$this->assertInstanceOf(Variable::class, $variable);
83+
$this->assertSame('foobar', $variable->toQuery());
84+
}
85+
86+
public function testVariableAcceptsEmptyArgument()
87+
{
88+
$variable = variable();
89+
90+
$this->assertInstanceOf(Variable::class, $variable);
91+
$this->assertStringMatchesFormat('var%s', $variable->toQuery());
92+
}
93+
94+
public function testVariableOnlyAcceptsString()
95+
{
96+
$this->expectException(TypeError::class);
97+
98+
variable([]);
99+
}
100+
101+
public function testLiteralReturnsLiteralClass()
102+
{
103+
$literal = literal();
104+
105+
$this->assertSame(Literal::class, $literal);
106+
}
107+
108+
public function testLiteralReturnsLiteral()
109+
{
110+
$literal = literal('foobar');
111+
112+
$this->assertInstanceOf(String_::class, $literal);
113+
$this->assertSame("'foobar'", $literal->toQuery());
114+
115+
$literal = literal(true);
116+
117+
$this->assertInstanceOf(Boolean::class, $literal);
118+
$this->assertSame("true", $literal->toQuery());
119+
}
120+
121+
public function testBooleanReturnsBoolean()
122+
{
123+
$boolean = \WikibaseSolutions\CypherDSL\boolean(true);
124+
125+
$this->assertInstanceOf(Boolean::class, $boolean);
126+
$this->assertSame('true', $boolean->toQuery());
127+
}
128+
129+
public function testBooleanOnlyAcceptsBoolean()
130+
{
131+
$this->expectException(TypeError::class);
132+
133+
\WikibaseSolutions\CypherDSL\boolean([]);
134+
}
135+
136+
public function testStringReturnsString()
137+
{
138+
$string = string('hello world');
139+
140+
$this->assertInstanceOf(String_::class, $string);
141+
$this->assertSame("'hello world'", $string->toQuery());
142+
}
143+
144+
public function testStringOnlyAcceptsString()
145+
{
146+
$this->expectException(TypeError::class);
147+
148+
string([]);
149+
}
150+
151+
public function testIntegerReturnsInteger()
152+
{
153+
$integer = integer(1);
154+
155+
$this->assertInstanceOf(Integer::class, $integer);
156+
$this->assertSame("1", $integer->toQuery());
157+
}
158+
159+
public function testIntegerOnlyAcceptsInteger()
160+
{
161+
$this->expectException(TypeError::class);
162+
163+
integer([]);
164+
}
165+
166+
public function testFloatReturnsFloat()
167+
{
168+
$float = float(1.1);
169+
170+
$this->assertInstanceOf(Float_::class, $float);
171+
$this->assertSame("1.1", $float->toQuery());
172+
}
173+
174+
public function testFloatOnlyAcceptsFloat()
175+
{
176+
$this->expectException(TypeError::class);
177+
178+
float([]);
179+
}
180+
181+
public function testListReturnsList()
182+
{
183+
$list = list_(['a', 'b', 'c']);
184+
185+
$this->assertInstanceOf(List_::class, $list);
186+
$this->assertSame("['a', 'b', 'c']", $list->toQuery());
187+
}
188+
189+
public function testListOnlyAcceptsArray()
190+
{
191+
$this->expectException(TypeError::class);
192+
193+
list_(1);
194+
}
195+
196+
public function testMapReturnsMap()
197+
{
198+
$map = map(['a' => 'b', 'c' => 'd']);
199+
200+
$this->assertInstanceOf(Map::class, $map);
201+
$this->assertSame("{a: 'b', c: 'd'}", $map->toQuery());
202+
}
203+
204+
public function testMapOnlyAcceptsArray()
205+
{
206+
$this->expectException(TypeError::class);
207+
208+
map(1);
209+
}
210+
211+
public function testFunctionReturnsFuncClass()
212+
{
213+
$function = function_();
214+
215+
$this->assertSame(Func::class, $function);
216+
}
217+
218+
public function testRawReturnsRawExpression()
219+
{
220+
$raw = raw('(unimplemented feature)');
221+
222+
$this->assertInstanceOf(RawExpression::class, $raw);
223+
$this->assertSame('(unimplemented feature)', $raw->toQuery());
224+
}
225+
226+
public function testRawOnlyAcceptsString()
227+
{
228+
$this->expectException(TypeError::class);
229+
230+
raw([]);
231+
}
232+
}

0 commit comments

Comments
 (0)