@@ -22,7 +22,7 @@ class RewriterTest extends \PHPUnit\Framework\TestCase
2222 * @covers ::rewrite
2323 * @dataProvider influxDbMessages
2424 */
25- public function testRewrite (string $ message , string $ prefix , array $ tags )
25+ public function testRewrite (string $ message , string $ prefix , float $ time , array $ tags )
2626 {
2727 $ dd = $ this ->createMock (DogStatsd::class);
2828 $ dd ->method ('microtiming ' )
@@ -38,6 +38,7 @@ public function testRewrite(string $message, string $prefix, array $tags)
3838 $ rewriter ->rewrite ($ message );
3939
4040 $ this ->expectIncrement ('request.count ' );
41+ $ this ->expectTimer ('request.duration ' , $ time );
4142 $ this ->performAssertions ($ prefix , $ tags );
4243 }
4344
@@ -51,6 +52,7 @@ public function influxDbMessages(): array
5152 'content_type="text/plain; charset=utf-8",request_time=0.047 ' ,
5253
5354 'default ' ,
55+ 0.047 ,
5456 ['code ' => '500 ' ,
5557 'method ' => 'GET ' ,
5658 'server_name ' => 'amp.reefpig.com ' ,
@@ -64,6 +66,11 @@ private function expectIncrement(string $metric)
6466 $ this ->expectedIncrements [] = $ metric ;
6567 }
6668
69+ private function expectTimer (string $ metric , float $ seconds )
70+ {
71+ $ this ->expectedTimers [] = [$ metric , $ seconds ];
72+ }
73+
6774 private function performAssertions (string $ prefix , array $ tags )
6875 {
6976 $ this ->assertCount (count ($ this ->expectedIncrements ), $ this ->increments , 'Incorrect increment count ' );
@@ -78,12 +85,27 @@ private function performAssertions(string $prefix, array $tags)
7885 break ;
7986 }
8087 }
81- if (!$ found ) {
82- $ this ->fail (sprintf ('Expected increment metric %s not found ' , $ metric ));
83- }
88+ $ this ->assertNotFalse ($ found , sprintf ('Expected increment metric %s not found ' , $ metric ));
8489 ksort ($ tags );
8590 ksort ($ found [2 ]);
8691 $ this ->assertEquals ($ tags , $ found [2 ], 'Tags do not match ' );
8792 }
93+
94+ foreach ($ this ->expectedTimers as $ et ) {
95+ list ($ suffix , $ seconds ) = $ et ;
96+ $ metric = sprintf ('%s.%s ' , $ prefix , $ suffix );
97+ $ found = false ;
98+ foreach ($ this ->timers as $ timer ) {
99+ if ($ timer [0 ] === $ metric ) {
100+ $ found = $ timer ;
101+ break ;
102+ }
103+ }
104+ $ this ->assertNotFalse ($ found , sprintf ('Timer metric %s not found ' , $ metric ));
105+ $ this ->assertEquals ($ seconds , $ found [1 ], 'Incorrect timing value ' );
106+ ksort ($ tags );
107+ ksort ($ found [3 ]);
108+ $ this ->assertEquals ($ tags , $ found [3 ], 'Tags do not match ' );
109+ }
88110 }
89111}
0 commit comments