Make sure created ICAO in factory is unique otherwise tests fail

This commit is contained in:
Nabeel Shahzad
2018-09-02 15:07:54 -04:00
parent f2f0873b2e
commit ebac18ebef

View File

@@ -2,18 +2,36 @@
use Faker\Generator as Faker;
/**
* Create an ICAO for use in the factory.
*/
if (!function_exists('createFactoryICAO')) {
function createFactoryICAO(): string
{
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$max = strlen($characters) - 1;
$string = '';
for ($i = 0; $i < 4; $i++) {
try {
$string .= $characters[random_int(0, $max)];
} catch (Exception $e) {
}
}
return $string;
}
}
/*
* Add any number of airports. Don't really care if they're real or not
*/
$factory->define(App\Models\Airport::class, function (Faker $faker) {
$used = [];
return [
'id' => function () {
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$string = '';
$max = strlen($characters) - 1;
for ($i = 0; $i < 4; $i++) {
$string .= $characters[random_int(0, $max)];
}
'id' => function () use ($used) {
do {
$string = createFactoryICAO();
} while (in_array($string, $used, true));
return $string;
},