기능 테스트에 픽스쳐를 추가하고 싶습니다 ( 심포니 2.8의 LiipFunctionalTestBundle 을 기반으로 합니다. 내가 작업하는 dev 데이터베이스라도 픽스쳐를 추가해야합니다.
따라서 각 기능 테스트 후에 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');
그러나 기능 테스트는 작동하지 않습니다.
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는 항상 기능 테스트를 실행할 때마다 제거됩니다.
그래서, 어떤 제안 ??
감사,
참고 : composer.json에 설명 된 번들 버전
"doctrine/doctrine-fixtures-bundle": "dev-master",
"phpunit/phpunit": "5.4.*",
"liip/functional-test-bundle":"1.6.*",
"guzzle/guzzle": "v3.9.*"
데이터베이스 제거를 방지하는 유일한 해결책은 테스트 데이터베이스를 만드는 것입니다. 이를 수행하기위한 단계는 다음과 같습니다.
# 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;
다른 사람들에게 도움이되기를 바랍니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다