Hi folks.
When creating a record (insert operation) in DB in unit testing the factories don't work properly.
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>
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 ;)
Most helpful comment
@dunice Thanks for leaving your solution here. Very helpfully. Wait for help but no time to give help ;)