|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Neo4j\QueryAPI\Tests\Unit\srcfiles; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Neo4j\QueryAPI\Configuration; |
| 7 | +use Nyholm\Psr7\Factory\Psr17Factory; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Psr\Http\Message\RequestInterface; |
| 10 | +use Psr\Http\Message\RequestFactoryInterface; |
| 11 | +use Psr\Http\Message\StreamFactoryInterface; |
| 12 | +use GuzzleHttp\Psr7\Request; |
| 13 | +use GuzzleHttp\Psr7\Utils; |
| 14 | +use Neo4j\QueryAPI\Neo4jRequestFactory; |
| 15 | +use Neo4j\QueryAPI\Objects\Authentication; |
| 16 | +use RuntimeException; |
| 17 | + |
| 18 | +/** |
| 19 | + * @api |
| 20 | + */ |
| 21 | +class Neo4jRequestFactoryUnitTest extends TestCase |
| 22 | +{ |
| 23 | + /** @psalm-suppress PropertyNotSetInConstructor */ |
| 24 | + private RequestFactoryInterface&\PHPUnit\Framework\MockObject\MockObject $psr17Factory; |
| 25 | + |
| 26 | + /** @psalm-suppress PropertyNotSetInConstructor */ |
| 27 | + private StreamFactoryInterface&\PHPUnit\Framework\MockObject\MockObject $streamFactory; |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + private string $address = ''; |
| 32 | + private string $authHeader = ''; |
| 33 | + |
| 34 | + /** |
| 35 | + * @throws Exception |
| 36 | + */ |
| 37 | + #[\Override] |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + $this->psr17Factory = $this->createMock(RequestFactoryInterface::class); |
| 42 | + $this->streamFactory = $this->createMock(StreamFactoryInterface::class); |
| 43 | + |
| 44 | + $address = getenv('NEO4J_ADDRESS'); |
| 45 | + $this->address = is_string($address) ? $address : ''; |
| 46 | + |
| 47 | + $auth = Authentication::fromEnvironment(); |
| 48 | + $this->authHeader = $auth->getHeader(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test for buildRunQueryRequest |
| 53 | + */ |
| 54 | + public function testBuildRunQueryRequest(): void |
| 55 | + { |
| 56 | + $cypher = 'MATCH (n) RETURN n'; |
| 57 | + $parameters = ['param1' => 'value1']; |
| 58 | + $database = 'neo4j'; |
| 59 | + |
| 60 | + $payload = json_encode([ |
| 61 | + 'statement' => $cypher, |
| 62 | + 'parameters' => $parameters, |
| 63 | + 'includeCounters' => true, |
| 64 | + ]); |
| 65 | + $uri = "{$this->address}/db/{$database}/query/v2"; |
| 66 | + |
| 67 | + $mockRequest = new Request('POST', $uri); |
| 68 | + $mockStream = Utils::streamFor($payload); |
| 69 | + |
| 70 | + $this->streamFactory->method('createStream') |
| 71 | + ->willReturn($mockStream); |
| 72 | + |
| 73 | + $this->psr17Factory->method('createRequest') |
| 74 | + ->willReturn($mockRequest); |
| 75 | + |
| 76 | + $factory = new Neo4jRequestFactory( |
| 77 | + $this->psr17Factory, |
| 78 | + $this->streamFactory, |
| 79 | + new Configuration($this->address), |
| 80 | + Authentication::fromEnvironment(), |
| 81 | + ); |
| 82 | + $request = $factory->buildRunQueryRequest($cypher, $parameters); |
| 83 | + |
| 84 | + $this->assertEquals('POST', $request->getMethod()); |
| 85 | + $this->assertEquals($uri, (string) $request->getUri()); |
| 86 | + $payload = json_encode([]); |
| 87 | + if ($payload === false) { |
| 88 | + throw new RuntimeException('JSON encoding failed: ' . json_last_error_msg()); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Test for buildBeginTransactionRequest |
| 95 | + */ |
| 96 | + public function testBuildBeginTransactionRequest(): void |
| 97 | + { |
| 98 | + $database = 'neo4j'; |
| 99 | + $uri = "{$this->address}/db/{$database}/query/v2/tx"; |
| 100 | + |
| 101 | + $mockRequest = new Request('POST', $uri); |
| 102 | + $mockStream = Utils::streamFor(''); |
| 103 | + |
| 104 | + $this->streamFactory->method('createStream') |
| 105 | + ->willReturn($mockStream); |
| 106 | + |
| 107 | + $this->psr17Factory->method('createRequest') |
| 108 | + ->willReturn($mockRequest); |
| 109 | + |
| 110 | + $factory = new Neo4jRequestFactory( |
| 111 | + $this->psr17Factory, |
| 112 | + $this->streamFactory, |
| 113 | + new Configuration($this->address), |
| 114 | + Authentication::fromEnvironment(), |
| 115 | + ); |
| 116 | + $request = $factory->buildBeginTransactionRequest(); |
| 117 | + |
| 118 | + $this->assertEquals('POST', $request->getMethod()); |
| 119 | + $this->assertEquals($uri, (string) $request->getUri()); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Test for buildCommitRequest |
| 124 | + */ |
| 125 | + public function testBuildCommitRequest(): void |
| 126 | + { |
| 127 | + $database = 'neo4j'; |
| 128 | + $transactionId = '12345'; |
| 129 | + $uri = "{$this->address}/db/{$database}/query/v2/tx/{$transactionId}/commit"; |
| 130 | + |
| 131 | + $mockRequest = new Request('POST', $uri); |
| 132 | + $mockStream = Utils::streamFor(''); |
| 133 | + |
| 134 | + $this->streamFactory->method('createStream') |
| 135 | + ->willReturn($mockStream); |
| 136 | + |
| 137 | + $this->psr17Factory->method('createRequest') |
| 138 | + ->willReturn($mockRequest); |
| 139 | + |
| 140 | + $factory = new Neo4jRequestFactory( |
| 141 | + $this->psr17Factory, |
| 142 | + $this->streamFactory, |
| 143 | + new Configuration($this->address), |
| 144 | + Authentication::fromEnvironment(), |
| 145 | + ); |
| 146 | + $request = $factory->buildCommitRequest($database, $transactionId); |
| 147 | + |
| 148 | + $this->assertEquals('POST', $request->getMethod()); |
| 149 | + $this->assertEquals($uri, (string) $request->getUri()); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Test for buildRollbackRequest |
| 154 | + */ |
| 155 | + public function testBuildRollbackRequest(): void |
| 156 | + { |
| 157 | + $database = 'neo4j'; |
| 158 | + $transactionId = '12345'; |
| 159 | + $uri = "{$this->address}/db/{$database}/query/v2/tx/{$transactionId}/rollback"; |
| 160 | + |
| 161 | + $mockRequest = new Request('POST', $uri); |
| 162 | + $mockStream = Utils::streamFor(''); |
| 163 | + |
| 164 | + $this->streamFactory->method('createStream') |
| 165 | + ->willReturn($mockStream); |
| 166 | + |
| 167 | + $this->psr17Factory->method('createRequest') |
| 168 | + ->willReturn($mockRequest); |
| 169 | + |
| 170 | + $factory = new Neo4jRequestFactory( |
| 171 | + $this->psr17Factory, |
| 172 | + $this->streamFactory, |
| 173 | + new Configuration($this->address), |
| 174 | + Authentication::fromEnvironment(), |
| 175 | + ); |
| 176 | + $request = $factory->buildRollbackRequest($database, $transactionId); |
| 177 | + |
| 178 | + $this->assertEquals('DELETE', $request->getMethod()); |
| 179 | + $this->assertEquals($uri, (string) $request->getUri()); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test for createRequest method with headers and body |
| 184 | + */ |
| 185 | + public function testCreateRequestWithHeadersAndBody(): void |
| 186 | + { |
| 187 | + $cypher = 'MATCH (n) RETURN n'; |
| 188 | + $parameters = ['param1' => 'value1']; |
| 189 | + $database = 'neo4j'; |
| 190 | + $uri = "{$this->address}/db/{$database}/query/v2"; |
| 191 | + |
| 192 | + $payload = json_encode([ |
| 193 | + 'statement' => $cypher, |
| 194 | + 'parameters' => $parameters, |
| 195 | + 'includeCounters' => true, |
| 196 | + ]); |
| 197 | + |
| 198 | + $mockStream = Utils::streamFor($payload); |
| 199 | + $this->streamFactory->method('createStream') |
| 200 | + ->willReturn($mockStream); |
| 201 | + |
| 202 | + $mockRequest = new Request('POST', $uri); |
| 203 | + $this->psr17Factory->method('createRequest') |
| 204 | + ->willReturn($mockRequest); |
| 205 | + |
| 206 | + $factory = new Neo4jRequestFactory( |
| 207 | + $this->psr17Factory, |
| 208 | + $this->streamFactory, |
| 209 | + new Configuration($this->address), |
| 210 | + Authentication::fromEnvironment(), |
| 211 | + ); |
| 212 | + |
| 213 | + $request = $factory->buildRunQueryRequest($cypher, $parameters); |
| 214 | + |
| 215 | + $this->assertEquals('application/json', $request->getHeaderLine('Content-Type')); |
| 216 | + $this->assertEquals('application/vnd.neo4j.query', $request->getHeaderLine('Accept')); |
| 217 | + $this->assertEquals($this->authHeader, $request->getHeaderLine('Authorization')); |
| 218 | + $payload = json_encode([]); |
| 219 | + if ($payload === false) { |
| 220 | + throw new RuntimeException('JSON encoding failed: ' . json_last_error_msg()); |
| 221 | + } |
| 222 | + |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Test createRequest without Authorization header |
| 227 | + */ |
| 228 | + public function testCreateRequestWithoutAuthorizationHeader(): void |
| 229 | + { |
| 230 | + $cypher = 'MATCH (n) RETURN n'; |
| 231 | + $parameters = ['param1' => 'value1']; |
| 232 | + $database = 'neo4j'; |
| 233 | + $uri = "{$this->address}/db/{$database}/query/v2"; |
| 234 | + |
| 235 | + $payload = json_encode([ |
| 236 | + 'statement' => $cypher, |
| 237 | + 'parameters' => $parameters, |
| 238 | + 'includeCounters' => true, |
| 239 | + ]); |
| 240 | + |
| 241 | + $mockStream = Utils::streamFor($payload); |
| 242 | + $this->streamFactory->method('createStream') |
| 243 | + ->willReturn($mockStream); |
| 244 | + |
| 245 | + $mockRequest = new Request('POST', $uri); |
| 246 | + $this->psr17Factory->method('createRequest') |
| 247 | + ->willReturn($mockRequest); |
| 248 | + |
| 249 | + $factory = new Neo4jRequestFactory( |
| 250 | + $this->psr17Factory, |
| 251 | + $this->streamFactory, |
| 252 | + new Configuration($this->address), |
| 253 | + Authentication::noAuth(), |
| 254 | + ); |
| 255 | + |
| 256 | + $request = $factory->buildRunQueryRequest($cypher, $parameters); |
| 257 | + $this->assertEquals('application/json', $request->getHeaderLine('Content-Type')); |
| 258 | + $this->assertEquals('application/vnd.neo4j.query', $request->getHeaderLine('Accept')); |
| 259 | + $this->assertEmpty($request->getHeaderLine('Authorization')); |
| 260 | + $payload = json_encode([]); |
| 261 | + if ($payload === false) { |
| 262 | + throw new RuntimeException('JSON encoding failed: ' . json_last_error_msg()); |
| 263 | + } |
| 264 | + } |
| 265 | +} |
0 commit comments