Skip to content

Commit 66af507

Browse files
Added unit test and code coverage over 90 percent
removed the coverage and phunitCoverage.xml remove unnecessary comments and phpunit.xml resolved all the psalm errors and info issues
1 parent 1751d47 commit 66af507

21 files changed

+1156
-115
lines changed

.env

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DOCKER_HOST_APP_PORT=8000
2+
DOCKER_HOST_NEO4J_HTTP_PORT=7474
3+
DOCKER_HOST_NEO4J_BOLT_PORT=7687
4+
5+
NEO4J_HOST=neo4j+s://bb79fe35.databases.neo4j.io
6+
NEO4J_DATABASE=neo4j
7+
NEO4J_PORT=7687
8+
NEO4J_USER=neo4j
9+
NEO4J_PASSWORD=your_secure_password

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ composer.lock
66
.php-cs-fixer.cache
77

88
composer.origin.json
9+
10+
coverage
11+
phpunitCoverage.xml

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"psalm": "APP_ENV=dev php bin/console.php cache:warmup && vendor/bin/psalm --show-info=true",
5858
"fix-cs": "vendor/bin/php-cs-fixer fix",
5959
"check-cs": "vendor/bin/php-cs-fixer fix --dry-run",
60-
"ci-symfony-install-version": "./.github/scripts/setup-symfony-env.bash"
60+
"ci-symfony-install-version": "./.github/scripts/setup-symfony-env.bash",
61+
"phpunit-with-coverage" : "XDEBUG_MODE=coverage php -d memory_limit=-1 vendor/bin/phpunit --configuration=phpunitCoverage.xml --testsuite=All --coverage-filter=src tests"
6162
}
6263
}

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ services:
1111
volumes:
1212
- ./:/opt/project
1313
environment:
14-
- NEO4J_HOST=neo4j
14+
- NEO4J_HOST=${NEO4J_HOST}
1515
- NEO4J_DATABASE=neo4j
1616
- NEO4J_PORT=7687
1717
- NEO4J_USER=neo4j
18-
- NEO4J_PASSWORD=testtest
18+
- NEO4J_PASSWORD=${NEO4J_PASSWORD}
1919
- XDEBUG_CONFIG="client_host=host.docker.internal log=/tmp/xdebug.log"
2020
working_dir: /opt/project
2121
extra_hosts:

src/Event/FailureEvent.php

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,19 @@ class FailureEvent extends Event
1515
protected bool $shouldThrowException = true;
1616

1717
public function __construct(
18-
private readonly ?string $alias,
19-
private readonly ?Statement $statement,
20-
private readonly Neo4jException $exception,
21-
private readonly \DateTimeInterface $time,
22-
private readonly ?string $scheme,
23-
private readonly ?string $transactionId,
18+
public readonly ?string $alias,
19+
public readonly ?Statement $statement,
20+
public readonly Neo4jException $exception,
21+
public readonly \DateTimeInterface $time,
22+
public readonly ?string $scheme,
23+
public readonly ?string $transactionId,
2424
) {
2525
}
2626

27-
public function getStatement(): ?Statement
28-
{
29-
return $this->statement;
30-
}
31-
32-
public function getException(): Neo4jException
33-
{
34-
return $this->exception;
35-
}
36-
3727
/** @api */
3828
public function disableException(): void
3929
{
4030
$this->shouldThrowException = false;
4131
}
4232

43-
public function shouldThrowException(): bool
44-
{
45-
return $this->shouldThrowException;
46-
}
47-
48-
public function getTime(): \DateTimeInterface
49-
{
50-
return $this->time;
51-
}
52-
53-
public function getAlias(): ?string
54-
{
55-
return $this->alias;
56-
}
57-
58-
public function getScheme(): ?string
59-
{
60-
return $this->scheme;
61-
}
62-
63-
public function getTransactionId(): ?string
64-
{
65-
return $this->transactionId;
66-
}
6733
}

src/Event/PostRunEvent.php

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,13 @@ class PostRunEvent extends Event
1212
public const EVENT_ID = 'neo4j.post_run';
1313

1414
public function __construct(
15-
private readonly ?string $alias,
16-
private readonly ResultSummary $result,
17-
private readonly \DateTimeInterface $time,
18-
private readonly ?string $scheme,
19-
private readonly ?string $transactionId,
15+
public readonly ?string $alias,
16+
public readonly ResultSummary $result,
17+
public readonly \DateTimeInterface $time,
18+
public readonly ?string $scheme,
19+
public readonly ?string $transactionId
2020
) {
2121
}
2222

23-
public function getResult(): ResultSummary
24-
{
25-
return $this->result;
26-
}
27-
28-
public function getTime(): \DateTimeInterface
29-
{
30-
return $this->time;
31-
}
32-
33-
public function getAlias(): ?string
34-
{
35-
return $this->alias;
36-
}
37-
38-
public function getScheme(): ?string
39-
{
40-
return $this->scheme;
41-
}
4223

43-
public function getTransactionId(): ?string
44-
{
45-
return $this->transactionId;
46-
}
4724
}

src/Event/PreRunEvent.php

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,13 @@ class PreRunEvent extends Event
1212
public const EVENT_ID = 'neo4j.pre_run';
1313

