Skip to content

Commit 56ccc9e

Browse files
PratikshaPratiksha
authored andcommitted
winp
1 parent 1be6d06 commit 56ccc9e

File tree

8 files changed

+138
-53
lines changed

8 files changed

+138
-53
lines changed

.php-cs-fixer.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"8.3.6","version":"3.68.0:v3.68.0#73f78d8b2b34a0dd65fedb434a602ee4c2c8ad4c","indent":" ","lineEnding":"\n","rules":{"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_parentheses":true,"no_blank_lines_after_class_opening":true,"no_extra_blank_lines":{"tokens":["use"]},"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_space_around_construct":{"constructs_followed_by_a_single_space":["abstract","as","case","catch","class","const_import","do","else","elseif","final","finally","for","foreach","function","function_import","if","insteadof","interface","namespace","new","private","protected","public","static","switch","trait","try","use","use_lambda","while"],"constructs_preceded_by_a_single_space":["as","else","elseif","use_lambda"]},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"unary_operator_spaces":{"only_dec_inc":true},"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"attribute_placement":"ignore","on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"strict_param":true},"hashes":{"tests\/Unit\/Neo4jExceptionUnitTest.php":"0d7842780eeb9729d501831ee55df0d8","tests\/Unit\/ResultRowTest.php":"f5ee9f21d2439793a290e8ab946a7f32","tests\/Integration\/Neo4jOGMTest.php":"73136b2d28fbb4fa298467d1ab3e18c8","src\/OGM.php":"93aae9c7afc8dbfd5aa00bc1d264ad19","src\/Results\/ResultRow.php":"ad55ec1bd999a8f6ad6b18874c4017b5","src\/Results\/ResultSet.php":"5f7748a356bf0fb30403e3c5a411bd24","src\/Exception\/Neo4jException.php":"dfb0f6933b9d3913c5495ba6d801d5f1","src\/Objects\/Path.php":"88c95962a6316ba7aa2fa3f0f6e31627","src\/Objects\/Node.php":"4a8ab7b8bd1981ee4d35d8c52b81c7c3","src\/Objects\/ProfiledQueryPlanArguments.php":"1be7b230a034a72c13349a5670a34a2f","src\/Objects\/Person.php":"f2f469937660f5454761e4f31154e081","src\/Objects\/Point.php":"169715b2157e08482e420374e6ca4cc3","src\/Objects\/Bookmarks.php":"50f89ca88b2df817433ce8237ccc0f18","src\/Objects\/ResultCounters.php":"a9372c98fe7bede10cb004af30ea502f","src\/Objects\/Relationship.php":"f6347c0260780d4f5d2dc407dc97e25e","src\/Objects\/ProfiledQueryPlan.php":"d9ba608f3177426ea34d73276d75f20b","src\/Objects\/php your-script.php":"63c3a9abbf4774d1da8c5f3c9f8f455e","src\/Objects\/ResultSet.php":"f126eac07a2190797052d123971933be","tests\/resources\/expected\/complex-query-profile.php":"10f481c27e83c1478b5c0e3ad509ab26","src\/Transaction.php":"3e57e12e463749f8e3aabece091c91fc","tests\/Unit\/Neo4jQueryAPIUnitTest.php":"2d0183e377b28f6381e3a065592118c4","tests\/Unit\/AuthenticationTest.php":"58e41cacacf5f521fef5abc25ad4bc9f","src\/BearerAuthentication.php":"0c6a9ba4adc3b762a586f8d1d58e62dc","src\/NoAuth.php":"80e445bfbd33cc7b60036db9461e0706","src\/AuthenticateInterface.php":"36290631a54b09926af0d78af8fc7282","src\/BasicAuthentication.php":"efaae7442bf25f033548765fd06b70dd","src\/Objects\/Authentication.php":"de564835b80cf54171f42c3f5fccda28","src\/Neo4jPhp.php":"79006f2c26015a1a1335559a5f8fc73d","src\/transaction_Script.php":"411ce81fd25cd49a8ec18ec817e56b50","tests\/Unit\/Neo4jRequestFactoryTest.php":"29f3446c8a8e7d202d7f403a5e261019","tests\/Integration\/Neo4jQueryAPIIntegrationTest.php":"5fb6a97821b94444748ad41ce67f4cac","src\/Neo4jQueryAPI.php":"a07a7ae10a29f8ce8b4c997e9359cdfd","src\/Neo4jRequestFactory.php":"5349daa39d67fff3de4c750cbaaa8d66","src\/requestFactory.php":"6a11cd1e82952867c137949dadf99126"}}
1+
{"php":"8.3.6","version":"3.68.0:v3.68.0#73f78d8b2b34a0dd65fedb434a602ee4c2c8ad4c","indent":" ","lineEnding":"\n","rules":{"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_parentheses":true,"no_blank_lines_after_class_opening":true,"no_extra_blank_lines":{"tokens":["use"]},"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_space_around_construct":{"constructs_followed_by_a_single_space":["abstract","as","case","catch","class","const_import","do","else","elseif","final","finally","for","foreach","function","function_import","if","insteadof","interface","namespace","new","private","protected","public","static","switch","trait","try","use","use_lambda","while"],"constructs_preceded_by_a_single_space":["as","else","elseif","use_lambda"]},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"unary_operator_spaces":{"only_dec_inc":true},"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"attribute_placement":"ignore","on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"strict_param":true},"hashes":{"tests\/Unit\/Neo4jExceptionUnitTest.php":"0d7842780eeb9729d501831ee55df0d8","tests\/Unit\/ResultRowTest.php":"f5ee9f21d2439793a290e8ab946a7f32","tests\/Integration\/Neo4jOGMTest.php":"73136b2d28fbb4fa298467d1ab3e18c8","src\/OGM.php":"93aae9c7afc8dbfd5aa00bc1d264ad19","src\/Results\/ResultRow.php":"ad55ec1bd999a8f6ad6b18874c4017b5","src\/Results\/ResultSet.php":"5f7748a356bf0fb30403e3c5a411bd24","src\/Exception\/Neo4jException.php":"dfb0f6933b9d3913c5495ba6d801d5f1","src\/Objects\/Path.php":"88c95962a6316ba7aa2fa3f0f6e31627","src\/Objects\/Node.php":"4a8ab7b8bd1981ee4d35d8c52b81c7c3","src\/Objects\/ProfiledQueryPlanArguments.php":"1be7b230a034a72c13349a5670a34a2f","src\/Objects\/Person.php":"f2f469937660f5454761e4f31154e081","src\/Objects\/Point.php":"169715b2157e08482e420374e6ca4cc3","src\/Objects\/Bookmarks.php":"50f89ca88b2df817433ce8237ccc0f18","src\/Objects\/ResultCounters.php":"a9372c98fe7bede10cb004af30ea502f","src\/Objects\/Relationship.php":"f6347c0260780d4f5d2dc407dc97e25e","src\/Objects\/ProfiledQueryPlan.php":"d9ba608f3177426ea34d73276d75f20b","src\/Objects\/php your-script.php":"63c3a9abbf4774d1da8c5f3c9f8f455e","src\/Objects\/ResultSet.php":"f126eac07a2190797052d123971933be","tests\/resources\/expected\/complex-query-profile.php":"10f481c27e83c1478b5c0e3ad509ab26","src\/BearerAuthentication.php":"0c6a9ba4adc3b762a586f8d1d58e62dc","src\/NoAuth.php":"80e445bfbd33cc7b60036db9461e0706","src\/AuthenticateInterface.php":"36290631a54b09926af0d78af8fc7282","src\/BasicAuthentication.php":"efaae7442bf25f033548765fd06b70dd","src\/Neo4jPhp.php":"79006f2c26015a1a1335559a5f8fc73d","src\/transaction_Script.php":"411ce81fd25cd49a8ec18ec817e56b50","src\/Neo4jRequestFactory.php":"5349daa39d67fff3de4c750cbaaa8d66","src\/requestFactory.php":"6a11cd1e82952867c137949dadf99126","tests\/Integration\/Neo4jTransactionIntegrationTest.php":"d0ab77ac9d29a586cb1baa5166e50342","tests\/Unit\/Neo4jQueryAPIUnitTest.php":"140ec5dbeb6cc8a8c3af34eeef7f8c1f","tests\/Unit\/Neo4jRequestFactoryTest.php":"84c4a7b894f9a3a5f970c7e4b52aa855","tests\/Unit\/AuthenticationTest.php":"dff945f2d353108899f48633988fa2cd","tests\/Integration\/Neo4jQueryAPIIntegrationTest.php":"8debf487c851c9dff1bf386dab4a64f4","src\/Neo4jQueryAPI.php":"cd798d0ff7ea9acb4e01165ea19d3336","src\/Objects\/Authentication.php":"4e360980cb18aa4c57d3c35d8c98b55b","src\/Transaction.php":"fa4827d369368e93e50be75e8929016c"}}

