Cachet/tests/Api/MetricPointTest.php

142 lines
4.2 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Api;
2015-11-07 15:37:06 +00:00
use Carbon\Carbon;
2015-12-06 11:04:02 +00:00
/**
* This is the metric point test class.
*
* @author James Brooks <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/
class MetricPointTest extends AbstractApiTestCase
{
public function testGetMetricPoint()
{
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint', 3)->create([
'metric_id' => $metric->id,
]);
$this->get("/api/v1/metrics/{$metric->id}/points");
2016-03-02 12:09:57 +00:00
2017-06-24 18:52:55 +01:00
$this->seeJsonContains(['id' => $metricPoint[0]->id]);
$this->seeJsonContains(['id' => $metricPoint[1]->id]);
$this->seeJsonContains(['id' => $metricPoint[2]->id]);
2016-03-02 12:09:57 +00:00
$this->assertResponseOk();
}
public function testPostMetricPointUnauthorized()
{
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create([
'metric_id' => $metric->id,
]);
$this->post("/api/v1/metrics/{$metric->id}/points");
$this->assertResponseStatus(401);
}
public function testPostMetricPoint()
{
$this->beUser();
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->make([
'metric_id' => $metric->id,
]);
$this->post("/api/v1/metrics/{$metric->id}/points", $metricPoint->toArray());
2016-03-02 12:09:57 +00:00
2017-06-24 18:52:55 +01:00
$this->seeJsonContains(['value' => $metricPoint->value]);
2016-03-02 12:09:57 +00:00
$this->assertResponseOk();
}
public function testPostMetricPointTimestamp()
{
$this->beUser();
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$timestamp = 1434369116;
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->make([
'metric_id' => $metric->id,
]);
$postData = $metricPoint->toArray();
$postData['timestamp'] = $timestamp;
$this->post("/api/v1/metrics/{$metric->id}/points", $postData);
2016-03-02 12:09:57 +00:00
$this->seeJsonContains([
'value' => $metricPoint->value,
'created_at' => date('Y-m-d H:i:s', 1434369116),
]);
2016-03-02 12:09:57 +00:00
$this->assertResponseOk();
}
public function testPostMetricPointTimestampTimezone()
{
$this->beUser();
// prevent tests breaking due to rolling into the next second
Carbon::setTestNow(Carbon::now());
$timezone = 'America/Mexico_City';
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
2016-01-29 22:49:06 +00:00
$datetime = Carbon::now()->timezone($timezone);
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->make([
'metric_id' => $metric->id,
]);
$postData = $metricPoint->toArray();
2016-01-29 22:49:06 +00:00
$postData['timestamp'] = $datetime->timestamp;
$this->post("/api/v1/metrics/{$metric->id}/points", $postData, ['Time-Zone' => $timezone]);
2016-03-02 12:09:57 +00:00
2017-06-24 18:52:55 +01:00
$this->seeJsonContains(['value' => $metricPoint->value, 'created_at' => $datetime->toDateTimeString()]);
2016-03-02 12:09:57 +00:00
$this->assertResponseOk();
}
public function testPutMetricPoint()
{
$this->beUser();
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create([
'metric_id' => $metric->id,
]);
2016-03-02 12:09:57 +00:00
$this->put("/api/v1/metrics/{$metric->id}/points/{$metricPoint->id}", [
'value' => 999,
]);
2016-03-02 12:09:57 +00:00
2017-06-24 18:52:55 +01:00
$this->seeJsonContains(['value' => 999]);
2016-03-02 12:09:57 +00:00
$this->assertResponseOk();
}
public function testDeleteMetricPoint()
{
$this->beUser();
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create([
'metric_id' => $metric->id,
]);
2016-03-02 12:09:57 +00:00
$this->delete("/api/v1/metrics/{$metric->id}/points/{$metricPoint->id}");
2016-03-02 12:09:57 +00:00
$this->assertResponseStatus(204);
}
}