-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Use precomputed hash instead of bcrypt in ModelFactory #3894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This is not particularly pretty. There is an easier way to do this to yield the same speedup, without having to hard code a computed hash. Give me a sec... |
What do you make of #3897 please? |
@GrahamCampbell Yeah I like it, I think I prefer this to expanded conditional:
|
Yeh, that looks prettier. Feel free to update your PR if you're happy with that, and I'll close mine. :) |
Cool done 👍 Thanks for your input! |
Seems like just $factory->define(App\User::class, function ($faker) {
static $password;
return [
'username' => $faker->userName,
'email' => $faker->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
}); |
Yeh, but only on PHP, not on HHVM. I guess we don't care about HHVM anymore though, so |
❤️❤️❤️ |
return [ | ||
'name' => $faker->name, | ||
'email' => $faker->safeEmail, | ||
'password' => bcrypt(str_random(10)), | ||
'password' => $password ?: $password = bcrypt('secret'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't using the Hash::make('secret')
method from the Hash facade be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bcrypt
global helper function does exactly that: https://github.com/laravel/framework/blob/5.3/src/Illuminate/Foundation/helpers.php#L191
Use a precomputed hash of the word "secret" instead of using
bcrypt
directly. Sincebcrypt
is intentionally slow, it can really slow down test suites in large applications that use factories to generate models in many tests.Here's a reference point from a user who saw their test suite get 5x faster thanks to this change 👀
#3456 (comment)