gpt4 book ai didi

symfony - 使用 LiipFunctionalTestBundle 和固定装置进行独立功能测试?

转载 作者:行者123 更新时间:2023-12-04 01:38:53 27 4
gpt4 key购买 nike

我正在向 Symfony2 项目添加测试。以前我在 devtest 环境中使用相同的数据库,它使用的 MySQL 数据库已经填充了与生产服务器相同的数据。

测试是相互依赖的,因为一些测试依赖于以前的测试。例如,如果我有一个商店网站,我在购物车中添加了一个产品,然后从购物车中删除了该产品。因此,我需要先使用表单插入数据,然后才能将其删除。

现在我想进行独立的功能测试,因为那是 recommended way (by one of Symfony2's developers) .

我已经配置了 LiipFunctionalTestBundletest 环境中正确使用 SQLite 数据库,我已经开始使用 DoctrineFixturesBundle 添加固定装置。 .

但我不知道每次功能测试需要加载多少数据。我应该在测试开始时加载什么夹具?当实体因表间关系而依赖于其他实体时,如何处理CRUD操作?

假设我正在开发一家商店,我想要进行一些测试:

  1. 用户在购物车中添加一些产品
  2. 用户从购物车中移除一件产品
  3. 用户订购剩余产品

我应该为每个步骤创建不同的夹具吗?这意味着我的固定装置需要以多种不同的状态存在:空车、订购了一种产品的车等。这对我来说似乎是正确的,但非常耗时,所以我想知道我的想法是否有效。

最佳答案

对于每个测试用例,为了隔离和性能,最好加载尽可能少的夹具(测试套件可能会非常慢)。

当 fixture 相互依赖时,您只需使用 doctrine reference 管理它们并相互链接,同时注意顺序。例如,假设简单的用户和角色关系。

管理角色夹具的通用类:

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 Functional Test Bundle:

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

}

}

希望这对您有所帮助。

关于symfony - 使用 LiipFunctionalTestBundle 和固定装置进行独立功能测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28584393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com