taylornetwork/laravel-username-generator

Bug impossible to get a username

You2s opened this issue · 9 comments

You2s commented

First of all, thank you for the very fast addition of the feature, it's the first time I've seen such a reactivity!
Unfortunately I have a problem, I installed the package, published the configuration (php artisan vendor:publish), and tried to use the package with tinker (php artisan tinker) but it doesn't seem to work:
Capture d’écran 2019-10-31 à 20 17 15

Capture d’écran 2019-10-31 à 20 18 23

Can you help me ?

Hi @You2s,

That's very strange, I've run the tests and they all pass and then I spun it up in a new app and I'm not getting that issue.

What version of Laravel are you using?

You2s commented

I'm using laravel 5.7 just upgraded (1 day ago from 5.6)

Can you run composer dump-autoload and then under tinker

>>>  UsernameGenerator::generate();
>>>  UsernameGenerator::generate('Test User');

And see if that fixes it? Because on 5.7 I can't duplicate it unfortunately.

You2s commented

Already tried, same result

You2s commented

I tried this to add a Log::debug in the makeUnique function of the BaseDriver.php file :

     /**
     * Make the username unique.
     *
     * @param string $text
     *
     * @return string
     */
    public function makeUnique(string $text): string
    {
        if ($this->getConfig('unique') && $this->model() && method_exists($this->model(), 'findSimilarUsernames')) {
            Log::debug($this->model()->findSimilarUsernames($text));
            if (($similar = count($this->model()->findSimilarUsernames($text))) > 0) {
                return $text.$this->getConfig('separator').$similar;
            }
        }

        return $text;
    }

and get this:
[2019-10-31 20:02:00] local.DEBUG: 0

You2s commented

I may have found, in the findSimilarUsernames function, there is a return 0; if the username does not exist in db, this returned 0 is used as a parameter in the count() function that expects an object. Finally the function can return an int or an object.

I tried to replace this:

public function findSimilarUsernames($username)
    {
        if (count(static::where(config('username_generator.column', 'username'), $username)->get()) === 0) {
            return 0;
        }

        return static::where(config('username_generator.column', 'username'), 'LIKE', $username.'%')->get();
    }

By this:

public function findSimilarUsernames($username)
    {
        if (count(static::where(config('username_generator.column', 'username'), $username)->get()) === 0) {
            return static::where(config('username_generator.column', 'username'), $username)->get();
        }

        return static::where(config('username_generator.column', 'username'), 'LIKE', $username.'%')->get();
    }

And it's works

You're 100% right, I'll fix right now.

You2s commented

Thank you for your reactivity

No problem! I've published release v2.3.1 that fixes the issue. Appreciate the help in locating the bug!