Skip to content

Commit 02a0fb0

Browse files
Add tests for several clauses
1 parent da5b5e9 commit 02a0fb0

24 files changed

+529
-454
lines changed
File renamed without changes.

src/Clauses/CallClause.php

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/*
4-
* Cypher DSL
5-
* Copyright (C) 2021 Wikibase Solutions
6-
*
7-
* This program is free software; you can redistribute it and/or
8-
* modify it under the terms of the GNU General Public License
9-
* as published by the Free Software Foundation; either version 2
10-
* of the License, or (at your option) any later version.
3+
* This file is part of php-cypher-dsl.
114
*
12-
* This program is distributed in the hope that it will be useful,
13-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
* GNU General Public License for more details.
5+
* Copyright (C) 2021- Wikibase Solutions
166
*
17-
* You should have received a copy of the GNU General Public License
18-
* along with this program; if not, write to the Free Software
19-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
209
*/
21-
2210
namespace WikibaseSolutions\CypherDSL\Clauses;
2311

2412
use WikibaseSolutions\CypherDSL\Expressions\Variable;
13+
use WikibaseSolutions\CypherDSL\Patterns\Pattern;
2514
use WikibaseSolutions\CypherDSL\Query;
2615
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
2716
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
@@ -30,7 +19,7 @@
3019
* This class represents a CALL {} (subquery) clause. The CALL {} clause evaluates a subquery that returns
3120
* some values.
3221
*
33-
* @note This clause is not officially part of the openCypher standard.
22+
* @note This feature is not officially part of the openCypher standard. For more information, see https://github.com/opencypher/openCypher/blob/a507292d35280aca9e37bf938cdec4fdd1e64ba9/docs/standardisation-scope.adoc.
3423
*
3524
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/
3625
* @see Query::call() for a more convenient method to construct this class
@@ -66,12 +55,12 @@ public function withSubQuery(Query $subQuery): self
6655
/**
6756
* Add one or more variables to include in the WITH clause.
6857
*
69-
* @param Variable|string ...$variables
58+
* @param Variable|Pattern|string ...$variables
7059
* @return $this
7160
*
7261
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/#subquery-correlated-importing
7362
*/
74-
public function addVariable(...$variables): self
63+
public function addWithVariable(...$variables): self
7564
{
7665
$res = [];
7766

@@ -121,9 +110,7 @@ protected function getSubject(): string
121110
}
122111

123112
if ($this->withVariables !== []) {
124-
$withClause = new WithClause();
125-
$withClause->setEntries($this->withVariables);
126-
$subQuery = $withClause->toQuery() . ' ' . $subQuery;
113+
$subQuery = Query::new()->with($this->withVariables)->toQuery() . ' ' . $subQuery;
127114
}
128115

129116
return sprintf('{ %s }', $subQuery);

src/Clauses/CallProcedureClause.php

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ class CallProcedureClause extends Clause
5555
private array $yields = [];
5656

5757
/**
58-
* Sets the procedure to call. This can be for instance "apoc.load.json". This procedure name is automatically
59-
* escaped.
58+
* Sets the procedure to call. This can be for instance "apoc.load.json".
6059
*
6160
* @param string $procedure The procedure to call
6261
* @return $this
@@ -68,23 +67,23 @@ public function setProcedure(string $procedure): self
6867
return $this;
6968
}
7069

71-
/**
72-
* Sets the literal arguments to pass to this procedure call. This overwrites any previously passed
73-
* arguments.
74-
*
75-
* @param AnyType[] $arguments The arguments to pass to the procedure
76-
* @return $this
77-
*/
78-
public function setArguments(array $arguments): self
79-
{
80-
foreach ($arguments as $argument) {
81-
$this->assertClass('arguments', AnyType::class, $argument);
82-
}
70+
/**
71+
* Sets the literal arguments to pass to this procedure call. This overwrites any previously passed
72+
* arguments.
73+
*
74+
* @param AnyType[] $arguments The arguments to pass to the procedure
75+
* @return $this
76+
*/
77+
public function setArguments(array $arguments): self
78+
{
79+
foreach ($arguments as $argument) {
80+
$this->assertClass('arguments', AnyType::class, $argument);
81+
}
8382

84-
$this->arguments = $arguments;
83+
$this->arguments = $arguments;
8584

86-
return $this;
87-
}
85+
return $this;
86+
}
8887

