具有多个 Doctrine EntityManager 的功能测试 Symfony
Functional Testing Symfony with mulitple Doctrine EntityManagers
在 Symfony 4 应用程序中,我配置了多个实体管理器。
我希望我的功能测试能够为两个管理器自动创建数据库表。
然而,当我运行我的测试; PHP 单元遇到错误
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146
Table 'test.example' doesn't exist
config/packages/doctrine.yaml:
doctrine:
dbal:
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
custom:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
default_entity_manager: default
auto_generate_proxy_classes: '%kernel.debug%'
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
Model:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Model'
prefix: 'App\Entity\Model'
alias: Model
custom:
connection: custom
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Custom:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Custom'
prefix: 'App\Entity\Custom'
alias: Custom
要创建表和加载固定装置,我目前正在使用 Liip Functional Test Bundle。
一个简单的测试如下所示:
tests\Functional\GeneralControllerTest.php:
class GeneralControllerTest extends WebTestCase
{
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->loadFixtures([
SomeFixtures::class,
MoreFixtures::class,
]);
}
/**
* @dataProvider providePageUris
* @test
* @param string $uri
*/
public function checkThatPageLoads($uri)
{
$client = static::createClient();
/* ... more code ... */
$client->request(Request::METHOD_GET, $uri);
static::assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
}
/**
* @return array
*/
public function providePageUris()
{
return [
["/dashboard"],
];
}
}
如何让我的测试用例为其他自定义 Doctrine EntityManagers 创建数据库表?
我试过添加:
$this->loadFixtures([], true, "custom");
还有:
$this->runCommand("doctrine:schema:create --env=test --em=custom");
到测试用例的 setUp() 方法,但这并没有产生所需的结果。在将任何 Fixture 加载到数据库之前,都需要创建数据库表。
根据 WebTestCase on GitHub 的代码,第二个参数应包含对象管理器的名称。我不确定指定 custom
是否足够,或者您是否必须指定全名 doctrine.entity_manager.custom
。
为了在加载数据固定装置时不在不同的实体管理器中删除表和连接,我更改了具有不同环境变量的学说配置。
config/packages/doctrine.yaml:
doctrine:
dbal:
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
custom:
url: '%env(resolve:DATABASE_URL_CUSTOM)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
在 Symfony 4 应用程序中,我配置了多个实体管理器。 我希望我的功能测试能够为两个管理器自动创建数据库表。
然而,当我运行我的测试; PHP 单元遇到错误
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.example' doesn't exist
config/packages/doctrine.yaml:
doctrine:
dbal:
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
custom:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
default_entity_manager: default
auto_generate_proxy_classes: '%kernel.debug%'
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
Model:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Model'
prefix: 'App\Entity\Model'
alias: Model
custom:
connection: custom
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Custom:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Custom'
prefix: 'App\Entity\Custom'
alias: Custom
要创建表和加载固定装置,我目前正在使用 Liip Functional Test Bundle。
一个简单的测试如下所示:
tests\Functional\GeneralControllerTest.php:
class GeneralControllerTest extends WebTestCase
{
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->loadFixtures([
SomeFixtures::class,
MoreFixtures::class,
]);
}
/**
* @dataProvider providePageUris
* @test
* @param string $uri
*/
public function checkThatPageLoads($uri)
{
$client = static::createClient();
/* ... more code ... */
$client->request(Request::METHOD_GET, $uri);
static::assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
}
/**
* @return array
*/
public function providePageUris()
{
return [
["/dashboard"],
];
}
}
如何让我的测试用例为其他自定义 Doctrine EntityManagers 创建数据库表?
我试过添加:
$this->loadFixtures([], true, "custom");
还有:
$this->runCommand("doctrine:schema:create --env=test --em=custom");
到测试用例的 setUp() 方法,但这并没有产生所需的结果。在将任何 Fixture 加载到数据库之前,都需要创建数据库表。
根据 WebTestCase on GitHub 的代码,第二个参数应包含对象管理器的名称。我不确定指定 custom
是否足够,或者您是否必须指定全名 doctrine.entity_manager.custom
。
为了在加载数据固定装置时不在不同的实体管理器中删除表和连接,我更改了具有不同环境变量的学说配置。
config/packages/doctrine.yaml:
doctrine:
dbal:
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
custom:
url: '%env(resolve:DATABASE_URL_CUSTOM)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4