|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Neo4j\Neo4jBundle\Tests\Functional; |
4 | 6 |
|
5 | 7 | use Neo4j\Neo4jBundle\Collector\Neo4jDataCollector; |
6 | 8 | use Neo4j\Neo4jBundle\Tests\App\TestKernel; |
7 | 9 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 10 | +use Symfony\Component\HttpFoundation\Response; |
8 | 11 |
|
| 12 | +/** |
| 13 | + * Functional tests for the Neo4j profiler data collector. |
| 14 | + * |
| 15 | + * These tests verify that the profiler correctly collects Neo4j query data |
| 16 | + * including successful and failed statements, execution times, and query counts. |
| 17 | + * |
| 18 | + * @requires Neo4j server running for full test execution |
| 19 | + */ |
9 | 20 | final class ProfilerTest extends WebTestCase |
10 | 21 | { |
| 22 | + private const EXPECTED_QUERY_COUNT = 2; |
| 23 | + private const EXPECTED_SUCCESSFUL_STATEMENTS = 1; |
| 24 | + private const EXPECTED_FAILED_STATEMENTS = 1; |
| 25 | + |
11 | 26 | #[\Override] |
12 | 27 | protected static function getKernelClass(): string |
13 | 28 | { |
14 | 29 | return TestKernel::class; |
15 | 30 | } |
16 | 31 |
|
17 | | - public function testProfiler(): void |
| 32 | + /** |
| 33 | + * Check if Neo4j is available for testing. |
| 34 | + * If not, skip the test with a clear message. |
| 35 | + * |
| 36 | + * @throws \Exception When Neo4j connection fails for reasons other than unavailability |
| 37 | + */ |
| 38 | + private function skipIfNeo4jNotAvailable(): void |
18 | 39 | { |
19 | 40 | $client = static::createClient(); |
20 | | - $client->enableProfiler(); |
| 41 | + $container = $client->getContainer(); |
21 | 42 |
|
22 | | - $client->request('GET', '/client'); |
23 | | - |
24 | | - if ($profile = $client->getProfile()) { |
25 | | - /** @var Neo4jDataCollector $collector */ |
26 | | - $collector = $profile->getCollector('neo4j'); |
27 | | - $this->assertEquals( |
28 | | - 2, |
29 | | - $collector->getQueryCount() |
30 | | - ); |
31 | | - $successfulStatements = $collector->getSuccessfulStatements(); |
32 | | - $failedStatements = $collector->getFailedStatements(); |
33 | | - $this->assertCount(1, $successfulStatements); |
34 | | - $this->assertCount(1, $failedStatements); |
| 43 | + try { |
| 44 | + $neo4jClient = $container->get('neo4j.client'); |
| 45 | + $driver = $neo4jClient->getDriver(null); |
| 46 | + $session = $driver->createSession(); |
| 47 | + } catch (\Exception $e) { |
| 48 | + if (str_contains($e->getMessage(), 'Cannot connect to host') |
| 49 | + || str_contains($e->getMessage(), 'Host name lookup failure') |
| 50 | + || str_contains($e->getMessage(), 'Connection refused') |
| 51 | + || str_contains($e->getMessage(), 'Connection timed out')) { |
| 52 | + $this->markTestSkipped( |
| 53 | + 'Neo4j server is not available for testing. '. |
| 54 | + 'Please start a Neo4j server to run profiler tests. '. |
| 55 | + 'Error: '.$e->getMessage() |
| 56 | + ); |
| 57 | + } |
| 58 | + throw $e; |
35 | 59 | } |
36 | 60 | } |
37 | 61 |
|
| 62 | + /** |
| 63 | + * Test profiler data collection for client-level operations. |
| 64 | + * |
| 65 | + * This test verifies that the profiler correctly collects: |
| 66 | + * - Total query count |
| 67 | + * - Successful statements |
| 68 | + * - Failed statements |
| 69 | + * - Execution timing data |
| 70 | + */ |
| 71 | + public function testProfilerOnClient(): void |
| 72 | + { |
| 73 | + $this->skipIfNeo4jNotAvailable(); |
| 74 | + |
| 75 | + $client = static::createClient(); |
| 76 | + $client->enableProfiler(); |
| 77 | + |
| 78 | + $crawler = $client->request('GET', '/client'); |
| 79 | + |
| 80 | + $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
| 81 | + |
| 82 | + $profile = $client->getProfile(); |
| 83 | + $this->assertNotNull($profile, 'Profiler should be enabled and available'); |
| 84 | + $this->assertNotFalse($profile, 'Profiler should be enabled and available'); |
| 85 | + |
| 86 | + /** @var Neo4jDataCollector $collector */ |
| 87 | + $collector = $profile->getCollector('neo4j'); |
| 88 | + $this->assertInstanceOf(Neo4jDataCollector::class, $collector); |
| 89 | + |
| 90 | + $this->assertEquals( |
| 91 | + self::EXPECTED_QUERY_COUNT, |
| 92 | + $collector->getQueryCount(), |
| 93 | + 'Expected exactly 2 queries: 1 successful, 1 failed' |
| 94 | + ); |
| 95 | + |
| 96 | + $successfulStatements = $collector->getSuccessfulStatements(); |
| 97 | + $this->assertCount( |
| 98 | + self::EXPECTED_SUCCESSFUL_STATEMENTS, |
| 99 | + $successfulStatements, |
| 100 | + 'Expected exactly 1 successful statement' |
| 101 | + ); |
| 102 | + |
| 103 | + $failedStatements = $collector->getFailedStatements(); |
| 104 | + $this->assertCount( |
| 105 | + self::EXPECTED_FAILED_STATEMENTS, |
| 106 | + $failedStatements, |
| 107 | + 'Expected exactly 1 failed statement' |
| 108 | + ); |
| 109 | + |
| 110 | + $this->assertNotEmpty($successfulStatements, 'Successful statements should not be empty'); |
| 111 | + $this->assertNotEmpty($failedStatements, 'Failed statements should not be empty'); |
| 112 | + |
| 113 | + $successfulStatement = $successfulStatements[0]; |
| 114 | + $this->assertArrayHasKey('statement', $successfulStatement); |
| 115 | + $this->assertArrayHasKey('parameters', $successfulStatement); |
| 116 | + $this->assertArrayHasKey('time', $successfulStatement); |
| 117 | + |
| 118 | + $failedStatement = $failedStatements[0]; |
| 119 | + $this->assertArrayHasKey('statement', $failedStatement); |
| 120 | + $this->assertArrayHasKey('parameters', $failedStatement); |
| 121 | + $this->assertArrayHasKey('time', $failedStatement); |
| 122 | + $this->assertArrayHasKey('error', $failedStatement); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test profiler data collection for session-level operations. |
| 127 | + * |
| 128 | + * This test verifies that the profiler correctly collects data |
| 129 | + * when using session-level Neo4j operations. |
| 130 | + */ |
38 | 131 | public function testProfilerOnSession(): void |
39 | 132 | { |
| 133 | + $this->skipIfNeo4jNotAvailable(); |
| 134 | + |
40 | 135 | $client = static::createClient(); |
41 | 136 | $client->enableProfiler(); |
42 | 137 |
|
43 | | - $client->request('GET', '/session'); |
44 | | - |
45 | | - if ($profile = $client->getProfile()) { |
46 | | - /** @var Neo4jDataCollector $collector */ |
47 | | - $collector = $profile->getCollector('neo4j'); |
48 | | - $this->assertEquals( |
49 | | - 2, |
50 | | - $collector->getQueryCount() |
51 | | - ); |
52 | | - $successfulStatements = $collector->getSuccessfulStatements(); |
53 | | - $failedStatements = $collector->getFailedStatements(); |
54 | | - $this->assertCount(1, $successfulStatements); |
55 | | - $this->assertCount(1, $failedStatements); |
| 138 | + $crawler = $client->request('GET', '/session'); |
| 139 | + |
| 140 | + $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
| 141 | + |
| 142 | + $profile = $client->getProfile(); |
| 143 | + $this->assertNotNull($profile, 'Profiler should be enabled and available'); |
| 144 | + $this->assertNotFalse($profile, 'Profiler should be enabled and available'); |
| 145 | + |
| 146 | + /** @var Neo4jDataCollector $collector */ |
| 147 | + $collector = $profile->getCollector('neo4j'); |
| 148 | + $this->assertInstanceOf(Neo4jDataCollector::class, $collector); |
| 149 | + |
| 150 | + $this->assertEquals( |
| 151 | + self::EXPECTED_QUERY_COUNT, |
| 152 | + $collector->getQueryCount(), |
| 153 | + 'Expected exactly 2 queries: 1 successful, 1 failed' |
| 154 | + ); |
| 155 | + |
| 156 | + $successfulStatements = $collector->getSuccessfulStatements(); |
| 157 | + $this->assertCount( |
| 158 | + self::EXPECTED_SUCCESSFUL_STATEMENTS, |
| 159 | + $successfulStatements, |
| 160 | + 'Expected exactly 1 successful statement' |
| 161 | + ); |
| 162 | + |
| 163 | + $failedStatements = $collector->getFailedStatements(); |
| 164 | + $this->assertCount( |
| 165 | + self::EXPECTED_FAILED_STATEMENTS, |
| 166 | + $failedStatements, |
| 167 | + 'Expected exactly 1 failed statement' |
| 168 | + ); |
| 169 | + |
| 170 | + $successfulStatements = $collector->getSuccessfulStatements(); |
| 171 | + if (!empty($successfulStatements)) { |
| 172 | + $this->assertArrayHasKey('time', $successfulStatements[0], 'Successful statements should contain timing information'); |
56 | 173 | } |
57 | 174 | } |
58 | 175 |
|
| 176 | + /** |
| 177 | + * Test profiler data collection for transaction-level operations. |
| 178 | + * |
| 179 | + * This test verifies that the profiler correctly collects data |
| 180 | + * when using transaction-level Neo4j operations. |
| 181 | + */ |
59 | 182 | public function testProfilerOnTransaction(): void |
60 | 183 | { |
| 184 | + $this->skipIfNeo4jNotAvailable(); |
| 185 | + |
61 | 186 | $client = static::createClient(); |
62 | 187 | $client->enableProfiler(); |
63 | 188 |
|
64 | | - $client->request('GET', '/transaction'); |
65 | | - |
66 | | - if ($profile = $client->getProfile()) { |
67 | | - /** @var Neo4jDataCollector $collector */ |
68 | | - $collector = $profile->getCollector('neo4j'); |
69 | | - $this->assertEquals( |
70 | | - 2, |
71 | | - $collector->getQueryCount() |
72 | | - ); |
73 | | - $successfulStatements = $collector->getSuccessfulStatements(); |
74 | | - $failedStatements = $collector->getFailedStatements(); |
75 | | - $this->assertCount(1, $successfulStatements); |
76 | | - $this->assertCount(1, $failedStatements); |
| 189 | + $crawler = $client->request('GET', '/transaction'); |
| 190 | + |
| 191 | + $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
| 192 | + |
| 193 | + $profile = $client->getProfile(); |
| 194 | + $this->assertNotNull($profile, 'Profiler should be enabled and available'); |
| 195 | + $this->assertNotFalse($profile, 'Profiler should be enabled and available'); |
| 196 | + |
| 197 | + /** @var Neo4jDataCollector $collector */ |
| 198 | + $collector = $profile->getCollector('neo4j'); |
| 199 | + $this->assertInstanceOf(Neo4jDataCollector::class, $collector); |
| 200 | + |
| 201 | + $this->assertEquals( |
| 202 | + self::EXPECTED_QUERY_COUNT, |
| 203 | + $collector->getQueryCount(), |
| 204 | + 'Expected exactly 2 queries: 1 successful, 1 failed' |
| 205 | + ); |
| 206 | + |
| 207 | + $successfulStatements = $collector->getSuccessfulStatements(); |
| 208 | + $this->assertCount( |
| 209 | + self::EXPECTED_SUCCESSFUL_STATEMENTS, |
| 210 | + $successfulStatements, |
| 211 | + 'Expected exactly 1 successful statement' |
| 212 | + ); |
| 213 | + |
| 214 | + $failedStatements = $collector->getFailedStatements(); |
| 215 | + $this->assertCount( |
| 216 | + self::EXPECTED_FAILED_STATEMENTS, |
| 217 | + $failedStatements, |
| 218 | + 'Expected exactly 1 failed statement' |
| 219 | + ); |
| 220 | + |
| 221 | + $successfulStatements = $collector->getSuccessfulStatements(); |
| 222 | + if (!empty($successfulStatements)) { |
| 223 | + $this->assertArrayHasKey('time', $successfulStatements[0], 'Transaction statements should contain timing information'); |
77 | 224 | } |
78 | 225 | } |
| 226 | + |
| 227 | + /** |
| 228 | + * Test profiler data collector availability and basic functionality. |
| 229 | + * |
| 230 | + * This test verifies that the Neo4j data collector is properly registered |
| 231 | + * and returns expected default values when no queries have been executed. |
| 232 | + */ |
| 233 | + public function testProfilerDataCollectorAvailability(): void |
| 234 | + { |
| 235 | + $client = static::createClient(); |
| 236 | + $client->enableProfiler(); |
| 237 | + |
| 238 | + $crawler = $client->request('GET', '/'); |
| 239 | + |
| 240 | + $profile = $client->getProfile(); |
| 241 | + $this->assertNotNull($profile, 'Profiler should be enabled and available'); |
| 242 | + $this->assertNotFalse($profile, 'Profiler should be enabled and available'); |
| 243 | + |
| 244 | + /** @var Neo4jDataCollector $collector */ |
| 245 | + $collector = $profile->getCollector('neo4j'); |
| 246 | + $this->assertInstanceOf(Neo4jDataCollector::class, $collector); |
| 247 | + |
| 248 | + $this->assertEquals(0, $collector->getQueryCount(), 'Query count should be 0 when no queries executed'); |
| 249 | + $this->assertCount(0, $collector->getSuccessfulStatements(), 'No successful statements expected'); |
| 250 | + $this->assertCount(0, $collector->getFailedStatements(), 'No failed statements expected'); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Test profiler data collector name and toolbar integration. |
| 255 | + * |
| 256 | + * This test verifies that the data collector is properly configured |
| 257 | + * for integration with the Symfony profiler toolbar. |
| 258 | + */ |
| 259 | + public function testProfilerDataCollectorConfiguration(): void |
| 260 | + { |
| 261 | + $client = static::createClient(); |
| 262 | + $client->enableProfiler(); |
| 263 | + |
| 264 | + $crawler = $client->request('GET', '/'); |
| 265 | + |
| 266 | + $profile = $client->getProfile(); |
| 267 | + $this->assertNotNull($profile); |
| 268 | + $this->assertNotFalse($profile); |
| 269 | + |
| 270 | + /** @var Neo4jDataCollector $collector */ |
| 271 | + $collector = $profile->getCollector('neo4j'); |
| 272 | + $this->assertInstanceOf(Neo4jDataCollector::class, $collector); |
| 273 | + |
| 274 | + $this->assertEquals('neo4j', $collector->getName(), 'Collector name should be "neo4j"'); |
| 275 | + |
| 276 | + $serialized = serialize($collector); |
| 277 | + |
| 278 | + $unserialized = unserialize($serialized); |
| 279 | + $this->assertInstanceOf(Neo4jDataCollector::class, $unserialized); |
| 280 | + $this->assertEquals($collector->getQueryCount(), $unserialized->getQueryCount()); |
| 281 | + } |
79 | 282 | } |
0 commit comments