Skip to content

add validate instaceof Model and fix current in sync method for Hybrid relation #2276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: 5.x
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test sync hybrid
  • Loading branch information
juniohyago committed Jul 2, 2021
commit a6ae7acae8b31ee0615558d3f3c1dc14f4f871f6
45 changes: 45 additions & 0 deletions tests/HybridRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,49 @@ public function testHybridWith()
$this->assertEquals($user->id, $user->books->count());
});
}
public function testHybridSync()
{
$user = new MysqlUser;
$this->assertInstanceOf(MysqlUser::class, $user);
$this->assertInstanceOf(MySqlConnection::class, $user->getConnection());

// Mysql User
$user->name = 'John Doe';
$user->save();
$this->assertIsInt($user->id);
// SQL has many
$book = new Book(['title' => 'Harry Potter']);
$otherBook = new Book(['title' => 'Game of Thrones']);

$user->books()->sync([$book->id,$otherBook->id]);
$user = MysqlUser::find($user->id); // refetch
$this->assertCount(2, $user->books);

$user->books()->sync([$book->id]);
$user = MysqlUser::find($user->id); // refetch
$this->assertCount(1, $user->books);

$user->books()->sync([$book->id,$otherBook->id]);
$user = MysqlUser::find($user->id); // refetch
$this->assertCount(2, $user->books);
// MongoDB User
$user = new User;
$user->name = 'John Doe';
$user->save();
// MongoDB has many
$book = new MysqlBook(['title' => 'Harry Potter']);
$otherBook = new MysqlBook(['title' => 'Game of Thrones']);

$user->mysqlBooks()->sync([$book->id,$otherBook->id]);
$user = User::find($user->_id);
$this->assertCount(2, $user->mysqlBooks);

$user->mysqlBooks()->sync([$book->id]);
$user = User::find($user->_id);
$this->assertCount(1, $user->mysqlBooks);

$user->mysqlBooks()->sync([$book->id,$otherBook->id]);
$user = User::find($user->_id);
$this->assertCount(2, $user->mysqlBooks);
}
}