php - Use existing migrations tables for laravel unit testing -
i want use existing set of migrations create tables me stuff seed data into. using orchestra testbench testing. examples this show creating table on fly:
db::schema()->create('oauth_identities', function($table) {...
but want use existing migrations.
testcase.php:
use orchestra\testbench\testcase orchestratestcase; abstract class testcase extends orchestratestcase { protected function getbasepath() { // reset base path point our package's src directory return __dir__ . '/../vendor/orchestra/testbench/fixture'; } }
dbtestcase.php:
class dbtestcase extends testcase { protected $artisan;
/* * bootstrap application */ public function setup() { parent::setup(); $this->artisan = $this->app->make('illuminate\contracts\console\kernel'); $this->artisan('migrate', [ '--database' => 'testbench', '--realpath' => realpath(__dir__.'/../database/migrations'), ] ); } protected function getenvironmentsetup($app) { parent::getenvironmentsetup($app); $app['config']->set('database.default', 'testbench'); $app['config']->set('database.connections.testbench', [ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '' ] ); }
usertest.php:
class usertest extends dbtestcase { use databasemigrations; private function seeduser() { return [ 'email' => 'myemail', ... ]; ... } ... public function testcreateuser() { $user = user::create($this->seeduser()); die(print_r($user)); ...
error:
in \app\tests\usertest::testcreateuser general error: 1 no such table: users
but in migrations, creating table:
schema::create('users', function (blueprint $table) {...
so how can use existing migration tables , generate seed data migration?
edit: when running phpunit
after setting db driver in dbtestcase, 'driver' => 'sqlite',
, throws error:
class 'doctrine\dbal\driver\pdosqlite\driver' not found in .../database/sqliteconnection.php
navigating sqliteconnection.php
, pdosqlite
in use doctrine\dbal\driver\pdosqlite\driver doctrinedriver;
highlighted in red, saying it's undefined namespace.
i have available driver in database.php:
database.php:
'default' => env('db_connection', 'mysql'), 'connections' => [ 'sqlite' => [ 'driver' => 'sqlite', 'database' => storage_path('database.sqlite'), 'prefix' => '', ],
in case using laravel 5 can set test cases use data migrations this:
<?php use illuminate\foundation\testing\withoutmiddleware; use illuminate\foundation\testing\databasemigrations; use illuminate\foundation\testing\databasetransactions; class exampletest extends testcase { use databasemigrations; /** * basic functional test example. * * @return void */ public function testbasicexample() { $this->visit('/') ->see('laravel 5'); } }
by using databasemigration
trait database migrated before each test case , rolled after it. please remember if try testing same data in different test cases.
Comments
Post a Comment