Skip to content

Added unit test and code coverage over 90 percent #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DOCKER_HOST_APP_PORT=8000
DOCKER_HOST_NEO4J_HTTP_PORT=7474
DOCKER_HOST_NEO4J_BOLT_PORT=7687

NEO4J_HOST=neo4j+s://bb79fe35.databases.neo4j.io
NEO4J_DATABASE=neo4j
NEO4J_PORT=7687
NEO4J_USER=neo4j
NEO4J_PASSWORD=your_secure_password
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ composer.lock
.php-cs-fixer.cache

composer.origin.json

coverage
phpunitCoverage.xml
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"psalm": "APP_ENV=dev php bin/console.php cache:warmup && vendor/bin/psalm --show-info=true",
"fix-cs": "vendor/bin/php-cs-fixer fix",
"check-cs": "vendor/bin/php-cs-fixer fix --dry-run",
"ci-symfony-install-version": "./.github/scripts/setup-symfony-env.bash"
"ci-symfony-install-version": "./.github/scripts/setup-symfony-env.bash",
"phpunit-with-coverage" : "XDEBUG_MODE=coverage php -d memory_limit=-1 vendor/bin/phpunit --configuration=phpunitCoverage.xml --testsuite=All --coverage-filter=src tests"
}
}
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ services:
volumes:
- ./:/opt/project
environment:
- NEO4J_HOST=neo4j
- NEO4J_HOST=neo4j+s://bb79fe35.databases.neo4j.io
- NEO4J_DATABASE=neo4j
- NEO4J_PORT=7687
- NEO4J_USER=neo4j
- NEO4J_PASSWORD=testtest
- NEO4J_PASSWORD=OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0
- XDEBUG_CONFIG="client_host=host.docker.internal log=/tmp/xdebug.log"
working_dir: /opt/project
extra_hosts:
Expand Down
46 changes: 6 additions & 40 deletions src/Event/FailureEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,19 @@ class FailureEvent extends Event
protected bool $shouldThrowException = true;

public function __construct(
private readonly ?string $alias,
private readonly ?Statement $statement,
private readonly Neo4jException $exception,
private readonly \DateTimeInterface $time,
private readonly ?string $scheme,
private readonly ?string $transactionId,
public readonly ?string $alias,
public readonly ?Statement $statement,
public readonly Neo4jException $exception,
public readonly \DateTimeInterface $time,
public readonly ?string $scheme,
public readonly ?string $transactionId,
) {
}

public function getStatement(): ?Statement
{
return $this->statement;
}

public function getException(): Neo4jException
{
return $this->exception;
}

/** @api */
public function disableException(): void
{
$this->shouldThrowException = false;
}

public function shouldThrowException(): bool
{
return $this->shouldThrowException;
}

public function getTime(): \DateTimeInterface
{
return $this->time;
}

public function getAlias(): ?string
{
return $this->alias;
}

public function getScheme(): ?string
{
return $this->scheme;
}

public function getTransactionId(): ?string
{
return $this->transactionId;
}
}
33 changes: 5 additions & 28 deletions src/Event/PostRunEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,13 @@ class PostRunEvent extends Event
public const EVENT_ID = 'neo4j.post_run';

public function __construct(
private readonly ?string $alias,
private readonly ResultSummary $result,
private readonly \DateTimeInterface $time,
private readonly ?string $scheme,
private readonly ?string $transactionId,
public readonly ?string $alias,
public readonly ResultSummary $result,
public readonly \DateTimeInterface $time,
public readonly ?string $scheme,
public readonly ?string $transactionId
) {
}

public function getResult(): ResultSummary
{
return $this->result;
}

public function getTime(): \DateTimeInterface
{
return $this->time;
}

public function getAlias(): ?string
{
return $this->alias;
}

public function getScheme(): ?string
{
return $this->scheme;
}

public function getTransactionId(): ?string
{
return $this->transactionId;
}
}
34 changes: 5 additions & 29 deletions src/Event/PreRunEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,13 @@ class PreRunEvent extends Event
public const EVENT_ID = 'neo4j.pre_run';

public function __construct(
private readonly ?string $alias,
private readonly Statement $statement,
private readonly \DateTimeInterface $time,
private readonly ?string $scheme,
private readonly ?string $transactionId,
public readonly ?string $alias,
public readonly Statement $statement,
public readonly \DateTimeInterface $time,
public readonly ?string $scheme,
public readonly ?string $transactionId,
) {
}

/** @api */
public function getStatement(): Statement
{
return $this->statement;
}

public function getTime(): \DateTimeInterface
{
return $this->time;
}

public function getAlias(): ?string
{
return $this->alias;
}

public function getScheme(): ?string
{
return $this->scheme;
}

public function getTransactionId(): ?string
{
return $this->transactionId;
}
}
25 changes: 14 additions & 11 deletions src/EventListener/Neo4jProfileListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Service\ResetInterface;

