From 4c1a518f63d2c26099ededce34e5e4741f7ad992 Mon Sep 17 00:00:00 2001 From: exaby73 Date: Wed, 28 Aug 2024 10:43:17 +0530 Subject: [PATCH 1/3] refactor: Remove profiler integration --- config/services.php | 3 - src/Collector/Neo4jDataCollector.php | 83 ------------------- src/Collector/Twig/Neo4jResultExtension.php | 56 ------------- src/DependencyInjection/Neo4jExtension.php | 15 ---- src/Event/FailureEvent.php | 45 ---------- src/Event/PostRunEvent.php | 29 ------- src/Event/PreRunEvent.php | 27 ------ src/EventListener/Neo4jProfileListener.php | 78 ----------------- .../Twig/Neo4jResultExtensionTest.php | 65 --------------- .../Neo4jExtensionTest.php | 57 ------------- 10 files changed, 458 deletions(-) delete mode 100644 src/Collector/Neo4jDataCollector.php delete mode 100644 src/Collector/Twig/Neo4jResultExtension.php delete mode 100644 src/Event/FailureEvent.php delete mode 100644 src/Event/PostRunEvent.php delete mode 100644 src/Event/PreRunEvent.php delete mode 100644 src/EventListener/Neo4jProfileListener.php delete mode 100644 tests/Unit/Collector/Twig/Neo4jResultExtensionTest.php delete mode 100644 tests/Unit/DependencyInjection/Neo4jExtensionTest.php diff --git a/config/services.php b/config/services.php index 8ec9920..2e78d3a 100644 --- a/config/services.php +++ b/config/services.php @@ -48,7 +48,4 @@ $services->alias(DriverInterface::class, 'neo4j.driver'); $services->alias(SessionInterface::class, 'neo4j.session'); $services->alias(TransactionInterface::class, 'neo4j.transaction'); - - $services->set('neo4j.subscriber', Neo4jProfileListener::class) - ->tag('kernel.event_subscriber'); }; diff --git a/src/Collector/Neo4jDataCollector.php b/src/Collector/Neo4jDataCollector.php deleted file mode 100644 index 2e3f739..0000000 --- a/src/Collector/Neo4jDataCollector.php +++ /dev/null @@ -1,83 +0,0 @@ ->, - * failed_statements: list - * } $data - */ -final class Neo4jDataCollector extends AbstractDataCollector -{ - public function __construct( - private Neo4jProfileListener $subscriber - ) { - } - - public function collect(Request $request, Response $response, ?\Throwable $exception = null): void - { - $this->data['successful_statements'] = array_map( - static fn (ResultSummary $summary) => $summary->toArray(), - $this->subscriber->getProfiledSummaries() - ); - - $this->data['failed_statements'] = array_map( - static fn (array $x) => [ - 'statement' => $x['statement']->toArray(), - 'exception' => [ - 'code' => $x['exception']->getErrors()[0]->getCode(), - 'message' => $x['exception']->getErrors()[0]->getMessage(), - 'classification' => $x['exception']->getErrors()[0]->getClassification(), - 'category' => $x['exception']->getErrors()[0]->getCategory(), - 'title' => $x['exception']->getErrors()[0]->getTitle(), - ], - 'alias' => $x['alias'], - ], - $this->subscriber->getProfiledFailures() - ); - } - - public function reset(): void - { - parent::reset(); - $this->subscriber->reset(); - } - - public function getName(): string - { - return 'neo4j'; - } - - public function getFailedStatements(): array - { - return $this->data['failed_statements']; - } - - public function getSuccessfulStatements(): array - { - return $this->data['successful_statements']; - } - - public function getQueryCount(): int - { - return count($this->data['successful_statements']) + count($this->data['failed_statements']); - } - - public static function getTemplate(): ?string - { - return 'web_profiler.html.twig'; - } -} diff --git a/src/Collector/Twig/Neo4jResultExtension.php b/src/Collector/Twig/Neo4jResultExtension.php deleted file mode 100644 index 20277f1..0000000 --- a/src/Collector/Twig/Neo4jResultExtension.php +++ /dev/null @@ -1,56 +0,0 @@ - - */ -class Neo4jResultExtension extends AbstractExtension -{ - /** - * @return array - */ - public function getFilters(): array - { - return [ - new TwigFilter('neo4jResult', [$this, 'getType']), - ]; - } - - public function getType(mixed $object): string - { - return $this->doGetType($object, true); - } - - public function getName(): string - { - return 'neo4j.result'; - } - - private function doGetType(mixed $object, bool $recursive): string - { - if ($object instanceof Node) { - return sprintf('%s: %s', $object->getId(), $object->getLabels()->join(', ')); - } - - if (is_array($object) && $recursive) { - if (empty($object)) { - return 'Empty array'; - } - $ret = []; - foreach ($object as $o) { - $ret[] = $this->doGetType($o, false); - } - - return sprintf('[%s]', implode(', ', $ret)); - } - - return get_debug_type($object); - } -} diff --git a/src/DependencyInjection/Neo4jExtension.php b/src/DependencyInjection/Neo4jExtension.php index f8617ae..5283723 100644 --- a/src/DependencyInjection/Neo4jExtension.php +++ b/src/DependencyInjection/Neo4jExtension.php @@ -12,7 +12,6 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\DependencyInjection\Reference; @@ -52,20 +51,6 @@ public function load(array $configs, ContainerBuilder $container): ContainerBuil } } - if (0 !== count($enabledProfiles)) { - $container->setDefinition('neo4j.data_collector', (new Definition(Neo4jDataCollector::class)) - ->setAutowired(true) - ->addTag('data_collector') - ); - - $container->setAlias(Neo4jProfileListener::class, 'neo4j.subscriber'); - - $container->setDefinition('neo4j.subscriber', (new Definition(Neo4jProfileListener::class)) - ->setArgument(0, $enabledProfiles) - ->addTag('kernel.event_subscriber') - ); - } - return $container; } diff --git a/src/Event/FailureEvent.php b/src/Event/FailureEvent.php deleted file mode 100644 index 2981193..0000000 --- a/src/Event/FailureEvent.php +++ /dev/null @@ -1,45 +0,0 @@ -exception; - } - - public function disableException(): void - { - $this->shouldThrowException = false; - } - - public function shouldThrowException(): bool - { - return $this->shouldThrowException; - } - - public function getAlias(): ?string - { - return $this->alias; - } - - public function getStatement(): Statement - { - return $this->statement; - } -} diff --git a/src/Event/PostRunEvent.php b/src/Event/PostRunEvent.php deleted file mode 100644 index b0bf84b..0000000 --- a/src/Event/PostRunEvent.php +++ /dev/null @@ -1,29 +0,0 @@ -result; - } - - public function getAlias(): ?string - { - return $this->alias; - } -} diff --git a/src/Event/PreRunEvent.php b/src/Event/PreRunEvent.php deleted file mode 100644 index 6ab3f4d..0000000 --- a/src/Event/PreRunEvent.php +++ /dev/null @@ -1,27 +0,0 @@ -statement; - } - - public function getAlias(): ?string - { - return $this->alias; - } -} diff --git a/src/EventListener/Neo4jProfileListener.php b/src/EventListener/Neo4jProfileListener.php deleted file mode 100644 index b046b99..0000000 --- a/src/EventListener/Neo4jProfileListener.php +++ /dev/null @@ -1,78 +0,0 @@ - - */ - private array $profiledSummaries = []; - - /** - * @var list - */ - private array $profiledFailures = []; - - /** - * @param list $enabledProfiles - */ - public function __construct(private array $enabledProfiles = []) - { - } - - public static function getSubscribedEvents(): array - { - return [ - PostRunEvent::EVENT_ID => 'onPostRun', - FailureEvent::EVENT_ID => 'onFailure', - ]; - } - - public function onPostRun(PostRunEvent $event): void - { - if (in_array($event->getAlias(), $this->enabledProfiles)) { - $this->profiledSummaries[] = $event->getResult(); - } - } - - public function onFailure(FailureEvent $event): void - { - if (in_array($event->getAlias(), $this->enabledProfiles)) { - $this->profiledFailures[] = [ - 'exception' => $event->getException(), - 'statement' => $event->getStatement(), - 'alias' => $event->getAlias(), - ]; - } - } - - public function getProfiledSummaries(): array - { - return $this->profiledSummaries; - } - - /** - * @return list - */ - public function getProfiledFailures(): array - { - return $this->profiledFailures; - } - - public function reset(): void - { - $this->profiledFailures = []; - $this->profiledSummaries = []; - } -} diff --git a/tests/Unit/Collector/Twig/Neo4jResultExtensionTest.php b/tests/Unit/Collector/Twig/Neo4jResultExtensionTest.php deleted file mode 100644 index 07bc8f6..0000000 --- a/tests/Unit/Collector/Twig/Neo4jResultExtensionTest.php +++ /dev/null @@ -1,65 +0,0 @@ - - */ -class Neo4jResultExtensionTest extends TestCase -{ - public function testEmptyArray(): void - { - $o = new Neo4jResultExtension(); - $result = $o->getType([]); - - $this->assertEquals('Empty array', $result); - } - - public function testObject(): void - { - $o = new Neo4jResultExtension(); - $result = $o->getType($o); - - $this->assertEquals(Neo4jResultExtension::class, $result); - } - - public function testScalar(): void - { - $o = new Neo4jResultExtension(); - $result = $o->getType(3); - - $this->assertEquals('int', $result); - } - - public function testScalarArray(): void - { - $o = new Neo4jResultExtension(); - $result = $o->getType([3, 6.3]); - - $this->assertEquals('[int, float]', $result); - } - - public function testArrayArray(): void - { - $o = new Neo4jResultExtension(); - $result = $o->getType([[]]); - - $this->assertEquals('[array]', $result); - } - - public function testNode(): void - { - $o = new Neo4jResultExtension(); - $result = $o->getType(new Node(1, new CypherList(['Label']), new CypherMap(), null)); - - $this->assertEquals('1: Label', $result); - } -} diff --git a/tests/Unit/DependencyInjection/Neo4jExtensionTest.php b/tests/Unit/DependencyInjection/Neo4jExtensionTest.php deleted file mode 100644 index 572535d..0000000 --- a/tests/Unit/DependencyInjection/Neo4jExtensionTest.php +++ /dev/null @@ -1,57 +0,0 @@ - - */ -class Neo4jExtensionTest extends AbstractExtensionTestCase -{ - protected function getMinimalConfiguration(): array - { - $this->setParameter('kernel.cache_dir', 'foo'); - - return ['drivers' => ['default' => ['dsn' => 'bolt://localhost']]]; - } - - public function testDataCollectorLoaded(): void - { - $this->setParameter('kernel.debug', true); - $this->load(); - - $this->assertContainerBuilderHasService('neo4j.data_collector', Neo4jDataCollector::class); - } - - public function testDataCollectorNotLoadedInNonDebug(): void - { - $this->setParameter('kernel.debug', false); - $this->load(); - - $this->assertContainerBuilderNotHasService('neo4j.data_collector'); - } - - public function testDataCollectorNotLoadedWhenDisabled(): void - { - $this->setParameter('kernel.debug', true); - $this->load(['drivers' => [ - 'default' => [ - 'profiling' => false, - ], - ]]); - - $this->assertContainerBuilderNotHasService('neo4j.neo4j_data_collector'); - } - - protected function getContainerExtensions(): array - { - return [ - new Neo4jExtension(), - ]; - } -} From 5e6fdc5d1ba75ddabd4b5da9249751665b768d6f Mon Sep 17 00:00:00 2001 From: exaby73 Date: Wed, 28 Aug 2024 10:50:57 +0530 Subject: [PATCH 2/3] refactor: Bring back EventHandler and fix CS --- config/services.php | 1 - src/DependencyInjection/Neo4jExtension.php | 2 - src/Event/FailureEvent.php | 45 ++++++++++++++++++++++ src/Event/PostRunEvent.php | 29 ++++++++++++++ src/Event/PreRunEvent.php | 27 +++++++++++++ 5 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 src/Event/FailureEvent.php create mode 100644 src/Event/PostRunEvent.php create mode 100644 src/Event/PreRunEvent.php diff --git a/config/services.php b/config/services.php index 2e78d3a..957a43f 100644 --- a/config/services.php +++ b/config/services.php @@ -8,7 +8,6 @@ use Laudis\Neo4j\Contracts\TransactionInterface; use Neo4j\Neo4jBundle\ClientFactory; use Neo4j\Neo4jBundle\EventHandler; -use Neo4j\Neo4jBundle\EventListener\Neo4jProfileListener; use Neo4j\Neo4jBundle\SymfonyClient; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; diff --git a/src/DependencyInjection/Neo4jExtension.php b/src/DependencyInjection/Neo4jExtension.php index 5283723..bf491ec 100644 --- a/src/DependencyInjection/Neo4jExtension.php +++ b/src/DependencyInjection/Neo4jExtension.php @@ -4,8 +4,6 @@ namespace Neo4j\Neo4jBundle\DependencyInjection; -use Neo4j\Neo4jBundle\Collector\Neo4jDataCollector; -use Neo4j\Neo4jBundle\EventListener\Neo4jProfileListener; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; diff --git a/src/Event/FailureEvent.php b/src/Event/FailureEvent.php new file mode 100644 index 0000000..2981193 --- /dev/null +++ b/src/Event/FailureEvent.php @@ -0,0 +1,45 @@ +exception; + } + + public function disableException(): void + { + $this->shouldThrowException = false; + } + + public function shouldThrowException(): bool + { + return $this->shouldThrowException; + } + + public function getAlias(): ?string + { + return $this->alias; + } + + public function getStatement(): Statement + { + return $this->statement; + } +} diff --git a/src/Event/PostRunEvent.php b/src/Event/PostRunEvent.php new file mode 100644 index 0000000..b0bf84b --- /dev/null +++ b/src/Event/PostRunEvent.php @@ -0,0 +1,29 @@ +result; + } + + public function getAlias(): ?string + { + return $this->alias; + } +} diff --git a/src/Event/PreRunEvent.php b/src/Event/PreRunEvent.php new file mode 100644 index 0000000..6ab3f4d --- /dev/null +++ b/src/Event/PreRunEvent.php @@ -0,0 +1,27 @@ +statement; + } + + public function getAlias(): ?string + { + return $this->alias; + } +} From f84e9ddd4cec5229c6f1de18fada059e85e91858 Mon Sep 17 00:00:00 2001 From: exaby73 Date: Wed, 28 Aug 2024 10:53:08 +0530 Subject: [PATCH 3/3] remove: Unit test config --- phpunit.xml.dist | 3 --- 1 file changed, 3 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9fd609d..73e173e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,9 +1,6 @@ - - ./tests/Unit - ./tests/Functional