8988
/**
9089
* Add a literal argument to pass to this procedure call.
@@ -94,46 +93,46 @@ public function setArguments(array $arguments): self
9493
*/
9594
public function addArgument(AnyType $argument): self
9695
{
97-
$this->arguments[] = $argument;
96+
$this->arguments[] = $argument;
97+
98+
return $this;
99+
}
100+
101+
/**
102+
* Used to explicitly select which available result fields are returned as newly-bound
103+
* variables. If a key is non-numerical, it will be used as an alias.
104+
*
105+
* @param Variable[] $variables
106+
* @return $this
107+
*/
108+
public function setYields(array $variables): self
109+
{
110+
foreach ($variables as $variable) {
111+
$this->assertClass('variables', Variable::class, $variable);
112+
}
113+
114+
$this->yields = $variables;
98115

99-
return $this;
116+
return $this;
100117
}
101118

102-
/**
103-
* Used to explicitly select which available result fields are returned as newly-bound
104-
* variables. If a key is non-numerical, it will be used as an alias.
105-
*
106-
* @param Variable[] $variables
107-
* @return $this
108-
*/
109-
public function setYields(array $variables): self
110-
{
111-
foreach ($variables as $variable) {
112-
$this->assertClass('variables', Variable::class, $variable);
113-
}
114-
115-
$this->yields = $variables;
116-
117-
return $this;
118-
}
119-
120-
/**
121-
* Adds a variable to yield.
122-
*
123-
* @param Variable $variable The variable to yield
124-
* @param string|null $alias Optionally the alias to use for the variable
125-
* @return $this
126-
*/
127-
public function addYield(Variable $variable, ?string $alias = null): self
128-
{
129-
if ($alias !== null) {
130-
$this->yields[$alias] = $variable;
131-
} else {
132-
$this->yields[] = $variable;
133-
}
134-
135-
return $this;
136-
}
119+
/**
120+
* Adds a variable to yield.
121+
*
122+
* @param Variable $variable The variable to yield
123+
* @param string|null $alias Optionally the alias to use for the variable
124+
* @return $this
125+
*/
126+
public function addYield(Variable $variable, ?string $alias = null): self
127+
{
128+
if ($alias !== null) {
129+
$this->yields[$alias] = $variable;
130+
} else {
131+
$this->yields[] = $variable;
132+
}
133+
134+
return $this;
135+
}
137136

138137
/**
139138
* Returns the procedure to call.
@@ -182,23 +181,23 @@ protected function getSubject(): string
182181
return "";
183182
}
184183

185-
$procedure = implode(
186-
'.',
187-
array_map(fn (string $part): string => $this->escape($part), explode('.', $this->procedure))
188-
);
184+
$procedure = implode(
185+
'.',
186+
array_map(fn (string $part): string => $this->escape($part), explode('.', $this->procedure))
187+
);
189188

190189
$arguments = implode(
191190
", ",
192191
array_map(fn (AnyType $pattern): string => $pattern->toQuery(), $this->arguments)
193192
);
194193

195194
if (count($this->yields) > 0) {
196-
$yieldParameters = [];
197-
foreach ($this->yields as $alias => $yieldVariable) {
198-
$yieldParameters[] = is_int($alias) ?
199-
$yieldVariable->toQuery() :
200-
sprintf("%s AS %s", $yieldVariable->toQuery(), $this->escape($alias));
201-
}
195+
$yieldParameters = [];
196+
foreach ($this->yields as $alias => $yieldVariable) {
197+
$yieldParameters[] = is_int($alias) ?
198+
$yieldVariable->toQuery() :
199+
sprintf("%s AS %s", $yieldVariable->toQuery(), $this->escape($alias));
200+
}
202201

203202
return sprintf("%s(%s) YIELD %s", $procedure, $arguments, implode(", ", $yieldParameters));
204203
}

src/Clauses/LimitClause.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/*
4-
* Cypher DSL
5-
* Copyright (C) 2021 Wikibase Solutions
6-
*
7-
* This program is free software; you can redistribute it and/or
8-
* modify it under the terms of the GNU General Public License
9-
* as published by the Free Software Foundation; either version 2
10-
* of the License, or (at your option) any later version.
3+
* This file is part of php-cypher-dsl.
114
*
12-
* This program is distributed in the hope that it will be useful,
13-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
* GNU General Public License for more details.
5+
* Copyright (C) 2021- Wikibase Solutions
166
*
17-
* You should have received a copy of the GNU General Public License
18-
* along with this program; if not, write to the Free Software
19-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
209
*/
21-
2210
namespace WikibaseSolutions\CypherDSL\Clauses;
2311

