yii2-faker icon indicating copy to clipboard operation
yii2-faker copied to clipboard

Index and databese id's dont match

Open AdeAttwood opened this issue 6 years ago • 3 comments

Is anyone else having an issue with $index not being the same as the record id? Because my database id's start at 1 and the index being passed into the faker template starts at 0 the record id does not match the index.

if in the loop was for ($i = 1; $i <= $this->count; $i++) { The index and the record id would be the same.

AdeAttwood avatar Mar 28 '18 12:03 AdeAttwood

@cebe whats the plan now? Can I make the change or is it best to wait until the status is labelled "status:ready for adoption". Not sure on the workflow.

AdeAttwood avatar Apr 17 '18 05:04 AdeAttwood

@AdeAttwood could you come up with a unit test that shows the problem?

cebe avatar Apr 23 '18 08:04 cebe

@cube I have come up with this test. I did have an issue building the docker image, so I have run all the tests in a modified php:7.1-cli image. Dont know if the sqlite::memory dsn will work in your image thats a convo for another day.

    public function testIndexMatchesPrimaryKey()
    {
        $userTable = [
            'user_id' => 'pk',
            'username' => 'string',
            'email' => 'string',
            'auth_key' => 'string',
            'password' => 'string',
            'created_at' => 'integer',
            'updated_at' => 'integer'
        ];

        Yii::$app->db->createCommand()->createTable('user', $userTable)->execute();

        $this->_fixtureController->actionGenerate('user');
        $fixtureData = require Yii::getAlias('@runtime/faker/user.php');
        array_shift($userTable);

        Yii::$app->db->createCommand()->batchInsert('user', array_keys($userTable), $fixtureData)->execute();
        $rows = Yii::$app->db->createCommand('SELECT * FROM user')->queryAll();

        foreach ($rows as $row) {
            $validatePassword = Yii::$app->security->validatePassword('password_'.$row['user_id'], $row['password']);
            $this->assertTrue($validatePassword);
        }
    }

I had to add password to the user fixture from the docs. And also had to modify the moc app to include the database component.

    new $appClass(ArrayHelper::merge([
            'id' => 'testapp',
            'basePath' => __DIR__,
            'vendorPath' => dirname(__DIR__) . '/vendor',
            'components' => [
                'db' => [
                    'class' => '\yii\db\Connection',
                    'dsn' => 'sqlite::memory:',
                ],
            ],
        ], $config));

It did break one of the tests with Undefined index: profile0 because it will be profile1

AdeAttwood avatar Apr 23 '18 19:04 AdeAttwood