Lumen-framework: Factories not working for in-memory DB

Created on 1 May 2018  路  3Comments  路  Source: laravel/lumen-framework

  • Lumen Version: 5.6.3
  • PHP Version: 7.2
  • Database Driver & Version: In-memory SQLite (unit testing)

Description:

Hi folks.

When creating a record (insert operation) in DB in unit testing the factories don't work properly.

Steps To Reproduce:

class ControllerTest extends TestCase
{
    use \Laravel\Lumen\Testing\DatabaseMigrations;
    use \Laravel\Lumen\Testing\DatabaseTransactions;

    // It works
    public function testExistingRecord()
    {
        DB::select('select * from user'); // Empty array
        DB::insert('insert into user (first_name, last_name) values(?, ?)', ["test", "test"]); // Record inserted
        DB::select('select * from user'); // Array with one result
    }
}

But it doesn't work when:

factory(User::class, $amount)->create(); // Please consider $amount = 1

It throws the following error:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: user

/var/www/vendor/illuminate/database/Connection.php:664
/var/www/vendor/illuminate/database/Connection.php:624
/var/www/vendor/illuminate/database/Connection.php:459
/var/www/vendor/illuminate/database/Connection.php:411
/var/www/vendor/illuminate/database/Query/Processors/Processor.php:32
/var/www/vendor/illuminate/database/Query/Builder.php:2349
/var/www/vendor/illuminate/database/Eloquent/Builder.php:1285
/var/www/vendor/illuminate/database/Eloquent/Model.php:726
/var/www/vendor/illuminate/database/Eloquent/Model.php:691
/var/www/vendor/illuminate/database/Eloquent/Model.php:554
/var/www/vendor/illuminate/database/Eloquent/FactoryBuilder.php:195
/var/www/vendor/illuminate/support/Collection.php:376
/var/www/vendor/illuminate/database/Eloquent/FactoryBuilder.php:196
/var/www/vendor/illuminate/database/Eloquent/FactoryBuilder.php:174
/var/www/tests/TestCase.php:51
/var/www/tests/app/Http/Controllers/ControllerTest.php:78

phpunit.xml config:

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="DB_CONNECTION" value="testing" />
    <env name="CACHE_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
</php>

Most helpful comment

@dunice Thanks for leaving your solution here. Very helpfully. Wait for help but no time to give help ;)

All 3 comments

I have yet to see such issue using in-memory database with Lumen testing.

I found the issue. It wasn't related to Lumen/Laravel...

Closing.

@dunice Thanks for leaving your solution here. Very helpfully. Wait for help but no time to give help ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhengyuhe123 picture zhengyuhe123  路  4Comments

megawubs picture megawubs  路  5Comments

rmblstrp picture rmblstrp  路  5Comments

Andrei-Vakulski picture Andrei-Vakulski  路  3Comments

timrogers picture timrogers  路  3Comments