1414
public function __construct(
15-
private readonly ?string $alias,
16-
private readonly Statement $statement,
17-
private readonly \DateTimeInterface $time,
18-
private readonly ?string $scheme,
19-
private readonly ?string $transactionId,
15+
public readonly ?string $alias,
16+
public readonly Statement $statement,
17+
public readonly \DateTimeInterface $time,
18+
public readonly ?string $scheme,
19+
public readonly ?string $transactionId,
2020
) {
2121
}
2222

23-
/** @api */
24-
public function getStatement(): Statement
25-
{
26-
return $this->statement;
27-
}
28-
29-
public function getTime(): \DateTimeInterface
30-
{
31-
return $this->time;
32-
}
33-
34-
public function getAlias(): ?string
35-
{
36-
return $this->alias;
37-
}
38-
39-
public function getScheme(): ?string
40-
{
41-
return $this->scheme;
42-
}
4323

44-
public function getTransactionId(): ?string
45-
{
46-
return $this->transactionId;
47-
}
4824
}

src/EventListener/Neo4jProfileListener.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1313
use Symfony\Contracts\Service\ResetInterface;
1414

15-
final class Neo4jProfileListener implements EventSubscriberInterface, ResetInterface
15+
class Neo4jProfileListener implements EventSubscriberInterface, ResetInterface
1616
{
1717
/**
1818
* @var list<array{
19+
* alias: null|string,
20+
* end_time: float|int,
1921
* result: ResultSummary,
20-
* alias: string|null,
21-
* time: string,
2222
* start_time: float|int,
23-
* end_time: float|int
23+
* time: string,
24+
* scheme?: null|string,
25+
* transaction_id?: null|string
2426
* }>
2527
*/
2628
private array $profiledSummaries = [];
@@ -53,13 +55,16 @@ public static function getSubscribedEvents(): array
5355

5456
public function onPostRun(PostRunEvent $event): void
5557
{
56-
if (in_array($event->getAlias(), $this->enabledProfiles)) {
57-
$time = $event->getTime();
58-
$result = $event->getResult();
58+
if (in_array($event->alias, $this->enabledProfiles)) {
59+
$time = $event->time;
60+
$result = $event->result;
5961
$end_time = $time->getTimestamp() + $result->getResultAvailableAfter() + $result->getResultConsumedAfter();
62+
6063
$this->profiledSummaries[] = [
61-
'result' => $event->getResult(),
62-
'alias' => $event->getAlias(),
64+
'result' => $result,
65+
'alias' => $event->alias,
66+
'scheme' => $event->scheme,
67+
'transaction_id' => $event->transactionId,
6368
'time' => $time->format('Y-m-d H:i:s'),
6469
'start_time' => $time->getTimestamp(),
6570
'end_time' => $end_time,
@@ -69,12 +74,12 @@ public function onPostRun(PostRunEvent $event): void
6974

7075
public function onFailure(FailureEvent $event): void
7176
{
72-
if (in_array($event->getAlias(), $this->enabledProfiles)) {
73-
$time = $event->getTime();
77+
if (in_array($event->alias, $this->enabledProfiles)) {
78+
$time = $event->time;
7479
$this->profiledFailures[] = [
75-
'exception' => $event->getException(),
76-
'statement' => $event->getStatement(),
77-
'alias' => $event->getAlias(),
80+
'exception' => $event->exception,
81+
'statement' => $event->statement,
82+
'alias' => $event->alias,
7883
'time' => $time->format('Y-m-d H:i:s'),
7984
'timestamp' => $time->getTimestamp(),
8085
];

src/Factories/StopwatchEventNameFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Laudis\Neo4j\Enum\TransactionState;
66

7-
final class StopwatchEventNameFactory
7+
class StopwatchEventNameFactory
88
{
99
public function __construct(
1010
) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Neo4j\Neo4jBundle\Tests\Unit\Collector;
6+
use Neo4j\Neo4jBundle\EventListener\Neo4jProfileListener;
7+
use Neo4j\Neo4jBundle\Collector\Neo4jDataCollector;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Response;
12+
13+
class Neo4jDataCollectorTest extends TestCase
14+
{
15+
private MockObject&Neo4jProfileListener $subscriber;
16+
private Neo4jDataCollector $collector;
17+
18+
protected function setUp(): void
19+
{
20+
$this->subscriber = $this->createMock(Neo4jProfileListener::class);
21+
$this->collector = new Neo4jDataCollector($this->subscriber);
22+
}
23+
24+
public function testGetName(): void
25+
{
26+
$this->assertSame('neo4j', $this->collector->getName());
27+
}
28+
29+
public function testGetQueryCount(): void
30+
{
31+
$this->subscriber
32+
->expects($this->once())
33+
->method('getProfiledSummaries')
34+
->willReturn([
35+
['start_time' => 1000, 'query' => 'MATCH (n) RETURN n'],
36+
]);
37+
38+
$this->collector->collect(new Request(), new Response());
39+
40+
$this->assertSame(1, $this->collector->getQueryCount());
41+
}
42+
43+
public function testRecursiveToArray(): void
44+
{
45+
$obj = new class {
46+
public function toArray(): array
47+
{
48+
return ['key' => 'value'];
49+
}
50+
};
51+
$reflection = new \ReflectionClass($this->collector);
52+
$method = $reflection->getMethod('recursiveToArray');
53+
54+
$result = $method->invoke($this->collector, $obj);
55+
$this->assertSame(['key' => 'value'], $result);
56+
}
57+
}

0 commit comments

Comments
 (0)