2015-06-02 20:02:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-06-02 20:02:43 +01:00
|
|
|
*
|
|
|
|
* 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-06-02 20:02:43 +01:00
|
|
|
|
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
|
2015-06-02 20:02:43 +01:00
|
|
|
{
|
2015-05-23 19:09:24 +01:00
|
|
|
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();
|
2015-05-23 19:09:24 +01:00
|
|
|
}
|
|
|
|
|
2015-06-02 20:02:43 +01:00
|
|
|
public function testPostMetricPointUnauthorized()
|
|
|
|
{
|
2015-05-23 19:09:24 +01:00
|
|
|
$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");
|
2015-06-02 20:02:43 +01:00
|
|
|
|
|
|
|
$this->assertResponseStatus(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPostMetricPoint()
|
|
|
|
{
|
|
|
|
$this->beUser();
|
|
|
|
|
2015-05-23 19:09:24 +01:00
|
|
|
$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();
|
2015-06-02 20:02:43 +01:00
|
|
|
}
|
|
|
|
|
2015-06-15 12:57:12 +01:00
|
|
|
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
|
|
|
|
2017-07-17 21:21:06 +01: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();
|
2015-06-15 12:57:12 +01:00
|
|
|
}
|
|
|
|
|
2015-11-07 13:20:06 +00:00
|
|
|
public function testPostMetricPointTimestampTimezone()
|
|
|
|
{
|
|
|
|
$this->beUser();
|
|
|
|
|
2015-12-21 22:40:11 +00:00
|
|
|
// prevent tests breaking due to rolling into the next second
|
|
|
|
Carbon::setTestNow(Carbon::now());
|
|
|
|
|
2015-11-07 13:20:06 +00:00
|
|
|
$timezone = 'America/Mexico_City';
|
|
|
|
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
|
2016-01-29 22:49:06 +00:00
|
|
|
$datetime = Carbon::now()->timezone($timezone);
|
2015-11-07 13:20:06 +00:00
|
|
|
$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;
|
2015-11-07 13:20:06 +00:00
|
|
|
|
|
|
|
$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();
|
2015-11-07 13:20:06 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 20:02:43 +01:00
|
|
|
public function testPutMetricPoint()
|
|
|
|
{
|
|
|
|
$this->beUser();
|
2015-05-23 19:09:24 +01:00
|
|
|
$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
|
|
|
|
2015-05-23 19:09:24 +01:00
|
|
|
$this->put("/api/v1/metrics/{$metric->id}/points/{$metricPoint->id}", [
|
2015-06-02 20:02:43 +01:00
|
|
|
'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();
|
2015-06-02 20:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteMetricPoint()
|
|
|
|
{
|
|
|
|
$this->beUser();
|
2015-05-23 19:09:24 +01:00
|
|
|
$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
|
|
|
|
2015-05-23 19:09:24 +01:00
|
|
|
$this->delete("/api/v1/metrics/{$metric->id}/points/{$metricPoint->id}");
|
2016-03-02 12:09:57 +00:00
|
|
|
|
2015-06-02 20:02:43 +01:00
|
|
|
$this->assertResponseStatus(204);
|
|
|
|
}
|
|
|
|
}
|