You'll need to install phpunit/dbunit composer require --dev phpunit/dbunit
.
DbUnitTestCase
is an abstract class that uses onBefore and onAfter events to setup the laravel application and run artisan migrate commands. It also sets some environment variables to make sure we use a test database connection.PatientsTest
andAppointmentTest
are example tests that make use of dbunits provided ArrayData set to provide the initial dataset that tests run against.AppointmentAltTest
is a remake ofAppointmentTest
that utilizesModelFactoryDataset
to provide the test dataset. The benefit ofModelFactoryDataset
is that you can make use of your configured model factories to generate most of the record set data and you only need to provide override values for columns you care about. That could be none of the columns or all depending on your testing needs.ModelFactoryDatset
is a dataset implementation that will make use of configured model factories to populate row column data for you, but also gives you the opportunity to provide column data overrides. Construct the instance the same way you would an array data set where you pass an array where key is the model class name and value is an array of record arrays where each record is a key value array of column=>value. It's simplist usage might benew ModelFactoryDataset([Patient::class => [[],[]]]);
which creates 2 patient records relying on model factory to populate all the column values for each record.App\Patient
andApp\Appointment
are just example models for the exercisedatabase/factories/ModelFactory.php
configures model factories for Patient and Appointment models