77
88/**
99 * Class BoltTest
10+ *
1011 * @author Michal Stefanak
1112 * @link https://github.com/stefanak-michal/Bolt
13+ *
1214 * @covers \Bolt\Bolt
15+ * @covers \Bolt\Socket
16+ *
1317 * @package Bolt\tests
1418 * @requires PHP >= 7.1
1519 * @requires extension sockets
1822class BoltTest extends TestCase
1923{
2024
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-
3125 /**
32- * @return Bolt
33- * @throws \Exception
26+ * @return Bolt|null
3427 */
35- public function test__construct ()
28+ public function testHello (): Bolt
3629 {
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- }
30+ try {
31+ $ bolt = new Bolt ($ GLOBALS ['NEO_HOST ' ] ?? '127.0.0.1 ' , $ GLOBALS ['NEO_PORT ' ] ?? 7687 );
32+ $ this ->assertInstanceOf (Bolt::class, $ bolt );
33+ $ this ->assertTrue ($ bolt ->hello ('Test/1.0 ' , $ GLOBALS ['NEO_USER ' ], $ GLOBALS ['NEO_PASS ' ]));
34+ return $ bolt ;
35+ } catch (\Exception $ e ) {
36+ $ this ->markTestSkipped ($ e ->getMessage ());
37+ }
4138
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 ;
39+ return null ;
5240 }
5341
5442 /**
55- * @depends testInit
43+ * @depends testHello
5644 * @param Bolt $bolt
57- * @return Bolt
5845 */
59- public function testRun (Bolt $ bolt )
46+ public function testPull (Bolt $ bolt )
6047 {
6148 $ res = $ bolt ->run ('RETURN 1 AS num, 2 AS cnt ' );
6249 $ this ->assertIsArray ($ res );
6350 $ this ->assertArrayHasKey ('fields ' , $ res );
64- return $ bolt ;
65- }
6651
67- /**
68- * @depends testRun
69- * @param Bolt $bolt
70- * @return Bolt
71- */
72- public function testPull (Bolt $ bolt )
73- {
7452 $ res = $ bolt ->pull ();
7553 $ this ->assertEquals (1 , $ res [0 ][0 ] ?? 0 );
7654 $ this ->assertEquals (2 , $ res [0 ][1 ] ?? 0 );
77- return $ bolt ;
7855 }
7956
8057 /**
81- * @depends testInit
58+ * @depends testHello
8259 * @param Bolt $bolt
8360 */
8461 public function testDiscard (Bolt $ bolt )
8562 {
86- //test discard
8763 $ this ->assertNotFalse ($ bolt ->run ('MATCH (a:Test) RETURN * ' ));
8864 $ this ->assertTrue ($ bolt ->discard ());
8965 }
9066
9167 /**
92- * @depends testInit
93- * @depends testPull
68+ * @depends testHello
9469 * @param Bolt $bolt
95- * @return int
9670 */
97- public function testNodeCreate (Bolt $ bolt )
71+ public function testNode (Bolt $ bolt )
9872 {
9973 $ this ->assertNotFalse ($ bolt ->run ('CREATE (a:Test) RETURN a, ID(a) ' ));
10074
10175 $ created = $ bolt ->pull ();
10276 $ this ->assertIsArray ($ created );
10377 $ this ->assertInstanceOf (\Bolt \structures \Node::class, $ created [0 ][0 ]);
104- return $ created [0 ][1 ];
105- }
10678
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
79+ $ this ->assertNotFalse ($ bolt ->run ('MATCH (a:Test) WHERE ID(a) = ' . $ this ->formatParameter ($ bolt , 'a ' ) . ' DELETE a ' , [
80+ 'a ' => $ created [0 ][1 ]
11881 ]));
11982 $ this ->assertEquals (1 , $ bolt ->pull ()[0 ]['stats ' ]['nodes-deleted ' ] ?? 0 );
12083 }
12184
12285 /**
123- * @depends testInit
86+ * @depends testHello
12487 * @param Bolt $bolt
12588 */
12689 public function testTransaction (Bolt $ bolt )
@@ -136,14 +99,31 @@ public function testTransaction(Bolt $bolt)
13699 $ this ->assertIsArray ($ created );
137100 $ this ->assertTrue ($ bolt ->rollback ());
138101
139- $ this ->assertNotFalse ($ bolt ->run ('MATCH (a:Test) WHERE ID(a) = ' . ( $ this ->getParameterType ($ bolt) ? ' {a} ' : ' $ a ' ) . ' RETURN COUNT(a) ' , [
102+ $ this ->assertNotFalse ($ bolt ->run ('MATCH (a:Test) WHERE ID(a) = ' . $ this ->formatParameter ($ bolt, ' a ' ) . ' RETURN COUNT(a) ' , [
140103 'a ' => $ created [0 ][1 ]
141104 ]));
142105 $ res = $ bolt ->pull ();
143106 $ this ->assertIsArray ($ res );
144107 $ this ->assertEquals (0 , $ res [0 ][0 ]);
145108 }
146109
110+ public function testError ()
111+ {
112+ $ this ->expectException (\Exception::class);
113+ Bolt::error ('test ' );
114+ }
115+
116+ public function testErrorHandler ()
117+ {
118+ $ tmp = '' ;
119+ Bolt::$ errorHandler = function ($ msg , $ code ) use (&$ tmp ) {
120+ $ tmp = $ msg ;
121+ };
122+ Bolt::error ('test ' );
123+ $ this ->assertEquals ('test ' , $ tmp );
124+ Bolt::$ errorHandler = null ;
125+ }
126+
147127 /**
148128 * @var bool
149129 */
@@ -152,9 +132,10 @@ public function testTransaction(Bolt $bolt)
152132 /**
153133 * Because from Neo4j >= 4.0 is different placeholder for parameters
154134 * @param Bolt $bolt
155- * @return bool
135+ * @param string $name
136+ * @return string
156137 */
157- private function getParameterType (Bolt $ bolt ): bool
138+ private function formatParameter (Bolt $ bolt, string $ name ): string
158139 {
159140 if (self ::$ parameterType == null ) {
160141 $ this ->assertNotFalse ($ bolt ->run ('call dbms.components() yield versions unwind versions as version return version ' ));
@@ -163,7 +144,7 @@ private function getParameterType(Bolt $bolt): bool
163144 self ::$ parameterType = version_compare ($ neo4jVersion , '4 ' ) == -1 ;
164145 }
165146
166- return self ::$ parameterType ;
147+ return self ::$ parameterType ? ( ' { ' . $ name . ' } ' ) : ( ' $ ' . $ name ) ;
167148 }
168149
169150}
0 commit comments