如何在 liip 功能测试(symfony 2.8)中附加固定装置?
How to append fixtures in liip functional test (symfony 2.8)?
我想在我的功能测试中附加固定装置(基于 symfony 2.8 中的 LiipFunctionalTestBundle。即使它是我正在处理的开发数据库,我仍然需要附加固定装置,因为我将拥有:
- 区域数据(国家、地区、县)
- 车辆品牌和型号
- ...
因此,在每次功能测试后清除数据库对我来说不是一件好事。
注意: 通过命令行附加固定装置(不清除)成功: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']));
}
// ..
}
此测试会清除数据库。因此,我通过以下更改尝试了 link 中提出的代码:
// 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.*"
可以使用 SQLite 数据库进行功能测试。有关捆绑包配置的详细信息,请参阅 the docs。例如,如果您使用的是 MySQL,那么在测试环境中使用
创建数据库非常简单
$ php app/console doctrine:database:create --env=test
$ php app/console doctrine:schema:create --env=test
并且在测试环境中的行为与在开发环境中的行为相同。
您的固定装置应该很容易加载。
编辑:
在 config_test.yml:
doctrine:
dbal:
driver: pdo_mysql
host: localhost
port: 3306
dbname: minnapi_test
user: "%database_user%"
password: "%database_password%"
复制.../web/app_dev.php
到.../web/app_test.php
并修改一行为$kernel = new AppKernel('test', true);
避免清除数据库的唯一解决方案是创建一个测试数据库。为此,步骤如下:
测试数据库的配置
# 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
如何在测试中加载fixture?
$fixtures = array('Minn\APIBundle\Tests\Fixtures\Entity\LoadBrandData');
$this->loadFixtures($fixtures);
$brands = LoadBrandData::$brands;
希望对大家有所帮助!
我想在我的功能测试中附加固定装置(基于 symfony 2.8 中的 LiipFunctionalTestBundle。即使它是我正在处理的开发数据库,我仍然需要附加固定装置,因为我将拥有:
- 区域数据(国家、地区、县)
- 车辆品牌和型号
- ...
因此,在每次功能测试后清除数据库对我来说不是一件好事。
注意: 通过命令行附加固定装置(不清除)成功: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']));
}
// ..
}
此测试会清除数据库。因此,我通过以下更改尝试了 link 中提出的代码:
// 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.*"
可以使用 SQLite 数据库进行功能测试。有关捆绑包配置的详细信息,请参阅 the docs。例如,如果您使用的是 MySQL,那么在测试环境中使用
创建数据库非常简单$ php app/console doctrine:database:create --env=test
$ php app/console doctrine:schema:create --env=test
并且在测试环境中的行为与在开发环境中的行为相同。
您的固定装置应该很容易加载。
编辑: 在 config_test.yml:
doctrine:
dbal:
driver: pdo_mysql
host: localhost
port: 3306
dbname: minnapi_test
user: "%database_user%"
password: "%database_password%"
复制.../web/app_dev.php
到.../web/app_test.php
并修改一行为$kernel = new AppKernel('test', true);
避免清除数据库的唯一解决方案是创建一个测试数据库。为此,步骤如下:
测试数据库的配置
# 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
如何在测试中加载fixture?
$fixtures = array('Minn\APIBundle\Tests\Fixtures\Entity\LoadBrandData');
$this->loadFixtures($fixtures);
$brands = LoadBrandData::$brands;
希望对大家有所帮助!