Skip to content

Commit 2dc7414

Browse files
committed
fixed path tests
1 parent fa80c09 commit 2dc7414

File tree

9 files changed

+102
-55
lines changed

9 files changed

+102
-55
lines changed

src/Patterns/Path.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
1010
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
1111
use WikibaseSolutions\CypherDSL\Variable;
12+
use function array_key_exists;
1213
use function is_array;
1314

1415
class Path implements PathType
@@ -22,13 +23,16 @@ class Path implements PathType
2223
private array $nodes;
2324

2425
/**
25-
* @param Node|Node[] $nodes
26-
* @param Relationship|Relationship[] $relationships
26+
* @param Node|Node[]|null $nodes
27+
* @param Relationship|Relationship[]|null $relationships
2728
*/
28-
public function __construct($nodes, $relationships = null)
29+
public function __construct($nodes = null, $relationships = null)
2930
{
30-
self::assertClassOrType('relationships', [Relationship::class, 'array'], $relationships);
31-
self::assertClassOrType('nodes', [Node::class, 'array'], $nodes);
31+
self::assertClass('relationships', [Relationship::class, 'array', 'null'], $relationships);
32+
self::assertClass('nodes', [Node::class, 'array', 'null'], $nodes);
33+
34+
$nodes ??= [];
35+
$relationships ??= [];
3236

3337
$this->nodes = is_array($nodes) ? array_values($nodes) : [$nodes];
3438
$this->relationships = is_array($relationships) ? array_values($relationships) : [$relationships];
@@ -46,14 +50,15 @@ public function toQuery(): string
4650
}
4751