final class Neo4jProfileListener implements EventSubscriberInterface, ResetInterface
class Neo4jProfileListener implements EventSubscriberInterface, ResetInterface
{
/**
* @var list<array{
Expand Down Expand Up @@ -53,13 +53,16 @@ public static function getSubscribedEvents(): array

public function onPostRun(PostRunEvent $event): void
{
if (in_array($event->getAlias(), $this->enabledProfiles)) {
$time = $event->getTime();
$result = $event->getResult();
if (in_array($event->alias, $this->enabledProfiles)) {
$time = $event->time;
$result = $event->result;
$end_time = $time->getTimestamp() + $result->getResultAvailableAfter() + $result->getResultConsumedAfter();

$this->profiledSummaries[] = [
'result' => $event->getResult(),
'alias' => $event->getAlias(),
'result' => $result,
'alias' => $event->alias,
'scheme' => $event->scheme,
'transaction_id' => $event->transactionId,
'time' => $time->format('Y-m-d H:i:s'),
'start_time' => $time->getTimestamp(),
'end_time' => $end_time,
Expand All @@ -69,12 +72,12 @@ public function onPostRun(PostRunEvent $event): void

public function onFailure(FailureEvent $event): void
{
if (in_array($event->getAlias(), $this->enabledProfiles)) {
$time = $event->getTime();
if (in_array($event->alias, $this->enabledProfiles)) {
$time = $event->time;
$this->profiledFailures[] = [
'exception' => $event->getException(),
'statement' => $event->getStatement(),
'alias' => $event->getAlias(),
'exception' => $event->exception,
'statement' => $event->statement,
'alias' => $event->alias,
'time' => $time->format('Y-m-d H:i:s'),
'timestamp' => $time->getTimestamp(),
];
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/StopwatchEventNameFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Laudis\Neo4j\Enum\TransactionState;

final class StopwatchEventNameFactory
class StopwatchEventNameFactory
{
public function __construct(
) {
Expand Down
53 changes: 53 additions & 0 deletions tests/Unit/Collector/Neo4jDataCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Neo4j\Neo4jBundle\Tests\Unit\Collector;

use Neo4j\Neo4jBundle\Collector\Neo4jDataCollector;
use Neo4j\Neo4jBundle\EventListener\Neo4jProfileListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Neo4jDataCollectorTest extends TestCase
{
private Neo4jProfileListener $subscriber;
private Neo4jDataCollector $collector;

protected function setUp(): void
{
$this->subscriber = $this->createMock(Neo4jProfileListener::class);
$this->collector = new Neo4jDataCollector($this->subscriber);
}

public function testGetName(): void
{
$this->assertSame('neo4j', $this->collector->getName());
}

public function testGetQueryCount(): void
{
$this->subscriber->method('getProfiledSummaries')->willReturn([
['start_time' => 1000, 'query' => 'MATCH (n) RETURN n'],
]);
$this->collector->collect(new Request(), new Response());

$this->assertSame(1, $this->collector->getQueryCount());
}

public function testRecursiveToArray(): void
{
$obj = new class {
public function toArray(): array
{
return ['key' => 'value'];
}
};
$reflection = new \ReflectionClass($this->collector);
$method = $reflection->getMethod('recursiveToArray');

$result = $method->invoke($this->collector, $obj);
$this->assertSame(['key' => 'value'], $result);
}
}
94 changes: 94 additions & 0 deletions tests/Unit/Decorators/SymfonyClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Neo4j\Neo4jBundle\Tests\Decorators;

use Laudis\Neo4j\Common\DriverSetupManager;
use Laudis\Neo4j\Databags\SessionConfiguration;
use Laudis\Neo4j\Databags\TransactionConfiguration;
use Laudis\Neo4j\Databags\Statement;
use Laudis\Neo4j\Databags\SummarizedResult;
use Laudis\Neo4j\Types\CypherList;
use Laudis\Neo4j\Types\CypherMap;
use Neo4j\Neo4jBundle\Decorators\SymfonyClient;
use Neo4j\Neo4jBundle\Factories\SymfonyDriverFactory;
use Neo4j\Neo4jBundle\Decorators\SymfonySession;
use Neo4j\Neo4jBundle\Decorators\SymfonyTransaction;
use PHPUnit\Framework\TestCase;

class SymfonyClientTest extends TestCase
{
private SymfonyClient $client;
private $driverSetupManagerMock;
private SessionConfiguration $sessionConfig;
private TransactionConfiguration $transactionConfig;
private $driverFactoryMock;
private $sessionMock;
private $transactionMock;

protected function setUp(): void
{
$this->driverSetupManagerMock = $this->createMock(DriverSetupManager::class);
$this->driverFactoryMock = $this->createMock(SymfonyDriverFactory::class);
$this->sessionMock = $this->createMock(SymfonySession::class);
$this->transactionMock = $this->createMock(SymfonyTransaction::class);

$this->sessionConfig = new SessionConfiguration();
$this->transactionConfig = new TransactionConfiguration();

$this->driverSetupManagerMock
->method('getDefaultAlias')
->willReturn('default');

$this->client = new SymfonyClient(
$this->driverSetupManagerMock,
$this->sessionConfig,
$this->transactionConfig,
$this->driverFactoryMock
);
}

public function testRunStatement()
{
$statement = Statement::create('MATCH (n) RETURN n');
$cypherMapMock = $this->createMock(CypherMap::class);
$summarizedResultMock = $this->createMock(SummarizedResult::class);

$this->sessionMock
->expects($this->once())
->method('runStatements')
->with([$statement], $this->transactionConfig)
->willReturn(new CypherList([$summarizedResultMock]));


$reflection = new \ReflectionClass($this->client);
$boundSessions = $reflection->getProperty('boundSessions');
$boundSessions->setValue($this->client, ['default' => $this->sessionMock]);

$result = $this->client->runStatement($statement);

$this->assertInstanceOf(SummarizedResult::class, $result);
}
public function testWriteTransaction()
{
$expectedResult = 'transaction success';

$this->sessionMock
->expects($this->once())
->method('writeTransaction')
->willReturnCallback(function ($tsxHandler) {
return $tsxHandler($this->transactionMock);
});

$reflection = new \ReflectionClass($this->client);
$boundSessions = $reflection->getProperty('boundSessions');
$boundSessions->setValue($this->client, ['default' => $this->sessionMock]);

$result = $this->client->writeTransaction(fn ($tsx) => 'transaction success');

$this->assertEquals($expectedResult, $result);
}


}
Loading