Skip to content

Commit

Permalink
Merge pull request #9 from 514sid/improve-migration
Browse files Browse the repository at this point in the history
Add nullOnDelete() for the User-related foreignId column
  • Loading branch information
cjmellor authored Aug 24, 2023
2 parents 38035c7 + d621c69 commit a446e4e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion database/migrations/create_ratings_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function up()
Schema::create('ratings', function (Blueprint $table) {
$table->id();
$table->morphs('rateable');
$table->foreignId((string) config(key: 'rating.users.primary_key', default: 'user_id'))->nullable()->constrained();
$table->foreignId((string) config(key: 'rating.users.primary_key', default: 'user_id'))->nullable()->constrained()->nullOnDelete();
$table->integer('rating');
$table->timestamps();
});
Expand Down
23 changes: 23 additions & 0 deletions tests/Concerns/CanBeRatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Cjmellor\Rating\Exceptions\MaxRatingException;
use Cjmellor\Rating\Models\Rating;
use Cjmellor\Rating\Tests\Models\FakeUser;
use Illuminate\Support\Facades\DB;

test(description: 'a Model can be rated', closure: function () {
// Create a Rating and attach to a fake User
Expand Down Expand Up @@ -144,3 +145,25 @@
'rating' => 5,
]);
});

test(description: 'user_id in rating table is set to null when the related user is deleted', closure: function () {
$this->actingAs($this->user)->assertAuthenticated();

$this->secondUser->rate(score: 4);

$this->assertDatabaseHas(table: Rating::class, data: [
'rateable_type' => 'Cjmellor\Rating\Tests\Models\FakeUser',
'rateable_id' => $this->secondUser->id,
'user_id' => $this->user->id,
'rating' => 4,
]);

DB::table('users')->where('id', $this->user->id)->delete();

$this->assertDatabaseHas(table: Rating::class, data: [
'rateable_type' => 'Cjmellor\Rating\Tests\Models\FakeUser',
'rateable_id' => $this->secondUser->id,
'user_id' => null,
'rating' => 4,
]);
});
2 changes: 2 additions & 0 deletions tests/Models/FakeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ class FakeUser extends User
* @var bool
*/
public $timestamps = false;

protected $table = 'users';
}
12 changes: 7 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ public function getEnvironmentSetUp($app): void
{
config()->set('database.default', 'testing');

$migration = include __DIR__.'/../database/migrations/create_ratings_table.php';

$migration->up();

Schema::create(table: 'fake_users', callback: function (Blueprint $table) {
Schema::create(table: 'users', callback: function (Blueprint $table) {
$table->id();
$table->string(column: 'username');
$table->string(column: 'password');
});

Schema::enableForeignKeyConstraints();

$migration = include __DIR__.'/../database/migrations/create_ratings_table.php';

$migration->up();
}

protected function setUp(): void
Expand Down

0 comments on commit a446e4e

Please sign in to comment.