Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -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\/Unit\/Neo4jQueryAPIUnitTest.php":"54be7f7b0f9dcf13d62c4912127583b9","tests\/Integration\/Neo4jQueryAPIIntegrationTest.php":"44977ffd6c09c505b00c8ef4857b8bfd","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\/Transaction.php":"e456922858b31d87b17ca47d25d58474","tests\/resources\/expected\/complex-query-profile.php":"cc2b1e7e731c30a8855d9fa368cd55f3","src\/Neo4jQueryAPI.php":"8bffb787a834b58523e89fc9e5c19fe3","src\/Objects\/ProfiledQueryPlan.php":"d9ba608f3177426ea34d73276d75f20b"}}
{"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\/Transaction.php":"e456922858b31d87b17ca47d25d58474","tests\/resources\/expected\/complex-query-profile.php":"cc2b1e7e731c30a8855d9fa368cd55f3","src\/Objects\/ProfiledQueryPlan.php":"d9ba608f3177426ea34d73276d75f20b","tests\/Unit\/AuthenticationTest.php":"e4fb1f55a984d75f229102ddf6a9350f","src\/Objects\/Authentication.php":"ea428c99aae634b69a5843fe523a016d","src\/Objects\/php your-script.php":"63c3a9abbf4774d1da8c5f3c9f8f455e","tests\/Unit\/Neo4jQueryAPIUnitTest.php":"cea0eed7cb66b5d5dec405d6f83c0669","tests\/Integration\/Neo4jQueryAPIIntegrationTest.php":"c04106facdd3b520cabe5e8b8c74ca10","src\/Neo4jQueryAPI.php":"8c57c191311e777d37b83eb63f624fb2"}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this from git history. You can do this by adding the file to .gitignore and then running git rm --cached .php-cs-fixer.cache

7 changes: 5 additions & 2 deletions src/Neo4jQueryAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Exception;
use GuzzleHttp\Client;
use Neo4j\QueryAPI\Objects\Auth;
use Neo4j\QueryAPI\Objects\Authentication;
use Neo4j\QueryAPI\Objects\ProfiledQueryPlanArguments;
use Neo4j\QueryAPI\Objects\ResultCounters;
use Neo4j\QueryAPI\Objects\ProfiledQueryPlan;
Expand All @@ -24,13 +26,13 @@ public function __construct(Client $client)
$this->client = $client;
}

