I’ve been using laracasts/Testdummy a lot recently which is a great tool for setting up any testing you’re doing. It auto-generates your Laravel 5 models with faker data.
However, sometimes when you’re setting up a very similar named modelĀ it becomes laborious copying and pasting the same things across multiple instances with the same default data.
So here’s my super easy “pattern” so you’re not creating too much. All it relies on is using the array_merge
to overwrite the default set.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Grade | |
$grade_defaults = [ | |
'courseid' => 'factory:App\Models\Course', | |
'itemname' => $faker->sentence, | |
'itemtype' => $faker->word, | |
'score' => $faker->randomFloat(2, 0, 1), | |
'grademax' => 100 | |
]; | |
$factory('App\Models\Grade', | |
$grade_defaults | |
); | |
$factory('App\Models\Grade', 'grade_grademax_not_100', array_merge( | |
$grade_defaults, | |
[ | |
'grademax' => $faker->numberBetween(1,100) | |
]) | |
); | |
$factory('App\Models\Grade', 'grade_score_10', array_merge( | |
$grade_defaults, | |
[ | |
'score' => 10 | |
]) | |
); | |
$factory('App\Models\Grade', 'grade_score_90', array_merge( | |
$grade_defaults, | |
[ | |
'score' => 90 | |
]) | |
); |