|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bolt\tests; |
| 4 | + |
| 5 | +use Bolt\protocol\IStructure; |
| 6 | +use Bolt\Bolt; |
| 7 | +use Bolt\protocol\AProtocol; |
| 8 | +use Bolt\protocol\v1\structures\{ |
| 9 | + Date, |
| 10 | + Duration, |
| 11 | + LocalTime, |
| 12 | + LocalDateTime |
| 13 | +}; |
| 14 | +use Bolt\enum\Signature; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class MemgraphTest |
| 19 | + * |
| 20 | + * @author Michal Stefanak |
| 21 | + * @link https://github.com/neo4j-php/Bolt |
| 22 | + * @package Bolt\tests |
| 23 | + */ |
| 24 | +class MemgraphTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @return AProtocol |
| 28 | + */ |
| 29 | + public function testConnection(): AProtocol |
| 30 | + { |
| 31 | + if (!extension_loaded('sockets')) |
| 32 | + $this->markTestSkipped('Sockets extension not available'); |
| 33 | + |
| 34 | + $conn = new \Bolt\connection\Socket('127.0.0.1', 7687, 3); |
| 35 | + $this->assertInstanceOf(\Bolt\connection\Socket::class, $conn); |
| 36 | + |
| 37 | + $bolt = new Bolt($conn); |
| 38 | + $this->assertInstanceOf(Bolt::class, $bolt); |
| 39 | + |
| 40 | + $protocol = $bolt->setProtocolVersions(5.2, 4.3, 4.1, 4.0)->build(); |
| 41 | + $this->assertInstanceOf(AProtocol::class, $protocol); |
| 42 | + |
| 43 | + if (version_compare($protocol->getVersion(), '5.2', '>=')) { |
| 44 | + $protocol->hello()->getResponse(); |
| 45 | + $protocol->logon(['scheme' => 'none'])->getResponse(); |
| 46 | + } else { |
| 47 | + $protocol->hello(['scheme' => 'none'])->getResponse(); |
| 48 | + } |
| 49 | + |
| 50 | + return $protocol; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Basic query test with basic data types |
| 55 | + * @depends testConnection |
| 56 | + * @param AProtocol $protocol |
| 57 | + */ |
| 58 | + public function testQuery(AProtocol $protocol): void |
| 59 | + { |
| 60 | + $params = [ |
| 61 | + 'number' => 123, |
| 62 | + 'string' => 'abc', |
| 63 | + 'null' => null, |
| 64 | + 'bool' => true, |
| 65 | + 'float' => 0.4591563, |
| 66 | + 'list' => [1, 2, 3], |
| 67 | + 'dictionary' => ['a' => 1, 'b' => 2, 'c' => 3] |
| 68 | + ]; |
| 69 | + |
| 70 | + $query = implode(', ', array_map(function (string $key) { |
| 71 | + return '$' . $key . ' AS ' . $key; |
| 72 | + }, array_keys($params))); |
| 73 | + |
| 74 | + $runResponse = $protocol->run('RETURN ' . $query, $params)->getResponse(); |
| 75 | + $this->assertEquals(Signature::SUCCESS, $runResponse->signature); |
| 76 | + |
| 77 | + $pullResponses = iterator_to_array($protocol->pull()->getResponses(), false); |
| 78 | + $this->assertCount(2, $pullResponses); |
| 79 | + $this->assertEquals(Signature::RECORD, $pullResponses[0]->signature); |
| 80 | + $this->assertEquals(Signature::SUCCESS, $pullResponses[1]->signature); |
| 81 | + |
| 82 | + $this->assertEquals($params, array_combine($runResponse->content['fields'], $pullResponses[0]->content)); |
| 83 | + } |
| 84 | + /** |
| 85 | + * Test transaction handling |
| 86 | + * @depends testConnection |
| 87 | + * @param AProtocol $protocol |
| 88 | + */ |
| 89 | + public function testTransaction(AProtocol $protocol): void |
| 90 | + { |
| 91 | + if (version_compare($protocol->getVersion(), 3, '<')) { |
| 92 | + $this->markTestSkipped('Old Memgraph version does not support transactions'); |
| 93 | + } |
| 94 | + |
| 95 | + $res = iterator_to_array( |
| 96 | + $protocol |
| 97 | + ->begin() |
| 98 | + ->run('CREATE (a:Test) RETURN a, ID(a)') |
| 99 | + ->pull() |
| 100 | + ->rollback() |
| 101 | + ->getResponses(), |
| 102 | + false |
| 103 | + ); |
| 104 | + |
| 105 | + $id = $res[2]->content[1]; |
| 106 | + $this->assertIsInt($id); |
| 107 | + |
| 108 | + $res = iterator_to_array( |
| 109 | + $protocol |
| 110 | + ->run('MATCH (a:Test) WHERE ID(a) = ' |
| 111 | + . (version_compare($protocol->getVersion(), 4, '<') ? '{a}' : '$a') |
| 112 | + . ' RETURN COUNT(a)', [ |
| 113 | + 'a' => $id |
| 114 | + ]) |
| 115 | + ->pull() |
| 116 | + ->getResponses(), |
| 117 | + false |
| 118 | + ); |
| 119 | + |
| 120 | + $this->assertEquals(0, $res[1]->content[0]); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Test additional data types |
| 125 | + * @depends testConnection |
| 126 | + * @dataProvider structureProvider |
| 127 | + * @param IStructure $structure |
| 128 | + * @param AProtocol $protocol |
| 129 | + */ |
| 130 | + public function testStructure(IStructure $structure, AProtocol $protocol): void |
| 131 | + { |
| 132 | + $responses = iterator_to_array( |
| 133 | + $protocol |
| 134 | + ->run('RETURN $s', [ |
| 135 | + 's' => $structure |
| 136 | + ]) |
| 137 | + ->pull() |
| 138 | + ->getResponses(), |
| 139 | + false |
| 140 | + ); |
| 141 | + |
| 142 | + $this->assertInstanceOf(get_class($structure), $responses[1]->content[0]); |
| 143 | + $this->assertEquals((string)$structure, (string)$responses[1]->content[0]); |
| 144 | + } |
| 145 | + |
| 146 | + public function structureProvider(): \Generator |
| 147 | + { |
| 148 | + yield 'Duration' => [new Duration(0, 4, 3, 2)]; |
| 149 | + yield 'Date' => [new Date(intval(floor(time() / 86400)))]; |
| 150 | + yield 'LocalTime' => [new LocalTime(intval(microtime(true) * 1000))]; |
| 151 | + yield 'LocalDateTime' => [new LocalDateTime(time(), 1234)]; |
| 152 | + } |
| 153 | +} |
0 commit comments