@@ -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