src/Neo4jQueryAPI.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Neo4j\QueryAPI\Objects\ResultCounters;
1414
use Neo4j\QueryAPI\Objects\ResultSet;
1515
use Neo4j\QueryAPI\Results\ResultRow;
16+
use Psr\Http\Client\ClientExceptionInterface;
1617
use Psr\Http\Client\ClientInterface;
1718
use Psr\Http\Client\RequestExceptionInterface;
1819
use RuntimeException;
@@ -38,6 +39,7 @@ public static function login(string $address, AuthenticateInterface $auth = null
3839
'base_uri' => rtrim($address, '/'),
3940
'timeout' => 10.0,
4041
'headers' => [
42+
'Authorization' => $auth->getHeader(),
4143
'Content-Type' => 'application/vnd.neo4j.query',
4244
'Accept' => 'application/vnd.neo4j.query',
4345
],
@@ -162,9 +164,15 @@ private function handleException(RequestExceptionInterface $e): void
162164
throw $e;
163165
}
164166

167+
/**
168+
* @throws ClientExceptionInterface
169+
*/
165170
public function beginTransaction(string $database = 'neo4j'): Transaction
166171
{
167-
$response = $this->client->sendRequest(new Request('POST', '/db/neo4j/query/v2/tx'));
172+
$request = new Request('POST', '/db/neo4j/query/v2/tx');
173+
$request = $this->auth->authenticate($request);
174+
$request = $request->withHeader('Content-Type', 'application/json');
175+
$response = $this->client->sendRequest($request);
168176

169177
$clusterAffinity = $response->getHeaderLine('neo4j-cluster-affinity');
170178
$responseData = json_decode($response->getBody(), true);

src/Objects/Authentication.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
class Authentication
1212
{
13-
public static function basic(): AuthenticateInterface
13+
public static function basic(string $username, string $password): AuthenticateInterface
1414
{
15-
return new BasicAuthentication(getenv("NEO4J_USERNAME"), getenv("NEO4J_PASSWORD"));
15+
return new BasicAuthentication('neo4j', '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0');
1616
}
1717

1818

src/Transaction.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Neo4j\QueryAPI;
44

55
use Neo4j\QueryAPI\Exception\Neo4jException;
6+
use Neo4j\QueryAPI\Objects\Authentication;
67
use Neo4j\QueryAPI\Objects\Bookmarks;
78
use Neo4j\QueryAPI\Objects\ResultCounters;
89
use Neo4j\QueryAPI\Objects\ResultSet;
@@ -31,6 +32,7 @@ public function run(string $query, array $parameters): ResultSet
3132
{
3233
$response = $this->client->post("/db/neo4j/query/v2/tx/{$this->transactionId}", [
3334
'headers' => [
35+
'Authorization' => Authentication::basic('neo4j', '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0')->getheader(),
3436
'neo4j-cluster-affinity' => $this->clusterAffinity,
3537
],
3638
'json' => [
@@ -115,6 +117,7 @@ public function commit(): void
115117
{
116118
$this->client->post("/db/neo4j/query/v2/tx/{$this->transactionId}/commit", [
117119
'headers' => [
120+
'Authorization' => Authentication::basic('neo4j', '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0')->getheader(),
118121
'neo4j-cluster-affinity' => $this->clusterAffinity,
119122
],
120123
]);

tests/Integration/Neo4jQueryAPIIntegrationTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private function initializeApi(): Neo4jQueryAPI
3939
{
4040
return Neo4jQueryAPI::login(
4141
getenv('NEO4J_ADDRESS'),
42-
Authentication::basic(),
42+
Authentication::basic("neo4j", "9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0"),
4343
);
4444
}
4545

@@ -236,11 +236,6 @@ public function testChildQueryPlanExistence(): void
236236
}
237237
}
238238

239-
240-
241-
242-
243-
244239
/**
245240
* @throws GuzzleException
246241
*/
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Tests\Integration;
4+
5+
use Neo4j\QueryAPI\Neo4jRequestFactory;
6+
use Neo4j\QueryAPI\Objects\Authentication;
7+
use GuzzleHttp\Client;
8+
use GuzzleHttp\Exception\GuzzleException;
9+
use GuzzleHttp\Handler\MockHandler;
10+
use GuzzleHttp\HandlerStack;
11+
use GuzzleHttp\Psr7\Response;
12+
use Neo4j\QueryAPI\Exception\Neo4jException;
13+
use Neo4j\QueryAPI\Neo4jQueryAPI;
14+
use Neo4j\QueryAPI\Objects\Bookmarks;
15+
use Neo4j\QueryAPI\Objects\ProfiledQueryPlan;
16+
use Neo4j\QueryAPI\Objects\ResultCounters;
17+
use Neo4j\QueryAPI\Objects\ResultSet;
18+
use Neo4j\QueryAPI\Results\ResultRow;
19+
use PHPUnit\Framework\TestCase;
20+
use Psr\Http\Client\RequestExceptionInterface;
21+
22+
class Neo4jTransactionIntegrationTest extends TestCase
23+
{
24+
private Neo4jQueryAPI $api;
25+
26+
private Neo4jRequestFactory $request;
27+
28+
/**
29+
* @throws GuzzleException
30+
*/
31+
public function setUp(): void
32+
{
33+
$this->api = $this->initializeApi();
34+
// Clear database and populate test data
35+
$this->clearDatabase();
36+
$this->populateTestData();
37+
}
38+
39+
private function initializeApi(): Neo4jQueryAPI
40+
{
41+
return Neo4jQueryAPI::login(
42+
getenv('NEO4J_ADDRESS'),
43+
Authentication::basic("neo4j", "9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0"),
44+
);
45+
}
46+
47+
/**
48+
* @throws GuzzleException
49+
*/
50+
private function clearDatabase(): void
51+
{
52+
$this->api->run('MATCH (n) DETACH DELETE n', []);
53+
}
54+
55+
/**
56+
* @throws GuzzleException
57+
*/
58+
private function populateTestData(): void
59+
{
60+
$names = ['bob1', 'alicy'];
61+
foreach ($names as $name) {
62+
$this->api->run('CREATE (:Person {name: $name})', ['name' => $name]);
63+
}
64+
}
65+
public function testTransactionCommit(): void
66+
{
67+
// Begin a new transaction
68+
$tsx = $this->api->beginTransaction();
69+
70+
// Generate a random name for the node
71+
$name = (string)mt_rand(1, 100000);
72+
73+
// Create a node within the transaction
74+
$tsx->run("CREATE (x:Human {name: \$name})", ['name' => $name]);
75+
76+
// Validate that the node does not exist in the database before the transaction is committed
77+
$results = $this->api->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
78+
$this->assertCount(0, $results);
79+
80+
// Validate that the node exists within the transaction
81+
$results = $tsx->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
82+
$this->assertCount(1, $results);
83+
84+
// Commit the transaction
85+
$tsx->commit();
86+
87+
// Validate that the node now exists in the database
88+
$results = $this->api->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
89+
$this->assertCount(1, $results); // Updated to expect 1 result
90+
}
91+
92+
}

tests/Unit/AuthenticationTest.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Neo4j\QueryAPI\Tests\Unit;
44

5+
use Neo4j\QueryAPI\BasicAuthentication;
56
use Neo4j\QueryAPI\Objects\Authentication;
7+
use PHPUnit\Framework\MockObject\Exception;
68
use PHPUnit\Framework\TestCase;
9+
use Psr\Http\Message\RequestInterface;
710

811
class AuthenticationTest extends TestCase
912
{
@@ -20,45 +23,31 @@ public function testBearerToken(): void
2023
$this->assertEquals('Bearer', $auth->getType(), 'Type should be Bearer.');
2124
}
2225

26+
/**
27+
* @throws Exception
28+
*/
2329
public function testBasicAuthentication(): void
2430
{
25-
// Mocked username and password
26-
$mockUsername = 'mockUser';
27-
$mockPassword = 'mockPass';
31+
$mockUsername = 'neo4j';
32+
$mockPassword = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
2833

29-
// Mock environment variables to return the mocked values
30-
putenv('NEO4J_USERNAME=' . $mockUsername);
31-
putenv('NEO4J_PASSWORD=' . $mockPassword);
34+
$auth = Authentication::basic($mockUsername, $mockPassword);
3235

33-
// Use Authentication::basic() to get the Basic authentication instance
34-
$auth = Authentication::basic();
36+
$this->assertInstanceOf(BasicAuthentication::class, $auth);
3537

36-
// Assert: Ensure correct Basic auth header is generated
3738
$expectedHeader = 'Basic ' . base64_encode("$mockUsername:$mockPassword");
38-
$this->assertEquals($expectedHeader, $auth->getHeader(), 'Basic authentication header mismatch.');
39-
$this->assertEquals('Basic', $auth->getType(), 'Type should be Basic.');
4039

41-
// Clean up: Remove environment variables after the test
42-
putenv('NEO4J_USERNAME');
43-
putenv('NEO4J_PASSWORD');
44-
}
45-
46-
public function testFallbackToEnvironmentVariables(): void
47-
{
48-
// Mock environment variables for Neo4j username and password
49-
putenv('NEO4J_USERNAME=mockEnvUser');
50-
putenv('NEO4J_PASSWORD=mockEnvPass');
40+
$request = $this->createMock(RequestInterface::class);
5141

52-
// Use Authentication::basic() to get the Basic authentication instance
53-
$auth = Authentication::basic();
42+
$request->expects($this->once())
43+
->method('withHeader')
44+
->with('Authorization', $expectedHeader) // Use dynamically generated expected header
45+
->willReturn($request);
5446

55-
// Assert: Ensure that the correct Basic authentication header is generated
56-
$expectedHeader = 'Basic ' . base64_encode("mockEnvUser:mockEnvPass");
57-
$this->assertEquals($expectedHeader, $auth->getHeader(), 'Basic authentication with environment variables mismatch.');
58-
$this->assertEquals('Basic', $auth->getType(), 'Type should be Basic.');
47+
$auth->authenticate($request);
5948

60-
// Clean up environment variables
61-
putenv('NEO4J_USERNAME');
62-
putenv('NEO4J_PASSWORD');
49+
$this->assertEquals($expectedHeader, $auth->getHeader());
50+
$this->assertEquals('Basic', $auth->getType());
6351
}
52+
6453
}

0 commit comments

Comments
 (0)