Skip to content

Commit 4d930ba

Browse files
authored
Add a false timestamp property to the model if timestamps are disabled (#189)
1 parent 79644cb commit 4d930ba

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

src/Generators/ModelGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ private function buildProperties(Model $model)
9292
{
9393
$properties = '';
9494

95+
if (! $model->usesTimestamps()) {
96+
$properties .= $this->files->stub('model/timestamps.stub');
97+
}
98+
9599
if (config('blueprint.use_guarded')) {
96100
$properties .= $this->files->stub('model/guarded.stub');
97101
} else {

stubs/model/timestamps.stub

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Indicates if the model should be timestamped.
3+
*
4+
* @var bool
5+
*/
6+
public $timestamps = false;

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function output_generates_models($definition, $path, $model)
7979
->with('model/method-comment.stub')
8080
->andReturn(file_get_contents('stubs/model/method-comment.stub'));
8181

82+
8283
$this->files->expects('exists')
8384
->with(dirname($path))
8485
->andReturnTrue();
@@ -163,6 +164,39 @@ public function output_generates_relationships()
163164
$this->assertEquals(['created' => ['app/Subscription.php']], $this->subject->output($tree));
164165
}
165166

167+
/**
168+
* @test
169+
*/
170+
public function output_generates_disabled_auto_columns()
171+
{
172+
$this->files->expects('stub')
173+
->with('model/class.stub')
174+
->andReturn(file_get_contents('stubs/model/class.stub'));
175+
$this->files->expects('stub')
176+
->with('model/timestamps.stub')
177+
->andReturn(file_get_contents('stubs/model/timestamps.stub'));
178+
$this->files->expects('stub')
179+
->with('model/fillable.stub')
180+
->andReturn(file_get_contents('stubs/model/fillable.stub'));
181+
$this->files->expects('stub')
182+
->with('model/casts.stub')
183+
->andReturn(file_get_contents('stubs/model/casts.stub'));
184+
$this->files->expects('stub')
185+
->with('model/method.stub')
186+
->andReturn(file_get_contents('stubs/model/method.stub'));
187+
188+
$this->files->expects('exists')
189+
->with('app')
190+
->andReturnTrue();
191+
$this->files->expects('put')
192+
->with('app/State.php', $this->fixture('models/disable-auto-columns.php'));
193+
194+
$tokens = $this->blueprint->parse($this->fixture('definitions/disable-auto-columns.bp'));
195+
$tree = $this->blueprint->analyze($tokens);
196+
197+
$this->assertEquals(['created' => ['app/State.php']], $this->subject->output($tree));
198+
}
199+
166200
/**
167201
* @test
168202
*/
@@ -214,6 +248,12 @@ public function output_generates_phpdoc_for_model($definition, $path, $model)
214248
->with('model/class.stub')
215249
->andReturn(file_get_contents('stubs/model/class.stub'));
216250

251+
if ($definition === 'definitions/disable-auto-columns.bp') {
252+
$this->files->expects('stub')
253+
->with('model/timestamps.stub')
254+
->andReturn(file_get_contents('stubs/model/timestamps.stub'));
255+
}
256+
217257
$this->files->expects('stub')
218258
->with('model/fillable.stub')
219259
->andReturn(file_get_contents('stubs/model/fillable.stub'));

tests/fixtures/models/disable-auto-columns-phpdoc.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
*/
1212
class State extends Model
1313
{
14+
/**
15+
* Indicates if the model should be timestamped.
16+
*
17+
* @var bool
18+
*/
19+
public $timestamps = false;
20+
1421
/**
1522
* The attributes that are mass assignable.
1623
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class State extends Model
8+
{
9+
/**
10+
* Indicates if the model should be timestamped.
11+
*
12+
* @var bool
13+
*/
14+
public $timestamps = false;
15+
16+
/**
17+
* The attributes that are mass assignable.
18+
*
19+
* @var array
20+
*/
21+
protected $fillable = [
22+
'name',
23+
'code',
24+
'country_id',
25+
];
26+
27+
/**
28+
* The attributes that should be cast to native types.
29+
*
30+
* @var array
31+
*/
32+
protected $casts = [
33+
'country_id' => 'integer',
34+
];
35+
}

0 commit comments

Comments
 (0)