3

내가 심포니 2.8 LiipFunctionalTestBundle에 따라 제 기능 테스트 (에 설비를 추가하고 싶습니다 비록 그것이 내가 작업 dev에 데이터베이스가, 나는 내가 가지고있는 것이기 때문에 여전히 설비를 추가해야합니까 :liip 기능 테스트에서 조명기를 추가하는 방법 (심포니 2.8)? .

  • 지역 데이터 (국가, 지역, 군)
  • vehicule 브랜드 및 모델
  • ... 각각의 기능 테스트는 나를 위해 좋은하지 않습니다 후 DB가 제거 가진 결과적으로

.

주의 사항 : 명령 줄을 통해 (정화없이) 덧붙이 비품은 성공하고있다 : 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'); 

하지만, 기능 테스트가 작동하지 도다 : 그래서, 나는이 변화를 수행하여 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 : 번들 버전 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

유일한 해결책을 읽고 한 줄을 수정은 테스트 데이터베이스를 만드는 것입니다.테스트 데이터베이스의

설정 데이터베이스의

# 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

은 기능 테스트 SQLite 데이터베이스를 사용하는 것이 가능하다에 기재된. 번들 구성에 대한 자세한 내용은 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'데이터베이스를 만들 수 없습니다; 데이터베이스 존재' –