Skip to content

Commit

Permalink
Add tests for #37
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jun 12, 2023
1 parent 1faf026 commit 6fe09d6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ foreach($users as $user) {
$user->hasFavorited($post);
}

// with favoriteable object
$users = User::with('favorites.favoriteable')->get();

foreach($users as $user) {
$user->hasFavorited($post);
}

// Favoriteable
$posts = Post::with('favorites')->get();
// or
Expand Down
22 changes: 22 additions & 0 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ public function test_eager_loading()
$this->assertEmpty($sqls->all());
}

public function test_eager_loading_from_favorite_model()
{
$user = User::create(['name' => 'overtrue']);

$post1 = Post::create(['title' => 'Hello world!']);
$post2 = Post::create(['title' => 'Hello everyone!']);
$book1 = Book::create(['title' => 'Learn laravel.']);
$book2 = Book::create(['title' => 'Learn symfony.']);

$user->favorite($post1);
$user->favorite($post2);
$user->favorite($book1);
$user->favorite($book2);

// start recording
$sqls = $this->getQueryLog(function () use ($user) {
Favorite::with('favoriteable')->where('user_id', $user->id)->get();
});

$this->assertSame(3, $sqls->count()); // from "favorites", "posts", "books"
}

public function test_eager_loading_error()
{
// hasFavorited
Expand Down

0 comments on commit 6fe09d6

Please sign in to comment.