3

私はsymfonyの2.8にLiipFunctionalTestBundleに基づいて、私の機能テスト(中備品を追加したいと考えたとしても、それは私が取り組んでのdevのデータベースがあり、私は私が持っていますので、まだ備品を追加する必要があります:リップ機能テストでフィクスチャを追加する方法(symfony 2.8)? 。

  • 地域各機能テストは私にとっていいことではないだろう後にDBをパージ有するデータ(国、地域、郡)
  • VEHICULEブランドやモデル
  • ...

その結果、。

注意:コマンドラインから(パージなし)追加は備品は、成功して取り組んでいる:php app/console doctrine:fixtures:load --append

をので、以下の私の機能テストです:

<?php 

namespace Minn\APIBundle\Tests\Controller; 

use Liip\FunctionalTestBundle\Test\WebTestCase as WebTestCase; 
//use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as WebTestCase; 
use Minn\APIBundle\Tests\Fixtures\Entity\LoadBrandData; 
use Doctrine\Common\DataFixtures\Purger\ORMPurger; 

class BrandControllerTest extends WebTestCase { 

    public function setUp() { 
     $this->auth = array(
      'PHP_AUTH_USER' => 'restapi', 
      'PHP_AUTH_PW' => 'secretpw', 
     ); 

     $this->client = static::createClient(array(), $this->auth); 
    } 

    public function testJsonGetPageAction() { 
     $fixtures = array('Minn\APIBundle\Tests\Fixtures\Entity\LoadBrandData'); 
     $this->loadFixtures($fixtures); 
     $brands = LoadBrandData::$brands; 
     $brand = array_pop($brands); 

     $route = $this->getUrl('api_1_brand_get_brand', array('id' => $brand->getId(), '_format' => 'json')); 

     $this->client->request('GET', $route, array('ACCEPT' => 'application/json')); 
     $response = $this->client->getResponse(); 
     $this->assertJsonResponse($response, 200); 
     $content = $response->getContent(); 

     $decoded = json_decode($content, true); 
     $this->assertTrue(isset($decoded['id'])); 
    } 

    // .. 
} 

このテストでは、DBをパージします。

// removed code 
$fixtures = array('Minn\APIBundle\Tests\Fixtures\Entity\LoadBrandData'); 
$this->loadFixtures($fixtures); 

// new code 
$this->runCommand('doctrine:fixtures:load --append --no-interaction --fixtures=src/Minn/APIBundle/Tests/Fixtures/Entity/LoadBrandData.php'); 

しかし、機能テストは動作しませdoest:だから、私はこの変更を行うことによってlinkで提案されているコードを試してみました。

There was 1 error: 

1) Minn\APIBundle\Tests\Controller\BrandControllerTest::testJsonGetPageAction 
Error: Call to a member function getId() on null 

/home/amine/NetBeansProjects/minnapi/src/Minn/APIBundle/Tests/Controller/BrandControllerTest.php:27 

私はこの変更を行うことによって機能にloadFixtures()を利用可能なオプションを使用してみました:

// removed code: 
$fixtures = array('Minn\APIBundle\Tests\Fixtures\Entity\LoadBrandData'); 
$this->loadFixtures($fixtures); 

// new code 
$fixtures = array('Minn\APIBundle\Tests\Fixtures\Entity\LoadBrandData'); 
$this->loadFixtures($fixtures, null,'doctrine', ORMPurger::PURGE_MODE_DELETE); 

評決:DBは常に機能テストの各実行後にパージされます。

だから、どんな提案?

おかげで、

NB:は、機能的テストのためにSQLiteデータベースを使用することが可能であるcomposer.json

"doctrine/doctrine-fixtures-bundle": "dev-master", 
    "phpunit/phpunit": "5.4.*", 
    "liip/functional-test-bundle":"1.6.*", 
    "guzzle/guzzle": "v3.9.*" 
+0

機能テストに別のデータベースを使用しない理由はありますか? – geoB

+0

私はすべてのアイデアに公開しています。リンクや例がありますか? –

+0

バンドルの設定で説明したようにSQLiteデータベースを使用していますか? –

答えて

2

唯一の解決策を読み取るために1行を変更するには、テスト用データベースを作成することです。これを行うには、以下の手順は以下のとおりです。データベースのテストデータベース

# the config has to be done in config_test.yml 
doctrine: 
    dbal: 
     default_connection: default 
     connections: 
      default: 
       driver: pdo_mysql 
       host:  "%database_host%" 
       port:  "%database_port%" 
       dbname: test 
       user:  "%database_user%" 
       password: "%database_password%" 
       charset: UTF8 

作成や表

// this command is run only once (just for creating the testing db) 
$ php app/console doctrine:database:create --env=test 
// this command is needed when you have new entities 
$ php app/console doctrine:schema:create --env=test 

設定のテストでフィクスチャをロードするためにどのように?

$fixtures = array('Minn\APIBundle\Tests\Fixtures\Entity\LoadBrandData'); 
    $this->loadFixtures($fixtures); 
    $brands = LoadBrandData::$brands; 

他人に役立つことを願っています!

0

に記載されたバージョンをバンドル。バンドルの設定の詳細については、the docsを参照してください。たとえば、MySQLを使用している場合には、

$ php app/console doctrine:database:create --env=test 
$ php app/console doctrine:schema:create --env=test 

とテスト環境でデータベースを作成し、DEV環境のように、テスト環境で同じ動作を持つことが非常に簡単です。

あなたの器具はすぐにロードされるはずです。

編集: config_test.yml中:

doctrine: 
    dbal: 
     driver: pdo_mysql 
     host:  localhost 
     port:  3306 
     dbname: minnapi_test 
     user:  "%database_user%" 
     password: "%database_password%" 

.../web/app_test.phpにコピー.../web/app_dev.phpとパージデータベースを避けるために$kernel = new AppKernel('test', true);

+0

データベースがmySQLにすでに存在するため、あなたのソリューションを使用することはできません: 'SQLSTATE [HY000]:一般的なエラー:1007データベース 'minnapi'を作成できません。データベースは存在する ' –

関連する問題