5

I use hautelook/alice-bundle.

I can't use encoded bcrypt password in my fixture because of the following error ($ in interpreted as a reference to an object) :

In SimpleObjectGenerator.php line 114:

An error occurred while generating the fixture "trainee" (App\Document\Trainee): Could not resolve value during the generation process.

App\Document\Trainee:
# template
trainee (template):
    firstName:              <fr_FR:firstName()>
    lastName:               <fr_FR:lastName()>
    email (unique):         <fr_FR:email()>
    password :              $2y$13$I5uLW8atzRPmC3NcvirYqO2htdMHH1l4uFQ3z0V8wHowO0FqTXl7u
    plainPassword:          password
    birthdate:              <date('now')>
    address:                '@address_tr_*'
    phoneNumber:            <fr_FR:phoneNumber()>
    profileCompleted:       false

Do you have an idea why ? Thanksss

tux23
  • 215
  • 4
  • 11

3 Answers3

9

You can put the hashed password in parameters, like this:

parameters:
    hash: $2y$13$I5uLW8atzRPmC3NcvirYqO2htdMHH1l4uFQ3z0V8wHowO0FqTXl7u

App\Document\Trainee:
    trainee (template):
        password: <{hash}>
        ...
umadesign
  • 236
  • 3
  • 6
0

You must simply escape every $ with \$

for your example:

App\Document\Trainee:
    trainee (template):
        [...]
        password: '\$2y\$13\$I5uLW8atzRPmC3NcvirYqO2htdMHH1l4uFQ3z0V8wHowO0FqTXl7u'
Mischa
  • 693
  • 8
  • 15
-1

I suggest you to simply set a plaintext encoder in the test environment and set plaintext passwords in your fixtures.

First, switch to a plaintext password encoder in test environnement:

# config/packages/test/security.yaml
security:
    encoders:
        App\Entity\User:
            algorithm: plaintext

Then in your fixture:

App\Entity\User:
    user1:
        username: user1@example.com
        password: 'password'

You can now use the plaintext password in your tests:

public function testLoginWithUsernameAndPassword()
{
    $response = static::createHttpClient()->request('POST', '/api/login', ['json' => [
        'username' => 'user1@example.com',
        'password' => 'password'
    ]]);

    // assert $response
}
dogawaf
  • 360
  • 2
  • 12