【发布时间】:2016-10-23 02:51:44
【问题描述】:
我想在我的功能测试中附加固定装置(基于 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);
结论:每次运行功能测试后都会清除数据库。
那么,有什么建议吗??
谢谢,
注意: 在 composer.json 中描述的捆绑版本
"doctrine/doctrine-fixtures-bundle": "dev-master",
"phpunit/phpunit": "5.4.*",
"liip/functional-test-bundle":"1.6.*",
"guzzle/guzzle": "v3.9.*"
【问题讨论】:
-
是否有某些理由不使用单独的数据库进行功能测试?
-
我对所有想法持开放态度。您有链接或示例吗?
-
您是否使用捆绑包配置中描述的 SQLite 数据库?
-
我用mysql。而已。没有sqlite
-
如果由于某种原因不能选择 SQLite(例如,当您使用 SQLite 中不可用的 MySQL 函数时),您始终可以为测试环境定义一个单独的 MySQL 数据库。至于附加fixture,请使用
...DataFixtures\ORM作为开发环境,..DataFixtures\Test作为测试环境。
标签: symfony phpunit functional-testing liipfunctionaltestbundle