4852
foreach ($this->relationships as $i => $relationship) {
49-
if (count($this->nodes) <= $i) {
53+
if (!array_key_exists($i + 1, $this->nodes)) {
54+
--$i;
5055
break;
5156
}
5257
$cql .= $this->nodes[$i]->toQuery();
5358
$cql .= $relationship->toQuery();
5459
}
5560

56-
$cql .= $this->nodes[$i ?? 0]->toQuery();
61+
$cql .= $this->nodes[($i ?? -1) + 1]->toQuery();
5762

5863
return $cql;
5964
}
@@ -116,8 +121,8 @@ public function relationshipUni(NodeType $node, $properties = null, $name = null
116121
*/
117122
private function buildRelationship(array $direction, $properties, $name): Relationship
118123
{
119-
self::assertClassOrType('properties', ['array', PropertyMap::class, null], $properties);
120-
self::assertClassOrType('name', ['string', Variable::class, null], $name);
124+
self::assertClass('properties', ['array', PropertyMap::class, 'null'], $properties);
125+
self::assertClass('name', ['string', Variable::class, 'null'], $name);
121126

122127
$relationship = new Relationship($direction);
123128
if ($properties !== null) {

src/PropertyMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PropertyMap implements MapType
5454
public function __construct(array $properties = [])
5555
{
5656
foreach ($properties as $property) {
57-
$this->assertClass('property', AnyType::class, $property);
57+
self::assertClass('property', AnyType::class, $property);
5858
}
5959

6060
$this->properties = $properties;

src/Traits/AliasableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait AliasableTrait
1818
*/
1919
public function alias($variable): Alias
2020
{
21-
self::assertClassOrType($variable, [Variable::class, 'string'], 'variable');
21+
self::assertClass($variable, [Variable::class, 'string'], 'variable');
2222

2323
$variable = is_string($variable) ? new Variable($variable) : $variable;
2424

src/Traits/ErrorTrait.php

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,6 @@ private static function assertClass(string $varName, $classNames, $userInput): v
5959
}
6060
}
6161

62-
private static function assertClassOrType(string $varName, $classOrTypes, $userInput): void
63-
{
64-
if (!self::isClass($classOrTypes, $userInput) && !self::isType($classOrTypes, $userInput)) {
65-
throw self::typeError($varName, $classOrTypes, $userInput);
66-
}
67-
}
68-
69-
private static function assertType(string $varName, $types, $userInput): void
70-
{
71-
if (!self::isType($types, $userInput)) {
72-
throw self::typeError($varName, $types, $userInput);
73-
}
74-
}
75-
7662
/**
7763
* Get debug type method stolen from the symfony polyfill
7864
*
@@ -111,8 +97,8 @@ public static function getDebugType($value): string
11197
}
11298

11399
/**
114-
* @param $classNames
115-
* @param $userInput
100+
* @param string|array $classNames
101+
* @param mixed $userInput
116102
* @return bool
117103
*/
118104
private static function isClass($classNames, $userInput): bool
@@ -122,28 +108,8 @@ private static function isClass($classNames, $userInput): bool
122108
}
123109

124110
foreach ($classNames as $class) {
125-
if (is_a($userInput, $class)) {
126-
return true;
127-
}
128-
}
129-
130-
return false;
131-
}
132-
133-
/**
134-
* @param $types
135-
* @param $userInput
136-
* @return bool
137-
*/
138-
private static function isType($types, $userInput): bool
139-
{
140-
if (!is_array($types)) {
141-
$types = [$types];
142-
}
143-
144-
$actualType = gettype($userInput);
145-
foreach ($types as $type) {
146-
if ($actualType === $type) {
111+
$type = self::getDebugType($userInput);
112+
if ($class === $type || is_a($userInput, $class)) {
147113
return true;
148114
}
149115
}

src/Traits/HasPropertiesTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function withProperty(string $key, AnyType $value): self
3636
*/
3737
public function withProperties($properties): self
3838
{
39-
$this->assertClassOrType('properties', [PropertyMap::class, 'array'], $properties);
39+
self::assertClass('properties', [PropertyMap::class, 'array'], $properties);
4040

4141
$this->initialiseProperties();
4242

src/Traits/HasVariableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ trait HasVariableTrait
2525
*/
2626
public function named($variable): self
2727
{
28-
$this->assertClassOrType('variable', ['string', 'null', Variable::class], $variable);
28+
self::assertClass('variable', ['string', 'null', Variable::class], $variable);
2929

3030
if (is_string($variable)) {
3131
if (trim($variable) === '') {

src/Traits/RelationshipTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
*/
2727
trait RelationshipTrait
2828
{
29-
use StructuralTypeTrait;
3029
use HasPropertiesTrait;
3130
use HasVariableTrait;
3231
}

src/Variable.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
namespace WikibaseSolutions\CypherDSL;
2323

24+
use WikibaseSolutions\CypherDSL\Patterns\Node;
25+
use WikibaseSolutions\CypherDSL\Patterns\Path;
26+
use WikibaseSolutions\CypherDSL\Patterns\Relationship;
2427
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
2528
use WikibaseSolutions\CypherDSL\Traits\DateTimeTrait;
2629
use WikibaseSolutions\CypherDSL\Traits\DateTrait;
@@ -50,6 +53,7 @@
5053
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\TimeType;
5154
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
5255
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
56+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
5357

5458
/**
5559
* Represents a variable.
@@ -79,7 +83,6 @@ class Variable implements
7983
use LocalDateTimeTrait;
8084
use LocalTimeTrait;
8185
use NumeralTypeTrait;
82-
use StructuralTypeTrait;
8386
use PointTrait;
8487
use StringTypeTrait;
8588
use TimeTrait;
@@ -143,4 +146,25 @@ public function toQuery(): string
143146
{
144147
return self::escape($this->getName());
145148
}
149+
150+
151+
public function relationship(RelationshipType $relationship, NodeType $node): Path
152+
{
153+
return (new Path((new Node())->named($this)))->relationship($relationship, $node);
154+
}
155+
156+
public function relationshipTo(NodeType $node, $properties = null, $name = null): Path
157+
{
158+
return (new Path((new Node())->named($this)))->relationshipTo($node, $properties, $name);
159+
}
160+
161+
public function relationshipFrom(NodeType $node, $properties = null, $name = null): Path
162+
{
163+
return (new Path((new Node())->named($this)))->relationshipFrom($node, $properties, $name);
164+
}
165+
166+
public function relationshipUni(NodeType $node, $properties = null, $name = null): Path
167+
{
168+
return (new Path((new Node())->named($this)))->relationshipUni($node, $properties, $name);
169+
}
146170
}

tests/Unit/Patterns/PathTest.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,60 @@
22

33
namespace WikibaseSolutions\CypherDSL\Tests\Unit\Patterns;
44

5-
class PathTest
5+
use PHPUnit\Framework\TestCase;
6+
use WikibaseSolutions\CypherDSL\Patterns\Node;
7+
use WikibaseSolutions\CypherDSL\Patterns\Path;
8+
use WikibaseSolutions\CypherDSL\Patterns\Relationship;
9+
use WikibaseSolutions\CypherDSL\Query;
10+
11+
class PathTest extends TestCase
612
{
7-
public function test
13+
public function testEmpty(): void
14+
{
15+
self::assertEquals('', (new Path())->toQuery());
16+
self::assertEquals('', (new Path(null, []))->toQuery());
17+
self::assertEquals('', (new Path([], []))->toQuery());
18+
self::assertEquals('', (new Path([], [new Relationship(Relationship::DIR_UNI)]))->toQuery());
19+
}
20+
21+
public function testSingleNode(): void
22+
{
23+
$path = new Path(new Node());
24+
25+
self::assertEquals('()', $path->toQuery());
26+
}
27+
28+
public function testSingleNodeNamed(): void
29+
{
30+
$path = new Path(new Node());
31+
$path->named('a');
32+
33+
self::assertEquals('a = ()', $path->toQuery());
34+
}
35+
36+
public function testSingle(): void
37+
{
38+
$path = new Path(new Node(), new Relationship(Relationship::DIR_UNI));
39+
40+
self::assertEquals('()', $path->toQuery());
41+
}
42+
43+
public function testSingleNoRel(): void
44+
{
45+
$path = new Path([new Node(), new Node()]);
46+
47+
self::assertEquals('()', $path->toQuery());
48+
}
49+
50+
public function testRelationshipLong(): void
51+
{
52+
$path = new Path(new Node());
53+
$path->relationshipUni(new Node('Label'))
54+
->relationshipTo((new Node())->named('b'))
55+
->relationshipFrom(new Node(), ['x' => Query::literal('y')], 'c')
56+
->relationship(new Relationship(Relationship::DIR_UNI), (new Node())->named('d'))
57+
->named('a');
58+
59+
self::assertEquals('a = ()-[]-(:Label)-[]->(b)<-[c {x: \'y\'}]-()-[]-(d)', $path->toQuery());
60+
}
861
}

0 commit comments

Comments
 (0)