Hi @m7vm7v. Thanks for the suggestions, these are helpful. I'm just learning, hence the somewhat nonsensical casting test.
In #1, the automatic reset via RefreshDatabase could be useful, but are you sure that should enable models to be created in setUpBeforeClass()? Also, that seems to delete all the data in the database, which isn't exactly what I want, I just want to delete any data created in the test.
When I add this trait to the test class above, I get errors like this which refers to $this->assertEquals(self::NAME, $m->name, '') in testGetModel()
1) Tests\Feature\AircraftManufacturer2Test::testGetModel
ErrorException: Trying to get property 'name' of non-object
AircraftManufacturer2Test.php:60
I think my issue may be a feature request. Namely that in addition to setUp and tearDown, my request would be that Illuminate\Foundation\Testing\TestCase also provide the static methods setUpBeforeClass and tearDownAfterClass. The result should be that by replacing the hack self::parentSetUp() in the above code with parent::setUpBeforeClass() it would work. I believe a key step in these is bootstrapping the app. For example, this also works:
use Illuminate\Contracts\Console\Kernel;
...
/**
* Bootstrap the app. A key step in the non-static Tests\Feature\TestCase::setUp().
*
* @return void
*/
public static function boostrapApp()
{
$app = require __DIR__ . '/../../../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
}
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass(); // The empty PHPUnit method that does nothing
self::boostrapApp(); // Rather than parentSetUp()
AircraftManufacturer::create(['name' => self::NAME]);
}
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass(); // The empty PHPUnit method that does nothing
self::boostrapApp(); // Rather than parentSetUp()
$m = new AircraftManufacturer();
$m->where('name', self::NAME)->first()->delete();
}
In general, rather than adding models in the test itself, I suspect it's better to simply rely on separate seed scripts like database/seeds/AircraftManufacturerSeeder.php. However, I imagine there may be times when one would want to create a very specific model directly in a test. It would be nice to be able to do this once per class using setUpBeforeClass and tearDownAfterClass. But perhaps the developers felt that for some reason this is not a good idea. There's a somewhat related issue here.