我正在向一个Symfony2项目添加测试。以前,我在dev和test环境中使用了相同的数据库,它使用了一个已经填充了与生产服务器上相同数据的MySQL数据库。
这些测试是可靠的,因为有些测试依赖于以前的测试。例如,如果我有一个商店网站,我在购物车中添加了一个产品,然后将产品从购物车中删除。因此,在能够删除数据之前,我需要使用表单插入数据。
现在我想使用独立的功能测试,因为这是推荐的方法(由Symfony2 2的开发人员推荐)。
我已经正确地将LiipFunctionalTestBundle配置为在test环境中使用SQLite数据库,并且我已经开始用DoctrineFixturesBundle添加补丁。
但我不知道我需要为每个功能测试加载多少数据。在测试开始时,我应该加载哪些夹具?当实体由于表之间的关系而依赖其他实体时,如何处理CRUD操作?
假设我正在开发一家商店,我需要一些测试:
我应该为每一步创建一个不同的夹具吗?这意味着我的装置需要在许多不同的状态下存在:空车,有一个产品订购的购物车,等等。对我来说,这似乎是正确的,但非常耗时,所以我想知道我的想法是否有效。
发布于 2015-02-18 13:42:06
对于每个测试用例来说,为了隔离和性能,最好尽可能少加载夹具(测试套件可以非常慢)。
当夹具相互依赖时,您只需用理论参考来管理它们,并将它们联系起来,同时也照顾好订单。例如,假设简单的用户和角色关系。
用于管理角色夹具的泛型类:
abstract class BaseLoadRoleData extends AbstractFixture implements OrderedFixtureInterface
{
public function getOrder()
{
return 1;
}
protected function createRole(ObjectManager $manager, $rolename)
{
$role= new Role();
$role->setName($rolename);
$manager->persist($role);
$manager->flush();
$this->setReference('role-' . $rolename, $role);
}
}用于简单角色的专用类
class LoadSimpleRoleData extends BaseLoadRoleData
{
public function load(ObjectManager $manager)
{
$this->createRole($manager, Role::SIMPLE);
}
}用于管理角色的专用类
class LoadAdminRoleData extends BaseLoadRoleData
{
public function load(ObjectManager $manager)
{
$this->createRole($manager, Role::ADMIN);
}
}以及用户:用于管理用户夹具的泛型类:
abstract class BaseLoadUserData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* @var ContainerInterface
*/
private $container;
/**
* {@inheritDoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function getOrder()
{
return 2;
}
protected function buildUser($username, $firstName = "",$lastName ="")
{
$user= new User();
$user->setUsername($username);
$user->setFirstName($firstName);
$user->setLastName($lastName);
return $user;
}
}为简单用户提供的专用类
class LoadSimpleUserData extends BaseLoadUserData {
/**
* Load data fixtures with the passed EntityManager
*
* @param Doctrine\Common\Persistence\ObjectManager $manager
*/
function load(ObjectManager $manager)
{
$user = $this->buildUser($manager, "simple@example.com");
$user->addRole($this->getReference('role-'.Role::SIMPLE));
$manager->persist($user);
$manager->flush();
$this->setReference('user-' . "admin@example.com", $user);
}
}管理用户专用的类
class LoadAdminUserData extends BaseLoadUserData {
/**
* Load data fixtures with the passed EntityManager
*
* @param Doctrine\Common\Persistence\ObjectManager $manager
*/
function load(ObjectManager $manager)
{
$user = $this->buildUser($manager, "admin@example.com");
$user->addRole($this->getReference('role-'.Role::ADMIN));
$manager->persist($user);
$manager->flush();
$this->setReference('user-' . "admin@example.com", $user);
}现在您可以根据Liip单独使用它了,例如:
class LoginControllerTest {
public function testAdminUserLogin()
{
$this->loadFixtures(array(
'Acme\DemoBundle\DataFixtures\ORM\LoadAdminRoleData',
'Acme\DemoBundle\DataFixtures\ORM\LoadAdminUserData'
));
// you can now run your functional tests with a populated database
$client = static::createClient();
// ...
// test the login with admin credential
}
public function testSimpleUserLogin()
{
// add all your fixtures classes that implement
// Doctrine\Common\DataFixtures\FixtureInterface
$this->loadFixtures(array(
'Acme\DemoBundle\DataFixtures\ORM\LoadSimpleRoleData',
'Acme\DemoBundle\DataFixtures\ORM\LoadSimpleUserData'
));
// you can now run your functional tests with a populated database
$client = static::createClient();
// ...
// test the login with simple user credential
}
}希望能帮上忙。
https://stackoverflow.com/questions/28584393
复制相似问题