|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MongoDB\Bundle\Tests\Functional\DataCollector; |
| 6 | + |
| 7 | +use ArrayIterator; |
| 8 | +use MongoDB\Bundle\DataCollector\MongoDBDataCollector; |
| 9 | +use MongoDB\Client; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\Stopwatch\Stopwatch; |
| 12 | + |
| 13 | +use function array_keys; |
| 14 | +use function serialize; |
| 15 | +use function spl_object_id; |
| 16 | +use function unserialize; |
| 17 | + |
| 18 | +class MongoDBDataCollectorTest extends TestCase |
| 19 | +{ |
| 20 | + public function testCollectMultipleClients(): void |
| 21 | + { |
| 22 | + $client1 = new Client($_SERVER['MONGODB_PRIMARY_URL']); |
| 23 | + $client2 = new Client($_SERVER['MONGODB_PRIMARY_URL']); |
| 24 | + $stopwatch = new Stopwatch(); |
| 25 | + |
| 26 | + $dataCollector = new MongoDBDataCollector($stopwatch, new ArrayIterator(['client1' => $client1, 'client2' => $client2])); |
| 27 | + |
| 28 | + $client1Id = spl_object_id($client1); |
| 29 | + $client2Id = spl_object_id($client2); |
| 30 | + |
| 31 | + // Successful command on Client 1 |
| 32 | + $dataCollector->collectCommandEvent($client1Id, 'request1', [ |
| 33 | + 'clientName' => 'client1', |
| 34 | + 'databaseName' => 'database1', |
| 35 | + 'commandName' => 'find', |
| 36 | + 'command' => ['find' => 'collection1', 'filter' => []], |
| 37 | + ]); |
| 38 | + $dataCollector->collectCommandEvent($client1Id, 'request1', ['durationMicros' => 1000]); |
| 39 | + |
| 40 | + // Error on Client 1 |
| 41 | + $dataCollector->collectCommandEvent($client1Id, 'request2', [ |
| 42 | + 'clientName' => 'client1', |
| 43 | + 'databaseName' => 'database1', |
| 44 | + 'commandName' => 'insert', |
| 45 | + 'command' => ['insert' => 'collection1', 'documents' => []], |
| 46 | + ]); |
| 47 | + $dataCollector->collectCommandEvent($client1Id, 'request2', [ |
| 48 | + 'durationMicros' => 500, |
| 49 | + 'error' => 'Error message', |
| 50 | + ]); |
| 51 | + |
| 52 | + // Successful command on Client 2 |
| 53 | + $dataCollector->collectCommandEvent($client2Id, 'request3', [ |
| 54 | + 'clientName' => 'client2', |
| 55 | + 'databaseName' => 'database2', |
| 56 | + 'commandName' => 'aggregate', |
| 57 | + 'command' => ['aggregate' => 'collection2', 'pipeline' => []], |
| 58 | + ]); |
| 59 | + $dataCollector->collectCommandEvent($client2Id, 'request3', ['durationMicros' => 800]); |
| 60 | + |
| 61 | + $dataCollector->lateCollect(); |
| 62 | + |
| 63 | + // Data is serialized and unserialized by the profiler |
| 64 | + $dataCollector = unserialize(serialize($dataCollector)); |
| 65 | + |
| 66 | + $this->assertSame('mongodb', $dataCollector->getName()); |
| 67 | + $this->assertCount(2, $dataCollector->getClients()); |
| 68 | + $this->assertSame(2300, $dataCollector->getTime()); |
| 69 | + $this->assertSame(3, $dataCollector->getRequestCount()); |
| 70 | + $this->assertSame(1, $dataCollector->getErrorCount()); |
| 71 | + |
| 72 | + $requests = $dataCollector->getRequests(); |
| 73 | + $this->assertSame(['client1', 'client2'], array_keys($requests)); |
| 74 | + $this->assertSame([ |
| 75 | + 'request1' => [ |
| 76 | + 'clientName' => 'client1', |
| 77 | + 'databaseName' => 'database1', |
| 78 | + 'commandName' => 'find', |
| 79 | + 'command' => ['find' => 'collection1', 'filter' => []], |
| 80 | + 'durationMicros' => 1000, |
| 81 | + ], |
| 82 | + 'request2' => [ |
| 83 | + 'clientName' => 'client1', |
| 84 | + 'databaseName' => 'database1', |
| 85 | + 'commandName' => 'insert', |
| 86 | + 'command' => ['insert' => 'collection1', 'documents' => []], |
| 87 | + 'durationMicros' => 500, |
| 88 | + 'error' => 'Error message', |
| 89 | + ], |
| 90 | + ], $requests['client1']); |
| 91 | + $this->assertSame([ |
| 92 | + 'request3' => [ |
| 93 | + 'clientName' => 'client2', |
| 94 | + 'databaseName' => 'database2', |
| 95 | + 'commandName' => 'aggregate', |
| 96 | + 'command' => ['aggregate' => 'collection2', 'pipeline' => []], |
| 97 | + 'durationMicros' => 800, |
| 98 | + ], |
| 99 | + ], $requests['client2']); |
| 100 | + |
| 101 | + $clients = $dataCollector->getClients(); |
| 102 | + $this->assertSame(['client1', 'client2'], array_keys($clients)); |
| 103 | + $this->assertSame(['serverBuildInfo', 'clientInfo'], array_keys($clients['client1'])); |
| 104 | + } |
| 105 | +} |
0 commit comments