From 04937e98bc2fbc658e69a6a9723135e98bd1e1af Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 19 Dec 2019 13:24:30 -0600 Subject: [PATCH] Fix multiple SoftDeletes trait --- src/Generators/ModelGenerator.php | 2 +- .../Feature/Generator/ModelGeneratorTest.php | 5 ++-- tests/fixtures/definitions/soft-deletes.bp | 1 + tests/fixtures/migrations/soft-deletes.php | 1 + tests/fixtures/models/comment.php | 27 +++++++++++++++++++ tests/fixtures/models/soft-deletes.php | 11 +++++++- 6 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/models/comment.php diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 3b83e52d..fb2dd1be 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -192,7 +192,7 @@ private function addTraits(Model $model, $stub) } $stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Model;' . PHP_EOL . 'use Illuminate\\Database\\Eloquent\\SoftDeletes;', $stub); - $stub = str_replace('{', '{' . PHP_EOL . ' use SoftDeletes;' . PHP_EOL, $stub); + $stub = preg_replace('/^\\{$/m', '{' . PHP_EOL . ' use SoftDeletes;' . PHP_EOL, $stub); return $stub; } diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 3edec9c7..70fee269 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -53,6 +53,7 @@ public function output_writes_migration_for_model_tree($definition, $path, $mode ->with('stubs/model/class.stub') ->andReturn(file_get_contents('stubs/model/class.stub')); + // TODO: remove conditional expectations if ($iteration === 0) { $this->files->expects('get') ->with('stubs/model/fillable.stub') @@ -67,7 +68,7 @@ public function output_writes_migration_for_model_tree($definition, $path, $mode ->andReturn(file_get_contents('stubs/model/dates.stub')); } - if ($definition === 'definitions/relationships.bp') { + if ($definition === 'definitions/soft-deletes.bp') { $this->files->expects('get') ->with('stubs/model/method.stub') ->andReturn(file_get_contents('stubs/model/method.stub')); @@ -88,8 +89,8 @@ public function modelTreeDataProvider() { return [ ['definitions/readme-example.bp', 'app/Post.php', 'models/readme-example.php'], + ['definitions/with-timezones.bp', 'app/Comment.php', 'models/comment.php'], ['definitions/soft-deletes.bp', 'app/Comment.php', 'models/soft-deletes.php'], - ['definitions/with-timezones.bp', 'app/Comment.php', 'models/soft-deletes.php'], ['definitions/relationships.bp', 'app/Comment.php', 'models/relationships.php'], ]; } diff --git a/tests/fixtures/definitions/soft-deletes.bp b/tests/fixtures/definitions/soft-deletes.bp index 5017df6d..1354c1ff 100644 --- a/tests/fixtures/definitions/soft-deletes.bp +++ b/tests/fixtures/definitions/soft-deletes.bp @@ -1,3 +1,4 @@ models: Comment: + post_id: id softdeletes \ No newline at end of file diff --git a/tests/fixtures/migrations/soft-deletes.php b/tests/fixtures/migrations/soft-deletes.php index 236e761b..f43408d4 100644 --- a/tests/fixtures/migrations/soft-deletes.php +++ b/tests/fixtures/migrations/soft-deletes.php @@ -15,6 +15,7 @@ public function up() { Schema::create('comments', function (Blueprint $table) { $table->increments('id'); + $table->unsignedBigInteger('post_id'); $table->softDeletes(); $table->timestamps(); }); diff --git a/tests/fixtures/models/comment.php b/tests/fixtures/models/comment.php new file mode 100644 index 00000000..87fe82f4 --- /dev/null +++ b/tests/fixtures/models/comment.php @@ -0,0 +1,27 @@ + 'integer', + ]; +} diff --git a/tests/fixtures/models/soft-deletes.php b/tests/fixtures/models/soft-deletes.php index 87fe82f4..1f3df1b9 100644 --- a/tests/fixtures/models/soft-deletes.php +++ b/tests/fixtures/models/soft-deletes.php @@ -14,7 +14,9 @@ class Comment extends Model * * @var array */ - protected $fillable = []; + protected $fillable = [ + 'post_id', + ]; /** * The attributes that should be cast to native types. @@ -23,5 +25,12 @@ class Comment extends Model */ protected $casts = [ 'id' => 'integer', + 'post_id' => 'integer', ]; + + + public function post() + { + return $this->belongsTo(\App\Post::class); + } }