In PHPUnit, I can declare a class variable and initialise it in the setup to use in each subsequent test like below. This has intellisense so when I type $this->bo, I can see my variable $bookService, or when I use $this->bookService-> the intellisense will get the functions inside that variable.
class BookServiceTest extends TestCase
{
protected BookService $bookService;
protected function setUp(): void
{
parent::setUp();
$this->bookService = new BookService();
}
public function test_get_all_books()
{
$books = $this->bookService->getAllBooks();
$this->assertNotEmpty($books);
}
}
How can I do the same in Pest PHP?
The equavalent seems to be using beforeEach. However, $this does not have the book service intellisensed because of the cannot load dynamic variables thing.
describe('book service', function () {
beforeEach(function () {
$this->bookService = new BookService();
});
it('should get all books', function () {
$books = $this->bookService->getAllBooks();
});
});
Using /** @var BookService $this->bookService **/ will not intellisense the bookService variable, but rather the $this variable. Where I want it tied to the variable instead.
My current solution is to declare the class variable at the top of describe, this retains the function intellisense, but now you need to declare the variable in use many many times.
describe('book service', function () {
$bookService = new BookService();
it('should get all books', function () use ($bookService) {
$books = $bookService->getAllBooks();
});
});