public static function login(string $address, string $username, string $password): self
public static function login(string $address, Authentication $auth): self
{
$client = new Client([
'base_uri' => rtrim($address, '/'),
'timeout' => 10.0,
'headers' => [
'Authorization' => 'Basic ' . base64_encode("$username:$password"),
'Authorization' => $auth->getHeader(),
'Content-Type' => 'application/vnd.neo4j.query',
'Accept' => 'application/vnd.neo4j.query',
],
Expand All @@ -39,6 +41,7 @@ public static function login(string $address, string $username, string $password
return new self($client);
}


/**
* @throws Neo4jException
* @throws RequestExceptionInterface
Expand Down
56 changes: 56 additions & 0 deletions src/Objects/Authentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Neo4j\QueryAPI\Objects;

use InvalidArgumentException;

class Authentication
{
private string $header;
private string $type;

private function __construct(string $header, string $type)
{
$this->header = $header;
$this->type = $type;
}

public static function request(string $username = null, string $password = null, string $token = null): self
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very great and convenient! But maybe we can improve the name by calling it for example: fromEnvironment or similar

{
if ($token !== null) {
return self::bearer($token);
}
if ($username === null) {
$username = getenv('NEO4J_USERNAME');
}

if ($password === null) {
$password = getenv('NEO4J_PASSWORD');
}
if ($username !== null && $password !== null) {
return self::basic($username, $password);
}

throw new InvalidArgumentException("Both username and password cannot be null.");
}

private static function basic(string $username, string $password): self
{
return new self("Basic " . base64_encode("$username:$password"), 'Basic');
}

private static function bearer(string $token): self
{
return new self("Bearer $token", 'Bearer');
}

public function getHeader(): string
{
return $this->header; // Return the header string directly
}

public function getType(): string
{
return $this->type;
}
}
20 changes: 20 additions & 0 deletions src/Objects/php your-script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Neo4j\QueryAPI\Objects\Authentication;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this class


require_once 'Authentication.php'; // Assuming your Authentication class is in Authentication.php

// Using Bearer Token Authentication
$authBearer = Authentication::create([
'token' => 'yourBearerToken'
]);

print_r($authBearer->getHeader());

// Using Basic Authentication
$authBasic = Authentication::create([
'username' => '',
'password' => ''
]);

print_r($authBasic->getHeader());
11 changes: 9 additions & 2 deletions tests/Integration/Neo4jQueryAPIIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GuzzleHttp\Psr7\Response;
use Neo4j\QueryAPI\Exception\Neo4jException;
use Neo4j\QueryAPI\Neo4jQueryAPI;
use Neo4j\QueryAPI\Objects\Authentication;
use Neo4j\QueryAPI\Objects\ProfiledQueryPlan;
use Neo4j\QueryAPI\Objects\Bookmarks;
use Neo4j\QueryAPI\Objects\ResultCounters;
Expand Down Expand Up @@ -37,20 +38,26 @@ public function setUp(): void

private function initializeApi(): Neo4jQueryAPI
{
$auth = Authentication::request(); // Automatically determines basic or bearer
return Neo4jQueryAPI::login(
getenv('NEO4J_ADDRESS'),
getenv('NEO4J_USERNAME'),
getenv('NEO4J_PASSWORD')
$auth
);
}



public function testCounters(): void
{
$result = $this->api->run('CREATE (x:Node {hello: "world"})');

$this->assertEquals(1, $result->getQueryCounters()->getNodesCreated());
}

/**
* @throws Neo4jException
* @throws RequestExceptionInterface
*/
public function testCreateBookmarks(): void
{
$result = $this->api->run(cypher: 'CREATE (x:Node {hello: "world"})');
Expand Down
58 changes: 58 additions & 0 deletions tests/Unit/AuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Neo4j\QueryAPI\Tests\Unit;

use Neo4j\QueryAPI\Objects\Authentication;
use PHPUnit\Framework\TestCase;
use InvalidArgumentException;

class AuthenticationTest extends TestCase
{
public function testBearerToken(): void
{
// Mock a bearer token
$mockToken = 'mocked_bearer_token';

// Create an Authentication instance with the bearer token
$auth = Authentication::request(token: $mockToken);

// Assert that the Authorization header is correct
$this->assertEquals("Bearer $mockToken", $auth->getHeader(), 'Bearer token mismatch.');
$this->assertEquals('Bearer', $auth->getType(), 'Type should be Bearer.');
}

public function testBasicAuthentication(): void
{
// Mock username and password
$mockUsername = 'mockUser';
$mockPassword = 'mockPass';

// Create an Authentication instance with username and password
$auth = Authentication::request($mockUsername, $mockPassword);

// Assert that the Authorization header is correct
$expectedHeader = 'Basic ' . base64_encode("$mockUsername:$mockPassword");
$this->assertEquals($expectedHeader, $auth->getHeader(), 'Basic authentication header mismatch.');
$this->assertEquals('Basic', $auth->getType(), 'Type should be Basic.');
}

public function testFallbackToEnvironmentVariables(): void
{
// Mock environment variables
putenv('NEO4J_USERNAME=mockEnvUser');
putenv('NEO4J_PASSWORD=mockEnvPass');

// Create an Authentication instance with environment variables
$auth = Authentication::request();

// Assert that the Authorization header is correct
$expectedHeader = 'Basic ' . base64_encode("mockEnvUser:mockEnvPass");
$this->assertEquals($expectedHeader, $auth->getHeader(), 'Basic authentication with environment variables mismatch.');
$this->assertEquals('Basic', $auth->getType(), 'Type should be Basic.');

// Cleanup the environment variables
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mind the excessive comment usage 😄

putenv('NEO4J_USERNAME');
putenv('NEO4J_PASSWORD');
}

}
24 changes: 19 additions & 5 deletions tests/Unit/Neo4jQueryAPIUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Neo4j\QueryAPI\Neo4jQueryAPI;
use Neo4j\QueryAPI\Objects\Authentication;
use Neo4j\QueryAPI\Objects\Bookmarks;
use Neo4j\QueryAPI\Objects\ResultCounters;
use Neo4j\QueryAPI\Results\ResultRow;
Expand All @@ -24,34 +25,47 @@ protected function setUp(): void
{
parent::setUp();

$this->address = getenv('NEO4J_ADDRESS');
$this->username = getenv('NEO4J_USERNAME');
$this->password = getenv('NEO4J_PASSWORD');
$this->address = getenv('NEO4J_ADDRESS') ;
$this->username = getenv('NEO4J_USERNAME') ;
$this->password = getenv('NEO4J_PASSWORD') ;
}

public function testCorrectClientSetup(): void
{
$neo4jQueryAPI = Neo4jQueryAPI::login($this->address, $this->username, $this->password);
// Verify Authentication object creation
$authentication = Authentication::request($this->username, $this->password);
$expectedAuthHeader = 'Basic ' . base64_encode("{$this->username}:{$this->password}");
$this->assertEquals($expectedAuthHeader, $authentication->getHeader(), 'Authentication header mismatch.');

// Use the updated login method
$neo4jQueryAPI = Neo4jQueryAPI::login($this->address, $authentication);

$this->assertInstanceOf(Neo4jQueryAPI::class, $neo4jQueryAPI);

// Use reflection to access private `client` property
$clientReflection = new \ReflectionClass(Neo4jQueryAPI::class);
$clientProperty = $clientReflection->getProperty('client');
// Ensure we can access private properties
$client = $clientProperty->getValue($neo4jQueryAPI);

$this->assertInstanceOf(Client::class, $client);

// Get the client's configuration and check headers
$config = $client->getConfig();
$this->assertEquals(rtrim($this->address, '/'), $config['base_uri']);
$this->assertEquals('Basic ' . base64_encode("{$this->username}:{$this->password}"), $config['headers']['Authorization']);
$this->assertArrayHasKey('Authorization', $config['headers'], 'Authorization header missing.');
$this->assertEquals($expectedAuthHeader, $config['headers']['Authorization'], 'Authorization header value mismatch.');
$this->assertEquals('application/vnd.neo4j.query', $config['headers']['Content-Type']);
$this->assertEquals('application/vnd.neo4j.query', $config['headers']['Accept']);
}


/**
* @throws GuzzleException
*/
public function testRunSuccess(): void
{
// Mock response for a successful query
$mock = new MockHandler([
new Response(200, ['X-Foo' => 'Bar'], '{"data": {"fields": ["hello"], "values": [[{"$type": "String", "_value": "world"}]]}}'),
]);
Expand Down
Loading