12+
use WikibaseSolutions\CypherDSL\Query;
13+
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
2414
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\NumeralType;
2515

2616
/**
2717
* This class represents a LIMIT clause.
2818
*
2919
* @see https://neo4j.com/docs/cypher-manual/current/clauses/limit/
20+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 98)
21+
* @see Query::limit() for a more convenient method to construct this class
3022
*/
31-
class LimitClause extends Clause
23+
final class LimitClause extends Clause
3224
{
25+
use CastTrait;
26+
3327
/**
3428
* The expression of the LIMIT statement.
3529
*
@@ -40,12 +34,14 @@ class LimitClause extends Clause
4034
/**
4135
* Sets the expression that returns the limit.
4236
*
43-
* @param NumeralType $limit The limit
44-
* @return LimitClause
37+
* TODO: Rewrite this to only accept IntegerType (this also requires work in other parts of the DSL)
38+
*
39+
* @param NumeralType|int $limit The limit
40+
* @return $this
4541
*/
46-
public function setLimit(NumeralType $limit): self
42+
public function setLimit($limit): self
4743
{
48-
$this->limit = $limit;
44+
$this->limit = self::toNumeralType($limit);
4945

5046
return $this;
5147
}

src/Clauses/MatchClause.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,25 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/*
4-
* Cypher DSL
5-
* Copyright (C) 2021 Wikibase Solutions
6-
*
7-
* This program is free software; you can redistribute it and/or
8-
* modify it under the terms of the GNU General Public License
9-
* as published by the Free Software Foundation; either version 2
10-
* of the License, or (at your option) any later version.
3+
* This file is part of php-cypher-dsl.
114
*
12-
* This program is distributed in the hope that it will be useful,
13-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
* GNU General Public License for more details.
5+
* Copyright (C) 2021- Wikibase Solutions
166
*
17-
* You should have received a copy of the GNU General Public License
18-
* along with this program; if not, write to the Free Software
19-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
209
*/
21-
2210
namespace WikibaseSolutions\CypherDSL\Clauses;
2311

2412
use WikibaseSolutions\CypherDSL\Patterns\MatchablePattern;
25-
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2613

2714
/**
2815
* This class represents a MATCH clause.
2916
*
3017
* @see https://neo4j.com/docs/cypher-manual/current/clauses/match/
3118
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 57)
19+
* @see Query::match() for a more convenient method to construct this class
3220
*/
33-
class MatchClause extends Clause
21+
final class MatchClause extends Clause
3422
{
35-
use ErrorTrait;
36-
3723
/**
3824
* @var MatchablePattern[] List of patterns
3925
*/
@@ -43,7 +29,7 @@ class MatchClause extends Clause
4329
* Add one or more patterns to the MATCH clause.
4430
*
4531
* @param MatchablePattern ...$pattern
46-
* @return MatchClause
32+
* @return $this
4733
*/
4834
public function addPattern(MatchablePattern ...$pattern): self
4935
{

0 commit comments

Comments
 (0)