|
6 | 6 |
|
7 | 7 | use PHPUnit\Framework\TestCase; |
8 | 8 | use Sentry\Client; |
| 9 | +use Sentry\ClientInterface; |
| 10 | +use Sentry\Event; |
9 | 11 | use Sentry\Metrics\MetricsAggregator; |
10 | 12 | use Sentry\Metrics\Types\CounterMetric; |
11 | 13 | use Sentry\Metrics\Types\DistributionMetric; |
12 | 14 | use Sentry\Metrics\Types\GaugeMetric; |
13 | 15 | use Sentry\Metrics\Types\Metric; |
14 | 16 | use Sentry\Options; |
| 17 | +use Sentry\SentrySdk; |
| 18 | +use Sentry\State\Hub; |
15 | 19 | use Sentry\State\HubAdapter; |
16 | 20 | use Sentry\State\Scope; |
17 | 21 |
|
@@ -110,6 +114,39 @@ public function testDoesNotFlushImmediatelyWhenMetricFlushThresholdIsNull(): voi |
110 | 114 | $this->assertCount(2, StubTransport::$events[0]->getMetrics()); |
111 | 115 | } |
112 | 116 |
|
| 117 | + public function testFlushCapturesMetricsWithProvidedClient(): void |
| 118 | + { |
| 119 | + $client = $this->createMock(ClientInterface::class); |
| 120 | + $client->method('getOptions') |
| 121 | + ->willReturn(new Options([ |
| 122 | + 'enable_metrics' => true, |
| 123 | + ])); |
| 124 | + |
| 125 | + $fallbackClient = $this->createMock(ClientInterface::class); |
| 126 | + $fallbackClient->method('getOptions') |
| 127 | + ->willReturn(new Options([ |
| 128 | + 'enable_metrics' => true, |
| 129 | + ])); |
| 130 | + $fallbackClient->expects($this->never()) |
| 131 | + ->method('captureEvent'); |
| 132 | + SentrySdk::setCurrentHub(new Hub($fallbackClient)); |
| 133 | + |
| 134 | + $aggregator = new MetricsAggregator(); |
| 135 | + $aggregator->add(CounterMetric::TYPE, 'test-count', 2, ['foo' => 'bar'], null); |
| 136 | + |
| 137 | + $client->expects($this->once()) |
| 138 | + ->method('captureEvent') |
| 139 | + ->with( |
| 140 | + $this->callback(function (Event $event): bool { |
| 141 | + $this->assertCount(1, $event->getMetrics()); |
| 142 | + |
| 143 | + return true; |
| 144 | + }) |
| 145 | + ); |
| 146 | + |
| 147 | + $aggregator->flush($client); |
| 148 | + } |
| 149 | + |
113 | 150 | public function testMetricsBufferFullWhenMetricFlushThresholdIsNull(): void |
114 | 151 | { |
115 | 152 | HubAdapter::getInstance()->bindClient(new Client(new Options([ |
|
0 commit comments