diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 419ea175..ffc51cdf 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -254,7 +254,8 @@ function (Column $column) { return $column->name(); }, array_filter($columns, function (Column $column) { - return stripos($column->dataType(), 'datetime') !== false + return $column->dataType() === 'date' + || stripos($column->dataType(), 'datetime') !== false || stripos($column->dataType(), 'timestamp') !== false; }) ); diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 17309b2c..415cfcc5 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -56,7 +56,7 @@ public function output_generates_models($definition, $path, $model) ->with('model.fillable.stub') ->andReturn(file_get_contents('stubs/model.fillable.stub')); - if (in_array($definition, ['drafts/nested-components.yaml','drafts/resource-statements.yaml'])) { + if (in_array($definition, ['drafts/nested-components.yaml', 'drafts/resource-statements.yaml'])) { $this->files->expects('stub') ->with('model.hidden.stub') ->andReturn(file_get_contents('stubs/model.hidden.stub')); @@ -66,7 +66,7 @@ public function output_generates_models($definition, $path, $model) ->with('model.casts.stub') ->andReturn(file_get_contents('stubs/model.casts.stub')); - if ($definition === 'drafts/readme-example.yaml') { + if (in_array($definition, ['drafts/readme-example.yaml', 'drafts/all-column-types.yaml'])) { $this->files->expects('stub') ->with('model.dates.stub') ->andReturn(file_get_contents('stubs/model.dates.stub')); @@ -109,6 +109,9 @@ public function output_works_for_pascal_case_definition() ->with('model.casts.stub') ->andReturn(file_get_contents('stubs/model.casts.stub')) ->twice(); + $this->files->expects('stub') + ->with('model.dates.stub') + ->andReturn(file_get_contents('stubs/model.dates.stub')); $this->files->expects('stub') ->with('model.method.stub') ->andReturn(file_get_contents('stubs/model.method.stub')) @@ -255,15 +258,12 @@ public function output_respects_configuration() $this->files->expects('stub') ->with('model.class.stub') ->andReturn(file_get_contents('stubs/model.class.stub')); - $this->files->expects('stub') ->with('model.fillable.stub') ->andReturn(file_get_contents('stubs/model.fillable.stub')); - $this->files->expects('stub') ->with('model.casts.stub') ->andReturn(file_get_contents('stubs/model.casts.stub')); - $this->files->expects('stub') ->with('model.method.stub') ->andReturn(file_get_contents('stubs/model.method.stub')); @@ -450,6 +450,7 @@ public function modelTreeDataProvider() ['drafts/unconventional.yaml', 'app/Team.php', 'models/unconventional.php'], ['drafts/nested-components.yaml', 'app/Admin/User.php', 'models/nested-components.php'], ['drafts/resource-statements.yaml', 'app/User.php', 'models/resource-statements.php'], + ['drafts/all-column-types.yaml', 'app/AllType.php', 'models/all-column-types.php'], ]; } diff --git a/tests/fixtures/models/all-column-types.php b/tests/fixtures/models/all-column-types.php new file mode 100644 index 00000000..bcefe7c7 --- /dev/null +++ b/tests/fixtures/models/all-column-types.php @@ -0,0 +1,109 @@ + 'integer', + 'boolean' => 'boolean', + 'decimal' => 'decimal', + 'double' => 'double', + 'float' => 'float', + 'json' => 'array', + 'mediumInteger' => 'integer', + 'smallInteger' => 'integer', + 'tinyInteger' => 'integer', + 'unsignedBigInteger' => 'integer', + 'unsignedDecimal' => 'decimal', + 'unsignedInteger' => 'integer', + 'unsignedMediumInteger' => 'integer', + 'unsignedSmallInteger' => 'integer', + 'unsignedTinyInteger' => 'integer', + ]; + + /** + * The attributes that should be mutated to dates. + * + * @var array + */ + protected $dates = [ + 'date', + 'dateTime', + 'dateTimeTz', + 'nullableTimestamps', + 'timestamp', + 'timestampTz', + ]; + + + public function uuid() + { + return $this->belongsTo(\App\Uuid::class); + } +} diff --git a/tests/fixtures/models/certificate-pascal-case-example.php b/tests/fixtures/models/certificate-pascal-case-example.php index 3da041cd..2807be48 100644 --- a/tests/fixtures/models/certificate-pascal-case-example.php +++ b/tests/fixtures/models/certificate-pascal-case-example.php @@ -30,6 +30,15 @@ class Certificate extends Model 'certificate_type_id' => 'integer', ]; + /** + * The attributes that should be mutated to dates. + * + * @var array + */ + protected $dates = [ + 'expiry_date', + ]; + public function certificateType() {