Skip to content

Commit 9765a13

Browse files
authored
Merge pull request #9 from colinodell/mock-client
Add MockClient to simplify downstream testing
2 parents 064d76e + 2604277 commit 9765a13

File tree

4 files changed

+464
-1
lines changed

4 files changed

+464
-1
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ install:
2828
- composer install --no-interaction
2929

3030
script:
31-
- php vendor/bin/phpunit --testsuite=functional
31+
- php vendor/bin/phpunit

phpunit.xml.dist

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<testsuite name="functional">
1111
<directory suffix="Test.php">tests/Functional</directory>
1212
</testsuite>
13+
<testsuite name="unit">
14+
<directory suffix="Test.php">tests/Unit</directory>
15+
</testsuite>
1316
</testsuites>
1417
<php>
1518
<server name="KERNEL_DIR" value="tests/Functional/App"/>

src/Client/MockClient.php

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Okvpn\Bundle\DatadogBundle\Client;
6+
7+
final class MockClient implements DogStatsInterface
8+
{
9+
private $data = [];
10+
11+
public function increment(string $metrics, int $delta = 1, float $sampleRate = 1.0, array $tags = [])
12+
{
13+
$this->data['increments'][] = [
14+
'metric' => $metrics,
15+
'delta' => $delta,
16+
'sampleRate' => $sampleRate,
17+
'tags' => $tags,
18+
];
19+
20+
return $this;
21+
}
22+
23+
public function getIncrements(string $metric = null): array
24+
{
25+
return $this->get('increments', $metric);
26+
}
27+
28+
public function decrement(string $metric, int $delta = 1, float $sampleRate = 1.0, array $tags = [])
29+
{
30+
$this->data['decrements'][] = [
31+
'metric' => $metric,
32+
'delta' => $delta,
33+
'sampleRate' => $sampleRate,
34+
'tags' => $tags,
35+
];
36+
37+
return $this;
38+
}
39+
40+
public function getDecrements(string $metric = null): array
41+
{
42+
return $this->get('decrements', $metric);
43+
}
44+
45+
public function timing(string $metric, float $time, array $tags = [])
46+
{
47+
$this->data['timings'][] = [
48+
'metric' => $metric,
49+
'time' => $time,
50+
'tags' => $tags,
51+
];
52+
53+
return $this;
54+
}
55+
56+
public function getTimings(string $metric = null): array
57+
{
58+
return $this->get('timings', $metric);
59+
}
60+
61+
public function time(string $metric, callable $func, array $tags = [])
62+
{
63+
$timerStart = microtime(true);
64+
$func();
65+
$timerEnd = microtime(true);
66+
$time = round(($timerEnd - $timerStart) * 1000, 4);
67+
68+
return $this->timing($metric, $time, $tags);
69+
}
70+
71+
public function gauge(string $metric, int $value, array $tags = [])
72+
{
73+
$this->data['gauges'][] = [
74+
'metric' => $metric,
75+
'value' => $value,
76+
'tags' => $tags,
77+
];
78+
79+
return $this;
80+
}
81+
82+
public function getGauges(string $metric = null): array
83+
{
84+
return $this->get('gauges', $metric);
85+
}
86+
87+
public function histogram(string $metric, float $value, float $sampleRate = 1.0, array $tags = [])
88+
{
89+
$this->data['histograms'][] = [
90+
'metric' => $metric,
91+
'value' => $value,
92+
'sampleRate' => $sampleRate,
93+
'tags' => $tags,
94+
];
95+
96+
return $this;
97+
}
98+
99+
public function getHistograms(string $metric = null): array
100+
{
101+
return $this->get('histograms', $metric);
102+
}
103+
104+
public function set(string $metric, int $value, array $tags = [])
105+
{
106+
$this->data['sets'][] = [
107+
'metric' => $metric,
108+
'value' => $value,
109+
'tags' => $tags,
110+
];
111+
112+
return $this;
113+
}
114+
115+
public function getSets(string $metric = null): array
116+
{
117+
return $this->get('sets', $metric);
118+
}
119+
120+
public function event(string $title, string $text, array $metadata = [], array $tags = [])
121+
{
122+
$this->data['events'][] = [
123+
'title' => $title,
124+
'text' => $text,
125+
'metadata' => $metadata,
126+
'tags' => $tags,
127+
];
128+
129+
return $this;
130+
}
131+
132+
public function getEvents(string $title = null): array
133+
{
134+
return $this->get('events', $title);
135+
}
136+
137+
public function serviceCheck(string $name, int $status, array $metadata = [], array $tags = [])
138+
{
139+
$this->data['serviceChecks'][] = [
140+
'name' => $name,
141+
'status' => $status,
142+
'metadata' => $metadata,
143+
'tags' => $tags,
144+
];
145+
146+
return $this;
147+
}
148+
149+
public function getServiceChecks(string $name = null): array
150+
{
151+
return $this->get('serviceChecks', $name);
152+
}
153+
154+
public function getOptions(): array
155+
{
156+
return [];
157+
}
158+
159+
public function getOption(string $name, $default = null)
160+
{
161+
return $default;
162+
}
163+
164+
private function get(string $type, string $metric = null): array
165+
{
166+
if ($metric === null) {
167+
return $this->data[$type];
168+
}
169+
170+
if ($type === 'events') {
171+
$key = 'title';
172+
} elseif ($type === 'serviceChecks') {
173+
$key = 'name';
174+
} else {
175+
$key = 'metric';
176+
}
177+
178+
return array_filter($this->data[$type], static function (array $data) use ($key, $metric) {
179+
return $data[$key] === $metric;
180+
});
181+
}
182+
}

0 commit comments

Comments
 (0)