Skip to content

Commit aec41d0

Browse files
PratikshaPratiksha
authored andcommitted
implemented the unit tests for authentication and objects
1 parent a947ec4 commit aec41d0

11 files changed

+359
-1
lines changed

src/Objects/Node.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Node
2222
private array $properties;
2323

2424
/**
25-
* Node constructor.
25+
* Node constructor
2626
*
2727
* @param string[] $labels Array of labels for the node.
2828
* @param array<string, mixed> $properties Associative array of properties.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Tests\Unit\Authentication;
4+
5+
use Neo4j\QueryAPI\Authentication\BasicAuthentication;
6+
use PHPUnit\Framework\MockObject\MockObject;
7+
use PHPUnit\Framework\TestCase;
8+
use Psr\Http\Message\RequestInterface;
9+
10+
final class BasicAuthenticationUnitTest extends TestCase
11+
{
12+
private BasicAuthentication $auth;
13+
private MockObject $requestMock;
14+
15+
protected function setUp(): void
16+
{
17+
$this->auth = new BasicAuthentication('testUser', 'testPass');
18+
$this->requestMock = $this->createMock(RequestInterface::class);
19+
}
20+
21+
public function testAuthenticateAddsAuthorizationHeader(): void
22+
{
23+
$authHeader = 'Basic ' . base64_encode('testUser:testPass');
24+
25+
$this->requestMock->expects($this->once())
26+
->method('withHeader')
27+
->with('Authorization', $authHeader)
28+
->willReturn($this->requestMock);
29+
30+
$result = $this->auth->authenticate($this->requestMock);
31+
$this->assertSame($this->requestMock, $result);
32+
}
33+
34+
public function testGetHeaderReturnsCorrectValue(): void
35+
{
36+
$expectedHeader = 'Basic ' . base64_encode('testUser:testPass');
37+
$this->assertEquals($expectedHeader, $this->auth->getHeader());
38+
}
39+
40+
public function testGetTypeReturnsBasic(): void
41+
{
42+
$this->assertEquals('Basic', $this->auth->getType());
43+
}
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Tests\Unit\Authentication;
4+
5+
use Neo4j\QueryAPI\Authentication\BearerAuthentication;
6+
use PHPUnit\Framework\TestCase;
7+
use Psr\Http\Message\RequestInterface;
8+
9+
class BearerAuthenticationUnitTest extends TestCase
10+
{
11+
private BearerAuthentication $auth;
12+
private RequestInterface $requestMock;
13+
14+
protected function setUp(): void
15+
{
16+
$this->auth = new BearerAuthentication('testToken');
17+
$this->requestMock = $this->createMock(RequestInterface::class);
18+
}
19+
20+
public function testAuthenticateAddsAuthorizationHeader(): void
21+
{
22+
$authHeader = 'Bearer testToken';
23+
24+
$this->requestMock->expects($this->once())
25+
->method('withHeader')
26+
->with('Authorization', $authHeader)
27+
->willReturnSelf();
28+
29+
$result = $this->auth->authenticate($this->requestMock);
30+
$this->assertSame($this->requestMock, $result);
31+
}
32+
33+
public function testGetHeaderReturnsCorrectValue(): void
34+
{
35+
$expectedHeader = 'Bearer testToken';
36+
$this->assertEquals($expectedHeader, $this->auth->getHeader());
37+
}
38+
39+
public function testGetTypeReturnsBearer(): void
40+
{
41+
$this->assertEquals('Bearer', $this->auth->getType());
42+
}
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Tests\Unit\Authentication;
4+
5+
use Neo4j\QueryAPI\Authentication\NoAuth;
6+
use PHPUnit\Framework\TestCase;
7+
use Psr\Http\Message\RequestInterface;
8+
9+
class NoAuthUnitTest extends TestCase
10+
{
11+
private NoAuth $auth;
12+
private RequestInterface $requestMock;
13+
14+
protected function setUp(): void
15+
{
16+
$this->auth = new NoAuth();
17+
$this->requestMock = $this->createMock(RequestInterface::class);
18+
}
19+
20+
public function testAuthenticateReturnsUnmodifiedRequest(): void
21+
{
22+
23+
$this->assertSame($this->requestMock, $this->auth->authenticate($this->requestMock));
24+
}
25+
26+
public function testGetHeaderReturnsEmptyString(): void
27+
{
28+
$this->assertEquals('', $this->auth->getHeader());
29+
}
30+
31+
public function testGetTypeReturnsNoAuth(): void
32+
{
33+
$this->assertEquals('NoAuth', $this->auth->getType());
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Neo4j\QueryAPI\Tests\Unit\objects;
3+
4+
use Neo4j\QueryAPI\Objects\Authentication;
5+
use Neo4j\QueryAPI\Authentication\BasicAuthentication;
6+
use Neo4j\QueryAPI\Authentication\BearerAuthentication;
7+
use Neo4j\QueryAPI\Authentication\NoAuth;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class AuthenticationUnitTest extends TestCase
11+
{
12+
public function testBasicReturnsBasicAuthenticationInstance(): void
13+
{
14+
$auth = Authentication::basic('testUser', 'testPass');
15+
$this->assertInstanceOf(BasicAuthentication::class, $auth);
16+
}
17+
18+
public function testFromEnvironmentReturnsBasicAuthenticationInstance(): void
19+
{
20+
putenv('NEO4J_USERNAME=testUser');
21+
putenv('NEO4J_PASSWORD=testPass');
22+
23+
$auth = Authentication::fromEnvironment();
24+
$this->assertInstanceOf(BasicAuthentication::class, $auth);
25+
}
26+
27+
public function testNoAuthReturnsNoAuthInstance(): void
28+
{
29+
$auth = Authentication::noAuth();
30+
$this->assertInstanceOf(NoAuth::class, $auth);
31+
}
32+
33+
public function testBearerReturnsBearerAuthenticationInstance(): void
34+
{
35+
$auth = Authentication::bearer('testToken');
36+
$this->assertInstanceOf(BearerAuthentication::class, $auth);
37+
}
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace Neo4j\QueryAPI\Tests\Unit\objects;
3+
4+
use Neo4j\QueryAPI\Objects\Bookmarks;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class BookmarksUnitTest extends TestCase
8+
{
9+
private Bookmarks $bookmarks;
10+
11+
protected function setUp(): void
12+
{
13+
$this->bookmarks = new Bookmarks(['bookmark1', 'bookmark2']);
14+
}
15+
16+
public function testGetBookmarksReturnsCorrectArray(): void
17+
{
18+
$this->assertEquals(['bookmark1', 'bookmark2'], $this->bookmarks->getBookmarks());
19+
}
20+
21+
public function testAddBookmarksMergesUniqueValues(): void
22+
{
23+
$newBookmarks = new Bookmarks(['bookmark1', 'bookmark2', 'bookmark3']);
24+
$this->bookmarks->addBookmarks($newBookmarks);
25+
26+
$this->assertEquals(['bookmark1', 'bookmark2', 'bookmark3'], array_values($this->bookmarks->getBookmarks()));
27+
}
28+
29+
public function testAddBookmarksDoesNothingWhenNullIsPassed(): void
30+
{
31+
$this->bookmarks->addBookmarks(null);
32+
$this->assertEquals(['bookmark1', 'bookmark2'], $this->bookmarks->getBookmarks());
33+
}
34+
35+
public function testCountReturnsCorrectNumber(): void
36+
{
37+
$this->assertEquals(2, $this->bookmarks->count());
38+
}
39+
40+
public function testJsonSerializeReturnsCorrectArray(): void
41+
{
42+
$this->assertEquals(['bookmark1', 'bookmark2'], $this->bookmarks->jsonSerialize());
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Tests\Unit\objects;
4+
5+
use Neo4j\QueryAPI\Objects\Node;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class NodeUnitTest extends TestCase
9+
{
10+
private Node $node;
11+
12+
protected function setUp(): void
13+
{
14+
$this->node = new Node(['Label1', 'Label2'], ['key1' => 'value1', 'key2' => 42]);
15+
}
16+
17+
public function testGetLabelsReturnsCorrectArray(): void
18+
{
19+
$this->assertEquals(['Label1', 'Label2'], $this->node->getLabels());
20+
}
21+
22+
public function testGetPropertiesReturnsCorrectArray(): void
23+
{
24+
$this->assertEquals(['key1' => 'value1', 'key2' => 42], $this->node->getProperties());
25+
}
26+
27+
public function testToArrayReturnsCorrectStructure(): void
28+
{
29+
$expected = [
30+
'_labels' => ['Label1', 'Label2'],
31+
'_properties' => ['key1' => 'value1', 'key2' => 42],
32+
];
33+
34+
$this->assertEquals($expected, $this->node->toArray());
35+
}
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Neo4j\QueryAPI\Tests\Unit\objects;
3+
4+
use Neo4j\QueryAPI\Objects\Path;
5+
use Neo4j\QueryAPI\Objects\Node;
6+
use Neo4j\QueryAPI\Objects\Relationship;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class PathUnitTest extends TestCase
10+
{
11+
private Path $path;
12+
private array $nodes;
13+
private array $relationships;
14+
15+
protected function setUp(): void
16+
{
17+
$this->nodes = [
18+
new Node(['Person'], ['name' => 'Alice']),
19+
new Node(['Person'], ['name' => 'Bob'])
20+
];
21+
22+
$this->relationships = [
23+
new Relationship('KNOWS', ['since' => 2020], $this->nodes[0], $this->nodes[1])
24+
];
25+
26+
$this->path = new Path($this->nodes, $this->relationships);
27+
}
28+
29+
public function testGetNodesReturnsCorrectArray(): void
30+
{
31+
$this->assertEquals($this->nodes, $this->path->getNodes());
32+
}
33+
34+
public function testGetRelationshipsReturnsCorrectArray(): void
35+
{
36+
$this->assertEquals($this->relationships, $this->path->getRelationships());
37+
}
38+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace Neo4j\QueryAPI\Tests\Unit\objects;
3+
4+
use Neo4j\QueryAPI\Objects\Person;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class PersonUnitTest extends TestCase{
8+
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Neo4j\QueryAPI\Tests\Unit\objects;
3+
4+
use Neo4j\QueryAPI\Objects\Point;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class PointUnitTest extends TestCase
8+
{
9+
private Point $point;
10+
11+
protected function setUp(): void
12+
{
13+
$this->point = new Point(1.5, 2.5, 3.5, 4326);
14+
}
15+
16+
public function testGetXReturnsCorrectValue(): void
17+
{
18+
$this->assertEquals(1.5, $this->point->getX());
19+
}
20+
21+
public function testGetYReturnsCorrectValue(): void
22+
{
23+
$this->assertEquals(2.5, $this->point->getY());
24+
}
25+
26+
public function testGetZReturnsCorrectValue(): void
27+
{
28+
$this->assertEquals(3.5, $this->point->getZ());
29+
}
30+
31+
public function testGetSridReturnsCorrectValue(): void
32+
{
33+
$this->assertEquals(4326, $this->point->getSrid());
34+
}
35+
36+
public function testToStringReturnsCorrectFormat(): void
37+
{
38+
$this->assertEquals('SRID=4326;POINT (1.5 2.5)', (string) $this->point);
39+
}
40+
}

0 commit comments

Comments
 (0)