Make sure created ICAO in factory is unique otherwise tests fail
This commit is contained in:
@@ -2,18 +2,36 @@
|
|||||||
|
|
||||||
use Faker\Generator as Faker;
|
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
|
* Add any number of airports. Don't really care if they're real or not
|
||||||
*/
|
*/
|
||||||
$factory->define(App\Models\Airport::class, function (Faker $faker) {
|
$factory->define(App\Models\Airport::class, function (Faker $faker) {
|
||||||
|
$used = [];
|
||||||
return [
|
return [
|
||||||
'id' => function () {
|
'id' => function () use ($used) {
|
||||||
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
do {
|
||||||
$string = '';
|
$string = createFactoryICAO();
|
||||||
$max = strlen($characters) - 1;
|
} while (in_array($string, $used, true));
|
||||||
for ($i = 0; $i < 4; $i++) {
|
|
||||||
$string .= $characters[random_int(0, $max)];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user