2016-02-04 29 views
6

Inizio con un database con seed e sto tentando di riseminare il database tra i test di unità in Laravel 5. In Laravel 4 capisco che è possibile utilizzare semplicemente Illuminate \ Support \ Facades \ Artisan e eseguire i comandiLaravel 5 Reseeding del database per il test dell'unità tra i test

Artisan :: call ('migrate'); Artisan :: call ('db: seed');

o si suppone possa fare:

$ this-> seme ('DatabaseSeeder');

prima di ogni test. In Laravel 5 sembra essere stato sostituito da

utilizzare DatabaseMigrations; o utilizzare DatabaseTransactions;

Ho provato a utilizzarli e sono riuscito a ottenere i test per migrare il database; tuttavia, non esegue nuovamente il reseeding dei dati nelle tabelle. Ho letto attraverso diversi forum che si lamentano di questo e ho provato diversi approcci chiamare questi dal TestCase e all'interno di ogni prova ... l'aggiunta del

$this->beforeApplicationDestroyed(function() { 
     Artisan::call('migrate'); 
     Artisan::call('migrate:reset'); 
     Artisan::call('db:seed'); 
     DB::disconnect(); 
    }); 

al TestCase.php tearDown() ...

ho anche provato ad aggiungere

$this->createApplication(); 

ad un metodo chiamato in ogni prova dal TestCase.php

a volte si pulisce solo i miei tavoli fuori completa LY. Nulla di ciò che trovo sul sito di Laravel o nei blog sembra funzionare. Parte di questo è probabilmente perché sto probabilmente provando i metodi di Laravel 4 in Laravel 5. C'è un modo per farlo in Laravel 5?

Il mio codice per la testcase.php assomiglia:

<?php 

use Illuminate\Support\Facades\Artisan as Artisan; 

class TestCase extends Illuminate\Foundation\Testing\TestCase{ 

    use Illuminate\Foundation\Testing\WithoutMiddleware; 
    use Illuminate\Foundation\Testing\DatabaseMigrations; 
    use Illuminate\Foundation\Testing\DatabaseTransactions; 

    protected $baseUrl = 'http://localhost'; 


    public function initializeTests(){ 

     $this->createApplication(); 

     Artisan::call('migrate'); 
     $this->artisan('migrate'); 
     Artisan::call('db:seed'); 
     $this->artisan('db:seed'); 
     $this->seed('DatabaseSeeder'); 
     $this->session(['test' => 'session']); 
     $this->seed('DatabaseSeeder'); 

    } 

    public function tearDown() 
    { 
     Mockery::close(); 
     Artisan::call('migrate:reset'); 
     $this->artisan('migrate:reset'); 
     Artisan::call('migrate:rollback'); 
     $this->artisan('migrate:rollback'); 
     Artisan::call('migrate'); 
     $this->artisan('migrate'); 
     Artisan::call('db:seed'); 
     $this->artisan('db:seed'); 
     $this->seed('DatabaseSeeder'); 
     DB::disconnect(); 

     foreach (\DB::getConnections() as $connection) { 
      $connection->disconnect(); 
     } 

     $this->beforeApplicationDestroyed(function() { 
     Artisan::call('migrate:reset'); 
     $this->artisan('migrate:reset'); 
     Artisan::call('migrate:rollback'); 
     $this->artisan('migrate:rollback'); 
     Artisan::call('migrate'); 
     $this->artisan('migrate'); 
     Artisan::call('db:seed'); 
     $this->artisan('db:seed'); 
     $this->seed('DatabaseSeeder'); 
     DB::disconnect(); 
     foreach (\DB::getConnections() as $connection) { 
      $connection->disconnect(); 
     } 
     }); 

     $this->flushSession(); 
     parent::tearDown(); 


    } 

    public function getConnection() 
    { 
     $Connection = mysqli_connect($GLOBALS['DB_DSN'], $GLOBALS['DB_USERNAME'], $GLOBALS['DB_PASSWORD'], $GLOBALS['DB_DATABASE']); 
     $this->createDefaultDBConnection(); 
     return $this->Connection; 
    } 

    public function createApplication() 
    { 
     $app = require __DIR__.'/../bootstrap/app.php'; 

     $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); 

     return $app; 
    } 

    /** 
    * Magic helper method to make running requests simpler. 
    * 
    * @param $method 
    * @param $args 
    * @return \Illuminate\Http\Response 
    */ 
    public function __call($method, $args) 
    { 
     if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) 
     { 
      return $this->call($method, $args[0]); 
     } 

     throw new BadMethodCallException; 
    } 

    /** 
    * Create a mock of a class as well as an instance. 
    * 
    * @param $class 
    * @return \Mockery\MockInterface 
    */ 
    public function mock($class) 
    { 
     $mock = Mockery::mock($class); 

     $this->app->instance($class, $mock); 

     return $mock; 
    } 

} 

mio test simile a

<?php 

use Illuminate\Foundation\Testing\WithoutMiddleware; 
use Illuminate\Foundation\Testing\DatabaseMigrations; 
use Illuminate\Foundation\Testing\DatabaseTransactions; 
use Illuminate\Database\Seeder; 
use Illuminate\Support\Facades\Artisan; 

class CustomerRegistrationControllerTest extends TestCase 
{ 

    use DatabaseMigrations; 

    protected static $db_inited = false; 

    protected static function initDB() 
    { 
     echo "\n---Customer Registration Controller Tests---\n"; // proof it only runs once per test TestCase class 
     Artisan::call('migrate'); 
     Artisan::call('db:seed'); 
    } 

    public function setUp() 
    { 

     parent::setUp(); 

     if (!static::$db_inited) { 
      static::$db_inited = true; 
      static::initDB(); 
     } 

//  $this->app->refreshApplication(); 
     $this->artisan('migrate:refresh'); 
     $this->seed(); 
     $this->seed('DatabaseSeeder'); 

     $this->initializeTests(); 

); 

    } 


    public function testSomething() 

    { 


     $this->Mock 
      ->shouldReceive('destroy') 
      ->with('1') 
      ->andReturn(); 


     $this->RegistrationController->postRegistration(); 
//  $this->assertResponseStatus(200); 

    } 

} 
+0

Questo link potrebbe aiutare https://laracasts.com/discuss/channels/tips/how-to-set-up-and-define-your-database-for-integration-testing – Raftalks

risposta

-1

Perché non creare il proprio comando come db: azzerato. Questo comando tronca tutte le tabelle o rilascia/crea schema e quindi esegue la migrazione.

Nel vostro test che si quindi utilizzare: $this->call('db:reset') tra i test

2

basta eseguire questo:

$this->artisan('migrate:refresh', [ 
     '--seed' => '1' 
    ]); 

Per evitare modifiche al database persistente tra i test aggiungere use DatabaseTransactions per i test che hanno colpito il database.