From a77efc12e093a35a85e693eeede2ae028c05bf79 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sat, 22 Aug 2015 15:57:53 +0100 Subject: [PATCH] Added casts properties to all models, re-ordered model properties. Fixes #916 --- app/Models/Component.php | 51 ++++++++++++++++++++------------ app/Models/ComponentGroup.php | 3 +- app/Models/Incident.php | 49 +++++++++++++++--------------- app/Models/IncidentTemplate.php | 19 +++++++++--- app/Models/Metric.php | 28 +++++++++++++----- app/Models/MetricPoint.php | 11 +++++++ app/Models/Setting.php | 11 +++++++ app/Models/Subscriber.php | 29 +++++++++--------- app/Models/Tag.php | 10 +++++++ app/Models/User.php | 29 +++++++++++++----- tests/Api/ComponentGroupTest.php | 6 ++-- tests/Api/ComponentTest.php | 6 ++-- tests/Api/IncidentTest.php | 6 ++-- tests/Api/MetricPointTest.php | 12 ++++---- tests/Api/MetricTest.php | 6 ++-- 15 files changed, 182 insertions(+), 94 deletions(-) diff --git a/app/Models/Component.php b/app/Models/Component.php index 77e2d5f8..50fe616d 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -23,16 +23,37 @@ class Component extends Model implements HasPresenter 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[] */ - public $rules = [ - 'name' => 'required|string', - 'status' => 'integer|required', - 'link' => 'url', + protected $casts = [ + 'id' => 'int', + 'order' => 'int', + 'group_id' => 'int', + 'description' => 'string', + 'link' => 'string', ]; + /** + * The attributes that should be mutated to dates. + * + * @var array + */ + protected $dates = ['deleted_at']; + /** * 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 = [ - 'order' => 0, - 'group_id' => 0, - 'description' => '', - 'link' => '', + public $rules = [ + 'name' => 'required|string', + 'status' => 'integer|required', + 'link' => 'url', ]; - /** - * The attributes that should be mutated to dates. - * - * @var array - */ - protected $dates = ['deleted_at']; - /** * Components can belong to a group. * diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 010750e5..4f1cdf5d 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -24,8 +24,9 @@ class ComponentGroup extends Model * @var string[] */ protected $casts = [ + 'id' => 'int', 'name' => 'string', - 'order' => 'integer', + 'order' => 'int', ]; /** diff --git a/app/Models/Incident.php b/app/Models/Incident.php index 2fc11769..b8c0e042 100644 --- a/app/Models/Incident.php +++ b/app/Models/Incident.php @@ -23,18 +23,29 @@ class Incident extends Model implements HasPresenter use SoftDeletes, ValidatingTrait; /** - * The validation rules. + * The accessors to append to the model's serialized form. * * @var string[] */ - public $rules = [ - 'component_id' => 'integer', - 'name' => 'required', - 'status' => 'required|integer', - 'visible' => 'required|boolean', - 'message' => 'required', + protected $appends = ['human_status']; + + /** + * The attributes that should be casted to native types. + * + * @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. * @@ -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[] */ - protected $appends = ['human_status']; - - /** - * The attributes that should be mutated to dates. - * - * @var string[] - */ - protected $dates = ['scheduled_at', 'deleted_at']; - - /** - * The attributes that should be casted to native types. - * - * @var string[] - */ - protected $casts = [ - 'visible' => 'integer', + public $rules = [ + 'component_id' => 'integer', + 'name' => 'required', + 'status' => 'required|integer', + 'visible' => 'required|boolean', + 'message' => 'required', ]; /** diff --git a/app/Models/IncidentTemplate.php b/app/Models/IncidentTemplate.php index 673054ae..ecb40abf 100644 --- a/app/Models/IncidentTemplate.php +++ b/app/Models/IncidentTemplate.php @@ -20,13 +20,14 @@ class IncidentTemplate extends Model use ValidatingTrait; /** - * The validation rules. + * The attributes that should be casted to native types. * * @var string[] */ - public $rules = [ - 'name' => 'required', - 'template' => 'required', + protected $casts = [ + 'id' => 'int', + 'name' => 'string', + 'template' => 'string', ]; /** @@ -36,6 +37,16 @@ class IncidentTemplate extends Model */ protected $fillable = ['name', 'template']; + /** + * The validation rules. + * + * @var string[] + */ + public $rules = [ + 'name' => 'required', + 'template' => 'required', + ]; + /** * Overrides the models boot method. */ diff --git a/app/Models/Metric.php b/app/Models/Metric.php index 995fffac..8949d28c 100644 --- a/app/Models/Metric.php +++ b/app/Models/Metric.php @@ -37,16 +37,17 @@ class Metric extends Model implements HasPresenter ]; /** - * The validation rules. + * The attributes that should be casted to native types. * * @var string[] */ - public $rules = [ - 'name' => 'required', - 'suffix' => 'required', - 'display_chart' => 'boolean', - 'default_value' => 'numeric', - 'places' => 'numeric|min:0|max:4', + protected $casts = [ + 'id' => 'int', + 'name' => 'string', + 'display_chart' => 'bool', + 'default_value' => 'int', + 'calc_type' => 'int', + 'places' => 'int', ]; /** @@ -64,6 +65,19 @@ class Metric extends Model implements HasPresenter '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. * diff --git a/app/Models/MetricPoint.php b/app/Models/MetricPoint.php index ebba7d9f..0d08bf91 100644 --- a/app/Models/MetricPoint.php +++ b/app/Models/MetricPoint.php @@ -20,6 +20,17 @@ class MetricPoint extends Model implements HasPresenter { 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. * diff --git a/app/Models/Setting.php b/app/Models/Setting.php index f378684c..37758ca8 100644 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -15,6 +15,17 @@ use Illuminate\Database\Eloquent\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. * diff --git a/app/Models/Subscriber.php b/app/Models/Subscriber.php index 3b96b969..8f5651de 100644 --- a/app/Models/Subscriber.php +++ b/app/Models/Subscriber.php @@ -21,21 +21,16 @@ class Subscriber extends Model implements HasPresenter use ValidatingTrait; /** - * The validation rules. + * The attributes that should be casted to native types. * * @var string[] */ - public $rules = [ - 'email' => 'required|email', + protected $casts = [ + 'id' => 'int', + 'email' => 'string', + 'verify_code' => 'string', ]; - /** - * The fillable properties. - * - * @var string[] - */ - protected $fillable = ['email']; - /** * The attributes that should be mutated to dates. * @@ -44,13 +39,19 @@ class Subscriber extends Model implements HasPresenter protected $dates = ['verified_at']; /** - * The attributes that should be casted to native types. + * The fillable properties. * * @var string[] */ - protected $casts = [ - 'email' => 'string', - 'verify_code' => 'string', + protected $fillable = ['email']; + + /** + * The validation rules. + * + * @var string[] + */ + public $rules = [ + 'email' => 'required|email', ]; /** diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 842a262b..dccce454 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -16,6 +16,16 @@ use Illuminate\Support\Str; class Tag extends Model { + /** + * The attributes that should be casted to native types. + * + * @var string[] + */ + protected $casts = [ + 'id' => 'int', + 'name' => 'string', + ]; + /** * The fillable properties. * diff --git a/app/Models/User.php b/app/Models/User.php index a7efda34..1af02ad7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -25,16 +25,27 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon use Authenticatable, CanResetPassword, ValidatingTrait; /** - * The validation rules. + * The attributes that should be casted to native types. * * @var string[] */ - public $rules = [ - 'username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/'], - 'email' => 'required|email', - 'password' => 'required', + protected $casts = [ + 'id' => 'int', + 'username' => 'string', + '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. * @@ -45,11 +56,15 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon protected $hidden = ['password', 'remember_token', 'google_2fa_secret']; /** - * The properties that cannot be mass assigned. + * The validation rules. * * @var string[] */ - protected $guarded = []; + public $rules = [ + 'username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/'], + 'email' => 'required|email', + 'password' => 'required', + ]; /** * Overrides the models boot method. diff --git a/tests/Api/ComponentGroupTest.php b/tests/Api/ComponentGroupTest.php index 6dafb852..bf981197 100644 --- a/tests/Api/ComponentGroupTest.php +++ b/tests/Api/ComponentGroupTest.php @@ -23,9 +23,9 @@ class ComponentGroupTest extends AbstractTestCase $groups = factory('CachetHQ\Cachet\Models\ComponentGroup', 3)->create(); $this->get('/api/v1/components/groups'); - $this->seeJson(['id' => (string) $groups[0]->id]); - $this->seeJson(['id' => (string) $groups[1]->id]); - $this->seeJson(['id' => (string) $groups[2]->id]); + $this->seeJson(['id' => $groups[0]->id]); + $this->seeJson(['id' => $groups[1]->id]); + $this->seeJson(['id' => $groups[2]->id]); $this->assertResponseOk(); } diff --git a/tests/Api/ComponentTest.php b/tests/Api/ComponentTest.php index 31dbab56..b13f7e96 100644 --- a/tests/Api/ComponentTest.php +++ b/tests/Api/ComponentTest.php @@ -23,9 +23,9 @@ class ComponentTest extends AbstractTestCase $components = factory('CachetHQ\Cachet\Models\Component', 3)->create(); $this->get('/api/v1/components'); - $this->seeJson(['id' => (string) $components[0]->id]); - $this->seeJson(['id' => (string) $components[1]->id]); - $this->seeJson(['id' => (string) $components[2]->id]); + $this->seeJson(['id' => $components[0]->id]); + $this->seeJson(['id' => $components[1]->id]); + $this->seeJson(['id' => $components[2]->id]); $this->assertResponseOk(); } diff --git a/tests/Api/IncidentTest.php b/tests/Api/IncidentTest.php index 9124ce9f..14887912 100644 --- a/tests/Api/IncidentTest.php +++ b/tests/Api/IncidentTest.php @@ -23,9 +23,9 @@ class IncidentTest extends AbstractTestCase $incidents = factory('CachetHQ\Cachet\Models\Incident', 3)->create(); $this->get('/api/v1/incidents'); - $this->seeJson(['id' => (string) $incidents[0]->id]); - $this->seeJson(['id' => (string) $incidents[1]->id]); - $this->seeJson(['id' => (string) $incidents[2]->id]); + $this->seeJson(['id' => $incidents[0]->id]); + $this->seeJson(['id' => $incidents[1]->id]); + $this->seeJson(['id' => $incidents[2]->id]); $this->assertResponseOk(); } diff --git a/tests/Api/MetricPointTest.php b/tests/Api/MetricPointTest.php index 2fac2b06..2c7bb81a 100644 --- a/tests/Api/MetricPointTest.php +++ b/tests/Api/MetricPointTest.php @@ -26,9 +26,9 @@ class MetricPointTest extends AbstractTestCase ]); $this->get("/api/v1/metrics/{$metric->id}/points"); - $this->seeJson(['id' => (string) $metricPoint[0]->id]); - $this->seeJson(['id' => (string) $metricPoint[1]->id]); - $this->seeJson(['id' => (string) $metricPoint[2]->id]); + $this->seeJson(['id' => $metricPoint[0]->id]); + $this->seeJson(['id' => $metricPoint[1]->id]); + $this->seeJson(['id' => $metricPoint[2]->id]); } public function testPostMetricPointUnauthorized() @@ -52,7 +52,7 @@ class MetricPointTest extends AbstractTestCase ]); $this->post("/api/v1/metrics/{$metric->id}/points", $metricPoint->toArray()); - $this->seeJson(['value' => (string) $metricPoint->value]); + $this->seeJson(['value' => $metricPoint->value]); } public function testPostMetricPointTimestamp() @@ -69,7 +69,7 @@ class MetricPointTest extends AbstractTestCase $postData['timestamp'] = $timestamp; $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() @@ -82,7 +82,7 @@ class MetricPointTest extends AbstractTestCase $this->put("/api/v1/metrics/{$metric->id}/points/{$metricPoint->id}", [ 'value' => 999, ]); - $this->seeJson(['value' => '999']); + $this->seeJson(['value' => 999]); } public function testDeleteMetricPoint() diff --git a/tests/Api/MetricTest.php b/tests/Api/MetricTest.php index 613b3e6a..bf6960ae 100644 --- a/tests/Api/MetricTest.php +++ b/tests/Api/MetricTest.php @@ -23,9 +23,9 @@ class MetricTest extends AbstractTestCase $metrics = factory('CachetHQ\Cachet\Models\Metric', 3)->create(); $this->get('/api/v1/metrics'); - $this->seeJson(['id' => (string) $metrics[0]->id]); - $this->seeJson(['id' => (string) $metrics[1]->id]); - $this->seeJson(['id' => (string) $metrics[2]->id]); + $this->seeJson(['id' => $metrics[0]->id]); + $this->seeJson(['id' => $metrics[1]->id]); + $this->seeJson(['id' => $metrics[2]->id]); $this->assertResponseOk(); }