Skip to content

Instantly share code, notes, and snippets.

@filipfilipovich
Created May 26, 2023 12:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save filipfilipovich/e583dc8af60b160090a61589e57eaff1 to your computer and use it in GitHub Desktop.
Save filipfilipovich/e583dc8af60b160090a61589e57eaff1 to your computer and use it in GitHub Desktop.
BookRepository test file
<?php
use App\Models\Book;
use App\Repositories\BookRepository;
beforeEach(function () {
$this->mockParameters = ['author' => 'Max Minimal'];
$this->mockBooksCollection = Book::factory(5)->create($this->mockParameters);
$this->bookRepository = new BookRepository();
});
it('can find books', function () {
$this->mockParameters['title'] = 'Sonic the Hedgehog';
$this->mockBooksCollection = $this->mockBooksCollection->add(Book::factory()->create($this->mockParameters));
expect($this->bookRepository->findBy($this->mockParameters))->count()->toBeOne();
})->group('books'); // Assign 'books' group to this test
it('can filter out books', function () {
$this->mockBooksCollection = $this->mockBooksCollection
->add(Book::factory()->create([
'author' => $mockAuthor = 'George R. R. Martin',
'title' => 'A Game of Thrones'
]))->add(Book::factory()->create([
'author' => 'J. K. Rowling',
'title' => $mockTitle = 'Harry Potter and the Chamber of Secrets'
]));
$this->mockParameters = [
'author' => $mockAuthor,
'title' => $mockTitle
];
expect($this->bookRepository->filterBy($this->mockParameters))
->toHaveCount(2)
->first()->author->toBe($mockAuthor)
->last()->title->toBe($mockTitle);
})->group('books'); // Assign 'books' group to this test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment