From 60ec8b766b1291bff267fc4641d082044595732a Mon Sep 17 00:00:00 2001 From: Joe Cohen Date: Tue, 3 Jul 2018 13:08:41 -0500 Subject: [PATCH 1/3] test metric points by latests --- tests/Api/MetricPointTest.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/Api/MetricPointTest.php b/tests/Api/MetricPointTest.php index 7f600fe5..d30e6bca 100644 --- a/tests/Api/MetricPointTest.php +++ b/tests/Api/MetricPointTest.php @@ -101,19 +101,22 @@ class MetricPointTest extends AbstractApiTestCase $this->beUser(); $metric = factory(Metric::class)->create(); - $timestamp = strtotime('now'); + $createdAt = Carbon::now(); $metricPoint = factory(MetricPoint::class)->make([ 'metric_id' => $metric->id, ]); $postData = $metricPoint->toArray(); - $postData['timestamp'] = $timestamp; + $postData['timestamp'] = $createdAt->timestamp; $response = $this->json('POST', "/api/v1/metrics/{$metric->id}/points", $postData); + // Round value to match ours + $timestamp = 30 * round($createdAt->timestamp / 30); + $response->assertStatus(200); $response->assertJsonFragment([ 'value' => $metricPoint->value, - 'created_at' => date('Y-m-d H:i:s', $timestamp), + 'created_at' => Carbon::createFromFormat('U', $timestamp)->setTimezone('Europe/London')->toDateTimeString(), ]); } @@ -126,17 +129,23 @@ class MetricPointTest extends AbstractApiTestCase $timezone = 'America/Mexico_City'; $metric = factory(Metric::class)->create(); - $datetime = Carbon::now()->timezone($timezone); + $createdAt = Carbon::now()->timezone($timezone); $metricPoint = factory(MetricPoint::class)->make([ 'metric_id' => $metric->id, ]); $postData = $metricPoint->toArray(); - $postData['timestamp'] = $datetime->timestamp; + $postData['timestamp'] = $createdAt->timestamp; $response = $this->json('POST', "/api/v1/metrics/{$metric->id}/points", $postData, ['Time-Zone' => $timezone]); + // Round value to match ours + $timestamp = 30 * round($createdAt->timestamp / 30); + $response->assertStatus(200); - $response->assertJsonFragment(['value' => $metricPoint->value, 'created_at' => $datetime->toDateTimeString()]); + $response->assertJsonFragment([ + 'value' => $metricPoint->value, + 'created_at' => Carbon::createFromFormat('U', $timestamp)->setTimezone($timezone)->toDateTimeString(), + ]); } public function test_can_update_metric_point() From 6a25bbddcee3602406980d084b711cfc846db69c Mon Sep 17 00:00:00 2001 From: Joe Cohen Date: Tue, 3 Jul 2018 18:08:57 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI [ci skip] [skip ci] --- tests/Api/MetricPointTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Api/MetricPointTest.php b/tests/Api/MetricPointTest.php index d30e6bca..ed19fb47 100644 --- a/tests/Api/MetricPointTest.php +++ b/tests/Api/MetricPointTest.php @@ -143,7 +143,7 @@ class MetricPointTest extends AbstractApiTestCase $response->assertStatus(200); $response->assertJsonFragment([ - 'value' => $metricPoint->value, + 'value' => $metricPoint->value, 'created_at' => Carbon::createFromFormat('U', $timestamp)->setTimezone($timezone)->toDateTimeString(), ]); } From edc9a45e53b303128973a61ffbf3662acc2eddef Mon Sep 17 00:00:00 2001 From: Joe Cohen Date: Tue, 3 Jul 2018 13:29:16 -0500 Subject: [PATCH 3/3] Set test now and use config date --- tests/Api/MetricPointTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Api/MetricPointTest.php b/tests/Api/MetricPointTest.php index ed19fb47..c9f4ffdf 100644 --- a/tests/Api/MetricPointTest.php +++ b/tests/Api/MetricPointTest.php @@ -100,6 +100,9 @@ class MetricPointTest extends AbstractApiTestCase { $this->beUser(); + // prevent tests breaking due to rolling into the next second + Carbon::setTestNow(Carbon::now()); + $metric = factory(Metric::class)->create(); $createdAt = Carbon::now(); $metricPoint = factory(MetricPoint::class)->make([ @@ -116,7 +119,7 @@ class MetricPointTest extends AbstractApiTestCase $response->assertStatus(200); $response->assertJsonFragment([ 'value' => $metricPoint->value, - 'created_at' => Carbon::createFromFormat('U', $timestamp)->setTimezone('Europe/London')->toDateTimeString(), + 'created_at' => Carbon::createFromFormat('U', $timestamp)->setTimezone(config('cachet.timezone'))->toDateTimeString(), ]); }