对于每个测试用例,为了隔离和性能,最好尽可能少加载夹具(测试套件可能会非常缓慢)。
当夹具相互依赖时,您只需使用学说参考管理它们并相互链接,同时注意顺序。作为示例,假设简单的用户和角色关系。
管理角色夹具的通用类:
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
}
}
希望这有帮助。