diff --git a/app/Models/MetricPoint.php b/app/Models/MetricPoint.php index f06832e8..88901f16 100644 --- a/app/Models/MetricPoint.php +++ b/app/Models/MetricPoint.php @@ -92,13 +92,17 @@ class MetricPoint extends Model implements HasPresenter /** * Round the created at value into intervals of 30 seconds. * - * @param string $value + * @param string $createdAt * - * @return void + * @return string|void */ - public function setCreatedAtAttribute($value) + public function setCreatedAtAttribute($createdAt) { - $timestamp = $value->format('U'); + if (!$createdAt) return; + + $createdAt = Carbon::parse($createdAt); + + $timestamp = $createdAt->format('U'); $timestamp = 30 * round($timestamp / 30); return Carbon::createFromFormat('U', $timestamp)->toDateTimeString(); diff --git a/tests/Api/MetricPointTest.php b/tests/Api/MetricPointTest.php index b11d3cc1..a603c9ae 100644 --- a/tests/Api/MetricPointTest.php +++ b/tests/Api/MetricPointTest.php @@ -78,11 +78,12 @@ class MetricPointTest extends AbstractApiTestCase $postData['timestamp'] = $timestamp; $response = $this->json('POST', "/api/v1/metrics/{$metric->id}/points", $postData); + $response->dump(); $response->assertStatus(200); $response->assertJsonFragment([ 'value' => $metricPoint->value, - 'created_at' => date('Y-m-d H:i:s', 1434369116), + 'created_at' => date('Y-m-d H:i:00', 1434369116), ]); }