123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace Illuminate\Tests\Integration\Database;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\Pivot;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\Schema;
- class EloquentPivotEventsTest extends DatabaseTestCase
- {
- protected function setUp(): void
- {
- parent::setUp();
- // clear event log between requests
- PivotEventsTestCollaborator::$eventsCalled = [];
- }
- protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
- {
- Schema::create('users', function (Blueprint $table) {
- $table->increments('id');
- $table->string('email');
- $table->timestamps();
- });
- Schema::create('projects', function (Blueprint $table) {
- $table->increments('id');
- $table->string('name');
- $table->timestamps();
- });
- Schema::create('project_users', function (Blueprint $table) {
- $table->integer('user_id');
- $table->integer('project_id');
- $table->text('permissions')->nullable();
- $table->string('role')->nullable();
- });
- }
- public function testPivotWillTriggerEventsToBeFired()
- {
- $user = PivotEventsTestUser::forceCreate(['email' => 'taylor@laravel.com']);
- $user2 = PivotEventsTestUser::forceCreate(['email' => 'ralph@ralphschindler.com']);
- $project = PivotEventsTestProject::forceCreate(['name' => 'Test Project']);
- $project->collaborators()->attach($user);
- $this->assertEquals(['saving', 'creating', 'created', 'saved'], PivotEventsTestCollaborator::$eventsCalled);
- PivotEventsTestCollaborator::$eventsCalled = [];
- $project->collaborators()->sync([$user2->id]);
- $this->assertEquals(['deleting', 'deleted', 'saving', 'creating', 'created', 'saved'], PivotEventsTestCollaborator::$eventsCalled);
- PivotEventsTestCollaborator::$eventsCalled = [];
- $project->collaborators()->sync([$user->id => ['role' => 'owner'], $user2->id => ['role' => 'contributor']]);
- $this->assertEquals(['saving', 'creating', 'created', 'saved', 'saving', 'updating', 'updated', 'saved'], PivotEventsTestCollaborator::$eventsCalled);
- PivotEventsTestCollaborator::$eventsCalled = [];
- $project->collaborators()->detach($user);
- $this->assertEquals(['deleting', 'deleted'], PivotEventsTestCollaborator::$eventsCalled);
- }
- public function testPivotWithPivotCriteriaTriggerEventsToBeFiredOnCreateUpdateNoneOnDetach()
- {
- $user = PivotEventsTestUser::forceCreate(['email' => 'taylor@laravel.com']);
- $user2 = PivotEventsTestUser::forceCreate(['email' => 'ralph@ralphschindler.com']);
- $project = PivotEventsTestProject::forceCreate(['name' => 'Test Project']);
- $project->contributors()->sync([$user->id, $user2->id]);
- $this->assertEquals(['saving', 'creating', 'created', 'saved', 'saving', 'creating', 'created', 'saved'], PivotEventsTestCollaborator::$eventsCalled);
- PivotEventsTestCollaborator::$eventsCalled = [];
- $project->contributors()->detach($user->id);
- $this->assertEquals([], PivotEventsTestCollaborator::$eventsCalled);
- }
- public function testCustomPivotUpdateEventHasExistingAttributes()
- {
- $_SERVER['pivot_attributes'] = false;
- $user = PivotEventsTestUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- ]);
- $project = PivotEventsTestProject::forceCreate([
- 'name' => 'Test Project',
- ]);
- $project->collaborators()->attach($user, ['permissions' => ['foo', 'bar']]);
- $project->collaborators()->updateExistingPivot($user->id, ['role' => 'Lead Developer']);
- $this->assertEquals(
- [
- 'user_id' => '1',
- 'project_id' => '1',
- 'permissions' => '["foo","bar"]',
- 'role' => 'Lead Developer',
- ],
- $_SERVER['pivot_attributes']
- );
- }
- public function testCustomPivotUpdateEventHasDirtyCorrect()
- {
- $_SERVER['pivot_dirty_attributes'] = false;
- $user = PivotEventsTestUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- ]);
- $project = PivotEventsTestProject::forceCreate([
- 'name' => 'Test Project',
- ]);
- $project->collaborators()->attach($user, ['permissions' => ['foo', 'bar'], 'role' => 'Developer']);
- $project->collaborators()->updateExistingPivot($user->id, ['role' => 'Lead Developer']);
- $this->assertSame(['role' => 'Lead Developer'], $_SERVER['pivot_dirty_attributes']);
- }
- }
- class PivotEventsTestUser extends Model
- {
- public $table = 'users';
- }
- class PivotEventsTestProject extends Model
- {
- public $table = 'projects';
- public function collaborators()
- {
- return $this->belongsToMany(
- PivotEventsTestUser::class, 'project_users', 'project_id', 'user_id'
- )->using(PivotEventsTestCollaborator::class);
- }
- public function contributors()
- {
- return $this->belongsToMany(PivotEventsTestUser::class, 'project_users', 'project_id', 'user_id')
- ->using(PivotEventsTestCollaborator::class)
- ->wherePivot('role', 'contributor');
- }
- }
- class PivotEventsTestCollaborator extends Pivot
- {
- public $table = 'project_users';
- protected $casts = [
- 'permissions' => 'json',
- ];
- public static $eventsCalled = [];
- public static function boot()
- {
- parent::boot();
- static::creating(function ($model) {
- static::$eventsCalled[] = 'creating';
- });
- static::created(function ($model) {
- static::$eventsCalled[] = 'created';
- });
- static::updating(function ($model) {
- static::$eventsCalled[] = 'updating';
- });
- static::updated(function ($model) {
- $_SERVER['pivot_attributes'] = $model->getAttributes();
- $_SERVER['pivot_dirty_attributes'] = $model->getDirty();
- static::$eventsCalled[] = 'updated';
- });
- static::saving(function ($model) {
- static::$eventsCalled[] = 'saving';
- });
- static::saved(function ($model) {
- static::$eventsCalled[] = 'saved';
- });
- static::deleting(function ($model) {
- static::$eventsCalled[] = 'deleting';
- });
- static::deleted(function ($model) {
- static::$eventsCalled[] = 'deleted';
- });
- }
- }
|