Added casts properties to all models, re-ordered model properties. Fixes #916

This commit is contained in:
James Brooks
2015-08-22 15:57:53 +01:00
parent 92192232ed
commit a77efc12e0
15 changed files with 182 additions and 94 deletions
+32 -19
View File
@@ -23,16 +23,37 @@ class Component extends Model implements HasPresenter
use SoftDeletes, ValidatingTrait; use SoftDeletes, ValidatingTrait;
/** /**
* The validation rules. * List of attributes that have default values.
*
* @var mixed[]
*/
protected $attributes = [
'order' => 0,
'group_id' => 0,
'description' => '',
'link' => '',
];
/**
* The attributes that should be casted to native types.
* *
* @var string[] * @var string[]
*/ */
public $rules = [ protected $casts = [
'name' => 'required|string', 'id' => 'int',
'status' => 'integer|required', 'order' => 'int',
'link' => 'url', 'group_id' => 'int',
'description' => 'string',
'link' => 'string',
]; ];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/** /**
* The fillable properties. * The fillable properties.
* *
@@ -49,24 +70,16 @@ class Component extends Model implements HasPresenter
]; ];
/** /**
* List of attributes that have default values. * The validation rules.
* *
* @var mixed[] * @var string[]
*/ */
protected $attributes = [ public $rules = [
'order' => 0, 'name' => 'required|string',
'group_id' => 0, 'status' => 'integer|required',
'description' => '', 'link' => 'url',
'link' => '',
]; ];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/** /**
* Components can belong to a group. * Components can belong to a group.
* *
+2 -1
View File
@@ -24,8 +24,9 @@ class ComponentGroup extends Model
* @var string[] * @var string[]
*/ */
protected $casts = [ protected $casts = [
'id' => 'int',
'name' => 'string', 'name' => 'string',
'order' => 'integer', 'order' => 'int',
]; ];
/** /**
+25 -24
View File
@@ -23,18 +23,29 @@ class Incident extends Model implements HasPresenter
use SoftDeletes, ValidatingTrait; use SoftDeletes, ValidatingTrait;
/** /**
* The validation rules. * The accessors to append to the model's serialized form.
* *
* @var string[] * @var string[]
*/ */
public $rules = [ protected $appends = ['human_status'];
'component_id' => 'integer',
'name' => 'required', /**
'status' => 'required|integer', * The attributes that should be casted to native types.
'visible' => 'required|boolean', *
'message' => 'required', * @var string[]
*/
protected $casts = [
'id' => 'int',
'visible' => 'integer',
]; ];
/**
* The attributes that should be mutated to dates.
*
* @var string[]
*/
protected $dates = ['scheduled_at', 'deleted_at'];
/** /**
* The fillable properties. * The fillable properties.
* *
@@ -52,26 +63,16 @@ class Incident extends Model implements HasPresenter
]; ];
/** /**
* The accessors to append to the model's serialized form. * The validation rules.
* *
* @var string[] * @var string[]
*/ */
protected $appends = ['human_status']; public $rules = [
'component_id' => 'integer',
/** 'name' => 'required',
* The attributes that should be mutated to dates. 'status' => 'required|integer',
* 'visible' => 'required|boolean',
* @var string[] 'message' => 'required',
*/
protected $dates = ['scheduled_at', 'deleted_at'];
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'visible' => 'integer',
]; ];
/** /**
+15 -4
View File
@@ -20,13 +20,14 @@ class IncidentTemplate extends Model
use ValidatingTrait; use ValidatingTrait;
/** /**
* The validation rules. * The attributes that should be casted to native types.
* *
* @var string[] * @var string[]
*/ */
public $rules = [ protected $casts = [
'name' => 'required', 'id' => 'int',
'template' => 'required', 'name' => 'string',
'template' => 'string',
]; ];
/** /**
@@ -36,6 +37,16 @@ class IncidentTemplate extends Model
*/ */
protected $fillable = ['name', 'template']; protected $fillable = ['name', 'template'];
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => 'required',
'template' => 'required',
];
/** /**
* Overrides the models boot method. * Overrides the models boot method.
*/ */
+21 -7
View File
@@ -37,16 +37,17 @@ class Metric extends Model implements HasPresenter
]; ];
/** /**
* The validation rules. * The attributes that should be casted to native types.
* *
* @var string[] * @var string[]
*/ */
public $rules = [ protected $casts = [
'name' => 'required', 'id' => 'int',
'suffix' => 'required', 'name' => 'string',
'display_chart' => 'boolean', 'display_chart' => 'bool',
'default_value' => 'numeric', 'default_value' => 'int',
'places' => 'numeric|min:0|max:4', 'calc_type' => 'int',
'places' => 'int',
]; ];
/** /**
@@ -64,6 +65,19 @@ class Metric extends Model implements HasPresenter
'places', 'places',
]; ];
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => 'required',
'suffix' => 'required',
'display_chart' => 'boolean',
'default_value' => 'numeric',
'places' => 'numeric|min:0|max:4',
];
/** /**
* The relations to eager load on every query. * The relations to eager load on every query.
* *
+11
View File
@@ -20,6 +20,17 @@ class MetricPoint extends Model implements HasPresenter
{ {
use ValidatingTrait; use ValidatingTrait;
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'metric_id' => 'int',
'value' => 'int',
];
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
* *
+11
View File
@@ -15,6 +15,17 @@ use Illuminate\Database\Eloquent\Model;
class Setting extends Model class Setting extends Model
{ {
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'name' => 'string',
'value' => 'string',
];
/** /**
* The fillable properties. * The fillable properties.
* *
+15 -14
View File
@@ -21,21 +21,16 @@ class Subscriber extends Model implements HasPresenter
use ValidatingTrait; use ValidatingTrait;
/** /**
* The validation rules. * The attributes that should be casted to native types.
* *
* @var string[] * @var string[]
*/ */
public $rules = [ protected $casts = [
'email' => 'required|email', 'id' => 'int',
'email' => 'string',
'verify_code' => 'string',
]; ];
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = ['email'];
/** /**
* The attributes that should be mutated to dates. * The attributes that should be mutated to dates.
* *
@@ -44,13 +39,19 @@ class Subscriber extends Model implements HasPresenter
protected $dates = ['verified_at']; protected $dates = ['verified_at'];
/** /**
* The attributes that should be casted to native types. * The fillable properties.
* *
* @var string[] * @var string[]
*/ */
protected $casts = [ protected $fillable = ['email'];
'email' => 'string',
'verify_code' => 'string', /**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'email' => 'required|email',
]; ];
/** /**
+10
View File
@@ -16,6 +16,16 @@ use Illuminate\Support\Str;
class Tag extends Model class Tag extends Model
{ {
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'name' => 'string',
];
/** /**
* The fillable properties. * The fillable properties.
* *
+22 -7
View File
@@ -25,16 +25,27 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
use Authenticatable, CanResetPassword, ValidatingTrait; use Authenticatable, CanResetPassword, ValidatingTrait;
/** /**
* The validation rules. * The attributes that should be casted to native types.
* *
* @var string[] * @var string[]
*/ */
public $rules = [ protected $casts = [
'username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/'], 'id' => 'int',
'email' => 'required|email', 'username' => 'string',
'password' => 'required', 'email' => 'string',
'google_2fa_secret' => 'string',
'api_key' => 'string',
'active' => 'bool',
'level' => 'int',
]; ];
/**
* The properties that cannot be mass assigned.
*
* @var string[]
*/
protected $guarded = [];
/** /**
* The hidden properties. * The hidden properties.
* *
@@ -45,11 +56,15 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
protected $hidden = ['password', 'remember_token', 'google_2fa_secret']; protected $hidden = ['password', 'remember_token', 'google_2fa_secret'];
/** /**
* The properties that cannot be mass assigned. * The validation rules.
* *
* @var string[] * @var string[]
*/ */
protected $guarded = []; public $rules = [
'username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/'],
'email' => 'required|email',
'password' => 'required',
];
/** /**
* Overrides the models boot method. * Overrides the models boot method.
+3 -3
View File
@@ -23,9 +23,9 @@ class ComponentGroupTest extends AbstractTestCase
$groups = factory('CachetHQ\Cachet\Models\ComponentGroup', 3)->create(); $groups = factory('CachetHQ\Cachet\Models\ComponentGroup', 3)->create();
$this->get('/api/v1/components/groups'); $this->get('/api/v1/components/groups');
$this->seeJson(['id' => (string) $groups[0]->id]); $this->seeJson(['id' => $groups[0]->id]);
$this->seeJson(['id' => (string) $groups[1]->id]); $this->seeJson(['id' => $groups[1]->id]);
$this->seeJson(['id' => (string) $groups[2]->id]); $this->seeJson(['id' => $groups[2]->id]);
$this->assertResponseOk(); $this->assertResponseOk();
} }
+3 -3
View File
@@ -23,9 +23,9 @@ class ComponentTest extends AbstractTestCase
$components = factory('CachetHQ\Cachet\Models\Component', 3)->create(); $components = factory('CachetHQ\Cachet\Models\Component', 3)->create();
$this->get('/api/v1/components'); $this->get('/api/v1/components');
$this->seeJson(['id' => (string) $components[0]->id]); $this->seeJson(['id' => $components[0]->id]);
$this->seeJson(['id' => (string) $components[1]->id]); $this->seeJson(['id' => $components[1]->id]);
$this->seeJson(['id' => (string) $components[2]->id]); $this->seeJson(['id' => $components[2]->id]);
$this->assertResponseOk(); $this->assertResponseOk();
} }
+3 -3
View File
@@ -23,9 +23,9 @@ class IncidentTest extends AbstractTestCase
$incidents = factory('CachetHQ\Cachet\Models\Incident', 3)->create(); $incidents = factory('CachetHQ\Cachet\Models\Incident', 3)->create();
$this->get('/api/v1/incidents'); $this->get('/api/v1/incidents');
$this->seeJson(['id' => (string) $incidents[0]->id]); $this->seeJson(['id' => $incidents[0]->id]);
$this->seeJson(['id' => (string) $incidents[1]->id]); $this->seeJson(['id' => $incidents[1]->id]);
$this->seeJson(['id' => (string) $incidents[2]->id]); $this->seeJson(['id' => $incidents[2]->id]);
$this->assertResponseOk(); $this->assertResponseOk();
} }
+6 -6
View File
@@ -26,9 +26,9 @@ class MetricPointTest extends AbstractTestCase
]); ]);
$this->get("/api/v1/metrics/{$metric->id}/points"); $this->get("/api/v1/metrics/{$metric->id}/points");
$this->seeJson(['id' => (string) $metricPoint[0]->id]); $this->seeJson(['id' => $metricPoint[0]->id]);
$this->seeJson(['id' => (string) $metricPoint[1]->id]); $this->seeJson(['id' => $metricPoint[1]->id]);
$this->seeJson(['id' => (string) $metricPoint[2]->id]); $this->seeJson(['id' => $metricPoint[2]->id]);
} }
public function testPostMetricPointUnauthorized() public function testPostMetricPointUnauthorized()
@@ -52,7 +52,7 @@ class MetricPointTest extends AbstractTestCase
]); ]);
$this->post("/api/v1/metrics/{$metric->id}/points", $metricPoint->toArray()); $this->post("/api/v1/metrics/{$metric->id}/points", $metricPoint->toArray());
$this->seeJson(['value' => (string) $metricPoint->value]); $this->seeJson(['value' => $metricPoint->value]);
} }
public function testPostMetricPointTimestamp() public function testPostMetricPointTimestamp()
@@ -69,7 +69,7 @@ class MetricPointTest extends AbstractTestCase
$postData['timestamp'] = $timestamp; $postData['timestamp'] = $timestamp;
$this->post("/api/v1/metrics/{$metric->id}/points", $postData); $this->post("/api/v1/metrics/{$metric->id}/points", $postData);
$this->seeJson(['value' => (string) $metricPoint->value, 'created_at' => $datetime]); $this->seeJson(['value' => $metricPoint->value, 'created_at' => $datetime]);
} }
public function testPutMetricPoint() public function testPutMetricPoint()
@@ -82,7 +82,7 @@ class MetricPointTest extends AbstractTestCase
$this->put("/api/v1/metrics/{$metric->id}/points/{$metricPoint->id}", [ $this->put("/api/v1/metrics/{$metric->id}/points/{$metricPoint->id}", [
'value' => 999, 'value' => 999,
]); ]);
$this->seeJson(['value' => '999']); $this->seeJson(['value' => 999]);
} }
public function testDeleteMetricPoint() public function testDeleteMetricPoint()
+3 -3
View File
@@ -23,9 +23,9 @@ class MetricTest extends AbstractTestCase
$metrics = factory('CachetHQ\Cachet\Models\Metric', 3)->create(); $metrics = factory('CachetHQ\Cachet\Models\Metric', 3)->create();
$this->get('/api/v1/metrics'); $this->get('/api/v1/metrics');
$this->seeJson(['id' => (string) $metrics[0]->id]); $this->seeJson(['id' => $metrics[0]->id]);
$this->seeJson(['id' => (string) $metrics[1]->id]); $this->seeJson(['id' => $metrics[1]->id]);
$this->seeJson(['id' => (string) $metrics[2]->id]); $this->seeJson(['id' => $metrics[2]->id]);
$this->assertResponseOk(); $this->assertResponseOk();
} }