Skip to content

Commit 92f6afe

Browse files
phpunit implementation. main class Bolt base coverage.
1 parent c181175 commit 92f6afe

File tree

4 files changed

+191
-3
lines changed

4 files changed

+191
-3
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
.idea/
2-
2+
phpunit*.phar

autoload.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
spl_autoload_register(function ($name) {
66
$parts = explode("\\", $name);
77
$parts = array_filter($parts);
8-
array_shift($parts);
8+
if ($parts[0] != 'Bolt')
9+
return;
10+
array_shift($parts);
911

1012
/*
1113
* namespace calls
1214
*/
1315

1416
//compose standart namespaced path to file
15-
$path = '.' . DS . implode(DS, $parts) . '.php';
17+
$path = __DIR__ . DS . implode(DS, $parts) . '.php';
1618
if (file_exists($path)) {
1719
require_once $path;
1820
return;

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<phpunit bootstrap="autoload.php">
2+
<testsuites>
3+
<testsuite name="Bolt">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
<php>
8+
<var name="NEO_USER" value="neo4j"/>
9+
<var name="NEO_PASS" value="nothing"/>
10+
</php>
11+
<filter>
12+
<whitelist>
13+
<file>Bolt.php</file>
14+
<file>Socket.php</file>
15+
</whitelist>
16+
</filter>
17+
</phpunit>

tests/BoltTest.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
namespace Bolt\tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Bolt\Bolt;
7+
8+
/**
9+
* Class BoltTest
10+
* @author Michal Stefanak
11+
* @link https://github.com/stefanak-michal/Bolt
12+
* @covers \Bolt\Bolt
13+
* @package Bolt\tests
14+
* @requires PHP >= 7.1
15+
* @requires extension sockets
16+
* @requires extension mbstring
17+
*/
18+
class BoltTest extends TestCase
19+
{
20+
21+
public static function setUpBeforeClass(): void
22+
{
23+
Bolt::$errorHandler = function ($msg, $code) {
24+
echo $msg . ' (' . $code . ')' . PHP_EOL;
25+
};
26+
27+
//Todo pridat ked bude debugHandler aby sa dal naformatovat output
28+
//Bolt::$debug = true;
29+
}
30+
31+
/**
32+
* @return Bolt
33+
* @throws \Exception
34+
*/
35+
public function test__construct()
36+
{
37+
$bolt = new Bolt($GLOBALS['NEO_HOST'] ?? '127.0.0.1', $GLOBALS['NEO_PORT'] ?? 7687);
38+
$this->assertInstanceOf(Bolt::class, $bolt);
39+
return $bolt;
40+
}
41+
42+
/**
43+
* @depends test__construct
44+
* @param Bolt $bolt
45+
* @return Bolt
46+
* @throws \Exception
47+
*/
48+
public function testInit(Bolt $bolt)
49+
{
50+
$this->assertTrue($bolt->init('Test/1.0', $GLOBALS['NEO_USER'], $GLOBALS['NEO_PASS']));
51+
return $bolt;
52+
}
53+
54+
/**
55+
* @depends testInit
56+
* @param Bolt $bolt
57+
* @return Bolt
58+
*/
59+
public function testRun(Bolt $bolt)
60+
{
61+
$res = $bolt->run('RETURN 1 AS num, 2 AS cnt');
62+
$this->assertIsArray($res);
63+
$this->assertArrayHasKey('fields', $res);
64+
return $bolt;
65+
}
66+
67+
/**
68+
* @depends testRun
69+
* @param Bolt $bolt
70+
* @return Bolt
71+
*/
72+
public function testPull(Bolt $bolt)
73+
{
74+
$res = $bolt->pull();
75+
$this->assertEquals(1, $res[0][0] ?? 0);
76+
$this->assertEquals(2, $res[0][1] ?? 0);
77+
return $bolt;
78+
}
79+
80+
/**
81+
* @depends testInit
82+
* @param Bolt $bolt
83+
*/
84+
public function testDiscard(Bolt $bolt)
85+
{
86+
//test discard
87+
$this->assertNotFalse($bolt->run('MATCH (a:Test) RETURN *'));
88+
$this->assertTrue($bolt->discard());
89+
}
90+
91+
/**
92+
* @depends testInit
93+
* @depends testPull
94+
* @param Bolt $bolt
95+
* @return int
96+
*/
97+
public function testNodeCreate(Bolt $bolt)
98+
{
99+
$this->assertNotFalse($bolt->run('CREATE (a:Test) RETURN a, ID(a)'));
100+
101+
$created = $bolt->pull();
102+
$this->assertIsArray($created);
103+
$this->assertInstanceOf(\Bolt\structures\Node::class, $created[0][0]);
104+
return $created[0][1];
105+
}
106+
107+
/**
108+
* @depends testInit
109+
* @depends testNodeCreate
110+
* @param Bolt $bolt
111+
* @param int $id
112+
*/
113+
public function testNodeDelete(Bolt $bolt, int $id)
114+
{
115+
//test delete created node
116+
$this->assertNotFalse($bolt->run('MATCH (a:Test) WHERE ID(a) = ' . ($this->getParameterType($bolt) ? '{a}' : '$a') . ' DELETE a', [
117+
'a' => $id
118+
]));
119+
$this->assertEquals(1, $bolt->pull()[0]['stats']['nodes-deleted'] ?? 0);
120+
}
121+
122+
/**
123+
* @depends testInit
124+
* @param Bolt $bolt
125+
*/
126+
public function testTransaction(Bolt $bolt)
127+
{
128+
if ($bolt->getProtocolVersion() < 3) {
129+
$this->markTestSkipped('Old Neo4j version does not support transactions');
130+
return;
131+
}
132+
133+
$this->assertTrue($bolt->begin());
134+
$this->assertNotFalse($bolt->run('CREATE (a:Test) RETURN a, ID(a)'));
135+
$created = $bolt->pull();
136+
$this->assertIsArray($created);
137+
$this->assertTrue($bolt->rollback());
138+
139+
$this->assertNotFalse($bolt->run('MATCH (a:Test) WHERE ID(a) = ' . ($this->getParameterType($bolt) ? '{a}' : '$a') . ' RETURN COUNT(a)', [
140+
'a' => $created[0][1]
141+
]));
142+
$res = $bolt->pull();
143+
$this->assertIsArray($res);
144+
$this->assertEquals(0, $res[0][0]);
145+
}
146+
147+
/**
148+
* @var bool
149+
*/
150+
private static $parameterType;
151+
152+
/**
153+
* Because from Neo4j >= 4.0 is different placeholder for parameters
154+
* @param Bolt $bolt
155+
* @return bool
156+
*/
157+
private function getParameterType(Bolt $bolt): bool
158+
{
159+
if (self::$parameterType == null) {
160+
$this->assertNotFalse($bolt->run('call dbms.components() yield versions unwind versions as version return version'));
161+
$neo4jVersion = $bolt->pull()[0][0] ?? '';
162+
$this->assertNotEmpty($neo4jVersion);
163+
self::$parameterType = version_compare($neo4jVersion, '4') == -1;
164+
}
165+
166+
return self::$parameterType;
167+
}
168+
169+
}

0 commit comments

Comments
 (0)