66use GuzzleHttp \Exception \GuzzleException ;
77use GuzzleHttp \Handler \MockHandler ;
88use GuzzleHttp \HandlerStack ;
9+ use GuzzleHttp \Psr7 \Request ;
910use GuzzleHttp \Psr7 \Response ;
1011use Neo4j \QueryAPI \Neo4jQueryAPI ;
1112use Neo4j \QueryAPI \Objects \Authentication ;
1213use Neo4j \QueryAPI \Objects \Bookmarks ;
1314use Neo4j \QueryAPI \Objects \ResultCounters ;
1415use Neo4j \QueryAPI \Objects \ResultSet ;
1516use Neo4j \QueryAPI \Results \ResultRow ;
17+ use Neo4j \QueryAPI \AuthenticateInterface ;
1618use PHPUnit \Framework \TestCase ;
1719
1820class Neo4jQueryAPIUnitTest extends TestCase
1921{
2022 protected string $ address ;
21- protected string $ username ;
22- protected string $ password ;
2323
2424 protected function setUp (): void
2525 {
@@ -31,49 +31,65 @@ private function initializeApi(): Neo4jQueryAPI
3131 {
3232 return Neo4jQueryAPI::login (
3333 $ this ->address ,
34- Authentication::basic ( " neo4j " , " 9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0 " )
34+ Authentication::fromEnvironment ( )
3535 );
3636 }
3737
3838 public function testCorrectClientSetup (): void
3939 {
40+
4041 $ neo4jQueryAPI = $ this ->initializeApi ();
4142
4243 $ clientReflection = new \ReflectionClass (Neo4jQueryAPI::class);
44+
45+
4346 $ clientProperty = $ clientReflection ->getProperty ('client ' );
4447 $ client = $ clientProperty ->getValue ($ neo4jQueryAPI );
45-
4648 $ this ->assertInstanceOf (Client::class, $ client );
4749
48- $ config = $ client ->getConfig ();
49- $ expectedAuthHeader = 'Basic ' . base64_encode ('neo4j:9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0 ' );
50+ $ authProperty = $ clientReflection ->getProperty ('auth ' );
51+ $ auth = $ authProperty ->getValue ($ neo4jQueryAPI );
52+ $ this ->assertInstanceOf (AuthenticateInterface::class, $ auth );
53+
54+
55+ $ expectedAuth = Authentication::fromEnvironment ();
56+ $ this ->assertEquals ($ expectedAuth ->getHeader (), $ auth ->getHeader (), 'Authentication headers mismatch ' );
57+
58+ $ request = new Request ('GET ' , '/test-endpoint ' );
59+ $ authenticatedRequest = $ auth ->authenticate ($ request );
60+
5061
51- // $this->assertEquals(rtrim($this->address, '/'), $config['base_uri']);
52- // $this->assertArrayHasKey('Authorization', $config['headers'], 'Authorization header missing.');
53- // $this->assertEquals($expectedAuthHeader, $config['headers']['Authorization'], 'Authorization header value mismatch.');
54- $ this ->assertEquals ('application/vnd.neo4j.query ' , $ config ['headers ' ]['Content-Type ' ]);
62+ $ expectedAuthHeader = 'Basic ' . base64_encode (getenv ("NEO4J_USERNAME " ) . ': ' . getenv ("NEO4J_PASSWORD " ));
63+ $ this ->assertEquals ($ expectedAuthHeader , $ authenticatedRequest ->getHeaderLine ('Authorization ' ));
64+
65+ $ requestWithHeaders = $ authenticatedRequest ->withHeader ('Content-Type ' , 'application/vnd.neo4j.query ' );
66+ $ this ->assertEquals ('application/vnd.neo4j.query ' , $ requestWithHeaders ->getHeaderLine ('Content-Type ' ));
5567 }
5668
5769 /**
5870 * @throws GuzzleException
5971 */
6072 public function testRunSuccess (): void
6173 {
62-
6374 $ mock = new MockHandler ([
6475 new Response (200 , ['X-Foo ' => 'Bar ' ], '{"data": {"fields": ["hello"], "values": [[{"$type": "String", "_value": "world"}]]}} ' ),
6576 ]);
6677
67- $ auth = Authentication::basic ("neo4j " , "9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0 " );
68-
78+ $ auth = Authentication::fromEnvironment ();
6979 $ handlerStack = HandlerStack::create ($ mock );
7080 $ client = new Client (['handler ' => $ handlerStack ]);
7181
7282 $ neo4jQueryAPI = new Neo4jQueryAPI ($ client , $ auth );
7383
7484 $ cypherQuery = 'MATCH (n:Person) RETURN n LIMIT 5 ' ;
75-
7685 $ result = $ neo4jQueryAPI ->run ($ cypherQuery );
77- $ this ->assertEquals (new ResultSet ([new ResultRow (['hello ' => 'world ' ])], new ResultCounters (), new Bookmarks ([])), $ result );
86+
87+ $ expectedResult = new ResultSet (
88+ [new ResultRow (['hello ' => 'world ' ])],
89+ new ResultCounters (),
90+ new Bookmarks ([])
91+ );
92+
93+ $ this ->assertEquals ($ expectedResult , $ result );
7894 }
7995}
0 commit comments