@zunkt I never use Mock for testing my queries.
I have this query in one of my apps
public function index()
{
$books = BookView::orderBy('author_name')
->orderBy('series_started')
->orderBy('part')
->orderBy('released')
->orderBy('title')
->get();
return view('books.index')->with(['books' => $books, 'type' => 'books']);
}
And to test the sorting I do this
/**
* @test
*/
public function when_listing_all_books_they_are_alphabetically_sorted_by_authors_last_name()
{
$jordan = Author::factory()->create([
'last_name' => 'Jordan',
'first_name' => 'Robert'
]);
$terry = Author::factory()->create([
'last_name' => 'Goodkind',
'first_name' => 'Terry'
]);
$sarah = Author::factory()->create([
'last_name' => 'Ash',
'first_name' => 'Sarah'
]);
$bookJordan = Book::factory()->create();
$bookTerry = Book::factory()->create();
$bookSarah = Book::factory()->create();
AuthorBook::factory()->create([
'book_id' => $bookTerry->id,
'author_id' => $terry->id
]);
AuthorBook::factory()->create([
'book_id' => $bookJordan->id,
'author_id' => $jordan->id
]);
AuthorBook::factory()->create([
'book_id' => $bookSarah->id,
'author_id' => $sarah->id
]);
$response = $this->get('/books');
$response->assertSeeInOrder([
'Ash, Sarah',
'Goodkind, Terry',
'Jordan, Robert'
]);
}
/**
* @test
*/
public function books_by_the_same_author_is_sorted_by_relase_year()
{
$author = Author::factory()->create([
'last_name' => 'Goodkind',
'first_name' => 'Terry'
]);
$book1 = Book::factory()->create([
'title' => 'The Law Of Nines',
'released' => 2009
]);
$book2 = Book::factory()->create([
'title' => 'The Wizards First Rule',
'released' => 1994
]);
$book3 = Book::factory()->create([
'title' => 'Nest',
'released' => 2016
]);
AuthorBook::factory()->create([
'book_id' => $book1->id,
'author_id' => $author->id
]);
AuthorBook::factory()->create([
'book_id' => $book2->id,
'author_id' => $author->id
]);
AuthorBook::factory()->create([
'book_id' => $book3->id,
'author_id' => $author->id
]);
$response = $this->get('/books');
$response->assertSeeInOrder([
'The Wizards First Rule',
'The Law Of Nines',
'Nest'
]);
}
/**
* @test
*/
public function books_in_the_same_series_is_sorted_by_the_first_book_in_the_series_release_year_then_by_part()
{
$author = Author::factory()->create([
'last_name' => 'Goodkind',
'first_name' => 'Terry'
]);
$book1 = Book::factory()->create([
'title' => 'The Law Of Nines',
'series' => 'Standalone',
'released' => 2009
]);
$book2 = Book::factory()->create([
'title' => 'The Wizards First Rule',
'series' => 'The Sword Of Truth',
'part' => 1,
'released' => 1994
]);
$book3 = Book::factory()->create([
'title' => 'The Stone Of Tears',
'series' => 'The Sword Of Truth',
'part' => 2,
'released' => 2010
]);
AuthorBook::factory()->create([
'book_id' => $book1->id,
'author_id' => $author->id
]);
AuthorBook::factory()->create([
'book_id' => $book2->id,
'author_id' => $author->id
]);
AuthorBook::factory()->create([
'book_id' => $book3->id,
'author_id' => $author->id
]);
$response = $this->get('/books');
$response->assertSeeInOrder([
'The Wizards First Rule',
'The Stone Of Tears',
'The Law Of Nines',
]);
}