Skip to content

Commit 978fe2b

Browse files
committed
fix test sync hybrid
1 parent 0c96f80 commit 978fe2b

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

tests/HybridRelationsTest.php

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class HybridRelationsTest extends TestCase
99
public function setUp(): void
1010
{
1111
parent::setUp();
12-
1312
MysqlUser::executeSchema();
1413
MysqlBook::executeSchema();
1514
MysqlRole::executeSchema();
@@ -194,50 +193,49 @@ public function testHybridWith()
194193
$this->assertEquals($user->id, $user->books->count());
195194
});
196195
}
197-
198196
public function testHybridSync()
199197
{
200198
$user = new MysqlUser;
199+
$otherUser = new MysqlUser;
201200
$this->assertInstanceOf(MysqlUser::class, $user);
202201
$this->assertInstanceOf(MySqlConnection::class, $user->getConnection());
202+
$this->assertInstanceOf(MysqlUser::class, $otherUser);
203+
$this->assertInstanceOf(MySqlConnection::class, $otherUser->getConnection());
203204

204-
// Mysql User
205-
$user->name = 'John Doe';
206-
$user->save();
207-
$this->assertIsInt($user->id);
208-
// SQL has many
209-
$book = new Book(['title' => 'Harry Potter']);
210-
$otherBook = new Book(['title' => 'Game of Thrones']);
211-
212-
$user->books()->sync([$book->id, $otherBook->id]);
213-
$user = MysqlUser::find($user->id); // refetch
214-
$this->assertCount(2, $user->books);
215-
216-
$user->books()->sync([$book->id]);
217-
$user = MysqlUser::find($user->id); // refetch
218-
$this->assertCount(1, $user->books);
219-
220-
$user->books()->sync([$book->id, $otherBook->id]);
221-
$user = MysqlUser::find($user->id); // refetch
222-
$this->assertCount(2, $user->books);
223-
// MongoDB User
224-
$user = new User;
205+
// Create Mysql Users
225206
$user->name = 'John Doe';
226207
$user->save();
227-
// MongoDB has many
228-
$book = new MysqlBook(['title' => 'Harry Potter']);
229-
$otherBook = new MysqlBook(['title' => 'Game of Thrones']);
230-
231-
$user->mysqlBooks()->sync([$book->id, $otherBook->id]);
232-
$user = User::find($user->_id);
233-
$this->assertCount(2, $user->mysqlBooks);
234-
235-
$user->mysqlBooks()->sync([$book->id]);
236-
$user = User::find($user->_id);
237-
$this->assertCount(1, $user->mysqlBooks);
238-
239-
$user->mysqlBooks()->sync([$book->id, $otherBook->id]);
240-
$user = User::find($user->_id);
241-
$this->assertCount(2, $user->mysqlBooks);
208+
$user = MysqlUser::find($user->id);
209+
$otherUser->name = 'Maria Doe';
210+
$otherUser->save();
211+
// Create Mongodb Clients
212+
$client = Client::create(['name' => 'Pork Pies Ltd.']);
213+
$otherClient = Client::create(['name' => 'Pork Pies Ltd.']);
214+
215+
// sync 2 users
216+
$client->usersMysql()->sync([$user->id, $otherUser->id]);
217+
$client = Client::find($client->_id);
218+
$this->assertEquals(2, $client->usersMysql->count());
219+
// sync 1 user
220+
$client->usersMysql()->sync([$user->id]);
221+
$client = Client::find($client->_id);
222+
$this->assertEquals(1, $client->usersMysql->count());
223+
// sync 2 users again
224+
$client->usersMysql()->sync([$user->id, $otherUser->id]);
225+
$client = Client::find($client->_id);
226+
$this->assertEquals(2, $client->usersMysql->count());
227+
228+
// sync 2 Clients
229+
$user->clients()->sync([$client->_id,$otherClient->_id ]);
230+
$user = MysqlUser::find($user->id);
231+
$this->assertEquals(2, $user->clients->count());
232+
// Sync 1 Client
233+
$user->clients()->sync([$client->_id]);
234+
$user = MysqlUser::find($user->id);
235+
$this->assertEquals(1, $user->clients->count());
236+
// Sync 2 Clients again
237+
$user->clients()->sync([$client->_id,$otherClient->_id ]);
238+
$user = MysqlUser::find($user->id);
239+
$this->assertEquals(2, $user->clients->count());
242240
}
243241
}

tests/models/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public function addresses(): HasMany
2727
{
2828
return $this->hasMany('Address', 'data.client_id', 'data.client_id');
2929
}
30+
31+
public function usersMysql(): BelongsToMany
32+
{
33+
return $this->belongsToMany('MysqlUser');
34+
}
3035
}

tests/models/MysqlUser.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public function mysqlBooks(): HasMany
3131
return $this->hasMany(MysqlBook::class);
3232
}
3333

34+
public function clients()
35+
{
36+
return $this->belongsToMany('Client',null,'mysql_users_id','clients');
37+
}
38+
3439
/**
3540
* Check if we need to run the schema.
3641
*/
@@ -46,5 +51,12 @@ public static function executeSchema(): void
4651
$table->timestamps();
4752
});
4853
}
54+
if (! $schema->hasTable('client_mysql_user')) {
55+
Schema::connection('mysql')->create('client_mysql_user', function (Blueprint $table) {
56+
$table->integer('mysql_user_id')->unsigned();
57+
$table->string('client_id');
58+
$table->primary(['mysql_user_id','client_id']);
59+
});
60+
}
4961
}
5062
}

0 commit comments

Comments
 (0)