feature/username-for-users #70

Merged
bence merged 10 commits from feature/username-for-users into develop 2023-09-25 20:04:50 +02:00
2 changed files with 270 additions and 0 deletions
Showing only changes of commit 2c706cc7f3 - Show all commits

View File

@ -0,0 +1,247 @@
<?php namespace MapGuesser\Util;
class UsernameGenerator
{
const ADJECTIVES = [
'abundant',
'agile',
'alluring',
'ample',
'adorable',
'angry',
'anxious',
'astonishing',
'beautiful',
'big',
'bitter',
'blissful',
'blue',
'brave',
'bright',
'brilliant',
'busy',
'calm',
'captivating',
'careful',
'charming',
'cheerful',
'clumsy',
'colorful',
'confused',
'cooperative',
'courageous',
'cozy',
'crispy',
'curious',
'dazzling',
'delightful',
'determined',
'eager',
'elegant',
'enchanting',
'enthusiastic',
'exciting',
'exquisite',
'faithful',
'fancy',
'fearless',
'fierce',
'fluffy',
'fresh',
'friendly',
'frigid',
'funny',
'gentle',
'glorious',
'graceful',
'grateful',
'happy',
'harmonious',
'healthy',
'helpful',
'honest',
'hopeful',
'hot',
'humble',
'hungry',
'impressive',
'infamous',
'innocent',
'intense',
'jolly',
'joyful',
'kind',
'lively',
'lonely',
'lovely',
'lucky',
'mysterious',
'naughty',
'nervous',
'nutritious',
'obedient',
'peaceful',
'playful',
'polite',
'powerful',
'precious',
'proud',
'radiant',
'reckless',
'reliable',
'rich',
'romantic',
'rough',
'sad',
'scary',
'sensitive',
'shiny',
'silky',
'sincere',
'sleepy',
'smart',
'sneaky',
'soft',
'sparkling',
'splendid',
'strong',
'stubborn',
'sweet',
'tender',
'thoughtful',
'thrilling',
'timid',
'tranquil',
'trustworthy',
'unique',
'vibrant',
'victorious',
'warm',
'wise',
'witty',
'wonderful',
'worried',
'zealous'
];
const NOUNS = [
'airplane',
'ant',
'apple',
'aquarium',
'backpack',
'banana',
'bear',
'bee',
'camera',
'car',
'cat',
'chocolate',
'desk',
'diamond',
'dog',
'dolphin',
'duck',
'egg',
'eiffeltower',
'elephant',
'fire',
'flower',
'forest',
'fork',
'fox',
'galaxy',
'giraffe',
'globe',
'guitar',
'hammer',
'hamster',
'hat',
'house',
'icecream',
'iguana',
'island',
'jacket',
'jaguar',
'jellyfish',
'jigsaw',
'kangaroo',
'key',
'kite',
'koala',
'lamp',
'lighthouse',
'lightning',
'lion',
'llama',
'moon',
'mountain',
'mouse',
'necklace',
'nest',
'newt',
'notebook',
'ocean',
'octopus',
'orchid',
'owl',
'panda',
'pencil',
'penguin',
'piano',
'queen',
'quilt',
'quokka',
'rabbit',
'rainbow',
'robot',
'ship',
'snake',
'statue',
'sun',
'sunflower',
'table',
'telescope',
'tiger',
'tree',
'turtle',
'uakari',
'umbrella',
'unicorn',
'universe',
'vase',
'violin',
'volcano',
'vulture',
'wallaby',
'waterfall',
'whale',
'xray',
'xylophone',
'yacht',
'yak',
'yarn',
'yeti',
'zebra',
'zeppelin',
'zucchini',
];
function generate(): string
{
$numberOfAdjectives = count(self::ADJECTIVES);
$numberOfNouns = count(self::NOUNS);
$firstAdjective = self::ADJECTIVES[mt_rand(0, $numberOfAdjectives - 1)];
do {
$secondAdjective = self::ADJECTIVES[mt_rand(0, $numberOfAdjectives - 1)];
} while ($firstAdjective === $secondAdjective);
$noun = self::NOUNS[mt_rand(0, $numberOfNouns - 1)];
$firstAdjective = ucfirst($firstAdjective);
$secondAdjective = ucfirst($secondAdjective);
$noun = ucfirst($noun);
return $firstAdjective . $secondAdjective . $noun;
}
}

View File

@ -0,0 +1,23 @@
<?php namespace MapGuesser\Tests\Util;
use MapGuesser\Util\UsernameGenerator;
use PHPUnit\Framework\TestCase;
final class UsernameGeneratorTest extends TestCase
{
public function testCanGenerateRandomUsernameFromComponents(): void
{
$generator = new UsernameGenerator();
$parts = $this->getUsernameParts($generator->generate());
$this->assertEquals(3, count($parts));
$this->assertContains($parts[0], UsernameGenerator::ADJECTIVES);
$this->assertContains($parts[1], UsernameGenerator::ADJECTIVES);
$this->assertContains($parts[2], UsernameGenerator::NOUNS);
}
private function getUsernameParts(string $username): array
{
return explode('-', strtolower(preg_replace('/([a-z])([A-Z])/', '$1-$2', $username)));
}
}