Skip to content

Commit 3181bec

Browse files
authored
Merge pull request #55 from ingenerator/add_single_timer_assertion
Add assertCapturedOneExactTimer helper.
2 parents 8f835c3 + 590d507 commit 3181bec

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
### Unreleased
22

3+
### v1.19.2 (2023-04-27)
4+
5+
* Add assertCapturedOneExactTimer helper. Allowing you to simultaneously assert only
6+
one timer was recorded and the timer duration in a single method.
7+
38
### v1.19.1 (2023-03-22)
49

510
* Fix a bug in UniqueMap where it throws on attempt to access an offset with a null value

src/Monitoring/AssertMetrics.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function assertCapturedOneTimer(
4444
);
4545
}
4646

47-
private static function assertOnlyOneMetricTypeAndPayload(array $metrics, string $type, $expected_payload)
47+
private static function assertOnlyOneMetricTypeAndPayload(array $metrics, string $type, $expected_payload): void
4848
{
4949
Assert::assertCount(1, $metrics, 'Expected only 1 metric recorded');
5050
Assert::assertSame($type, $metrics[0]['type'], 'Expected metric to be of type '.$type);
@@ -74,7 +74,7 @@ public static function assertNoMetricsCaptured(array $metrics): void
7474
Assert::assertEmpty($metrics, "Expected no metrics to be captured");
7575
}
7676

77-
public static function assertNoMetricsFor(array $metrics, string $metric_name)
77+
public static function assertNoMetricsFor(array $metrics, string $metric_name): void
7878
{
7979
Assert::assertNotContains(
8080
$metric_name,
@@ -83,12 +83,26 @@ public static function assertNoMetricsFor(array $metrics, string $metric_name)
8383
);
8484
}
8585

86+
public static function assertCapturedOneExactTimer(
87+
array $metrics,
88+
float $expect_millis,
89+
string $name,
90+
?string $source = NULL
91+
): void {
92+
AssertMetrics::assertCapturedOneTimer($metrics, $name, $source);
93+
AssertMetrics::assertTimerValues(
94+
$metrics,
95+
MetricId::nameAndSource($name, $source),
96+
[$expect_millis]
97+
);
98+
}
99+
86100
public static function assertTimerValues(
87101
array $metrics,
88102
MetricId $metric,
89103
array $expected_times,
90104
float $tolerance_ms = 0
91-
) {
105+
): void {
92106
$filtered_metrics = array_values(
93107
array_filter($metrics,
94108
fn($m) => ($m['type'] === 'timer' and

test/unit/Monitoring/AssertMetricsTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,55 @@ public function test_assertTimerValues(
102102
);
103103
}
104104

105+
/**
106+
* @testWith [[], "no metrics"]
107+
* [[{"name": "foo", "source": "wrong"}], "wrong source"]
108+
* [[{"name": "wrong", "source": "bar"}], "wrong name"]
109+
* [[{"name": "foo", "source": "bar"}, {"name": "foo", "source": "bar"}], "duplicate metric"]
110+
* [[{"name": "foo", "source": "bar"}, {"name": "foo", "source": "other"}], "extra metric"]
111+
**/
112+
public function test_assertCapturedOneExactTimer_fails_if_no_match(array $metrics)
113+
{
114+
$agent = new ArrayMetricsAgent();
115+
foreach ($metrics as $metric) {
116+
$agent->addTimer(
117+
MetricId::nameAndSource($metric['name'], $metric['source']),
118+
new DateTimeImmutable(),
119+
new DateTimeImmutable()
120+
);
121+
}
122+
try {
123+
AssertMetrics::assertCapturedOneExactTimer($agent->getMetrics(), 0,'foo', 'bar');
124+
$this->fail('Expected assertion failure');
125+
} catch (ExpectationFailedException $e) {
126+
// we're expecting this - increment the assertion count
127+
$this->assertTrue(true);
128+
}
129+
}
130+
131+
/**
132+
* @testWith ["2020-02-02 02:02:02.123456", "2020-02-02 02:02:02.123456", 0, true]
133+
* ["2020-02-02 02:02:02.123456", "2020-02-02 02:02:02.123456", 0.001, false]
134+
* ["2020-02-02 02:02:02.123456", "2020-02-02 02:02:02.123457", 0.001, true]
135+
* ["2020-02-02 02:02:02.123456", "2020-02-02 02:02:02.123455", 0.001, false]
136+
*/
137+
public function test_assertCapturedOneExactTimer_asserts_correct_time(
138+
string $start,
139+
string $end,
140+
float $expected_time,
141+
bool $expect_success
142+
) {
143+
$agent = new ArrayMetricsAgent();
144+
$metric = MetricId::nameAndSource('timer', 'test');
145+
$agent->addTimer($metric, new DateTimeImmutable($start), new DateTimeImmutable($end));
146+
$this->assertAssertionResult(
147+
$expect_success,
148+
fn() => AssertMetrics::assertCapturedOneExactTimer(
149+
$agent->getMetrics(),
150+
$expected_time, 'timer', 'test')
151+
);
152+
}
153+
105154
/**
106155
* @testWith ["something", "test", true]
107156
* ["something_else", "test", false]

0 commit comments

Comments
 (0)