gpt4 book ai didi

php - Symfony 或其他东西在执行测试时与登录用户混淆

转载 作者:行者123 更新时间:2023-11-28 21:11:52 25 4
gpt4 key购买 nike

经过大量研究但没有发现任何有用的信息,我来这里看看是否可以得到一些帮助。我正在为我的代码构建一些功能测试,其中一个测试需要一个假用户才能工作,因为在 Controller 的函数中我需要使用这段代码访问 User 对象:

$account = $this->getUser();
$user = $account->getUser();

这是我测试中的代码:

public function testmodifyCommissionAction() {
$this->logIn();

$updateCompanyCommission = $this->client->getContainer()->get('router')->generate('updateCompanyCommission', array(), false);
$this->client->request("POST", $updateCompanyCommission, array(
'id' => rand(1, 10),
'fee' => rand(1, 10)
));

var_dump($this->client->getResponse()->getContent());

$this->assertSame(200, $this->client->getResponse()->getStatusCode()); // Test if response is OK
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); // Test if Content-Type is valid application/json
$this->assertNotEmpty($this->client->getResponse()->getContent()); // Test that response is not empty

$decoded = json_decode($this->client->getResponse()->getContent(), true);

private function logIn() {
$session = $this->client->getContainer()->get('session');

$firewall = 'secured_area';
$token = new UsernamePasswordToken('admin', null, $firewall, array('ROLE_ADMIN'));
$session->set('_security_' . $firewall, serialize($token));
$session->save();

$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}

现在,这里的问题在哪里?如果我使用有效凭据登录我的应用程序并运行测试,则会收到此错误:

Cannot set session ID after the session has started. (500 Internal Server Error)

如果我注销并运行相同的测试,它会起作用,所以这里出了点问题,我无法得到什么,有什么帮助吗?建议?尖端?工作代码示例?

编辑:创建用户并尝试使用当前创建的用户登录

我修改了一些方法,现在这是我的当前代码:

protected function createUser() {
$kernel = new \AppKernel('test', true);
$kernel->boot();

$this->container = $kernel->getContainer();
$userManager = $this->container->get('fos_user.user_manager');
$userManager->createUser();

$data['firstname'] = $this->generateRandomString(4);
$data['lastname'] = $this->generateRandomString(4);
$data['lastname2'] = "";
$data['photo'] = "";
$data['banner'] = "";
$data['password'] = md5($this->generateRandomString(18));
$data['email'] = $this->generateRandomString(6) . "@test.com";

$user = $userManager->createAccountAndUser($data);

return $user;
}

这是我用来登录的功能,也进行了修改以添加对当前用户的支持:

private function logIn($client, $user) {
$session = $client->getContainer()->get('session');

$firewall = 'main';
$token = new UsernamePasswordToken($user, null, $firewall, array('ROLE_USER'));
$session->set('_security_' . $firewall, serialize($token));
$session->save();

$cookie = new Cookie($session->getName(), $session->getId());
$client->getCookieJar()->set($cookie);
}

这是我要运行的测试:

public function testmodifyCommissionAction() {
$client = static::createClient();
$user = $this->createUser();

$this->logIn($client, $user->getUser()->getAlias());

$updateCompanyCommission = $client->getContainer()->get('router')->generate('updateCompanyCommission', array(), false);
$client->request("POST", $updateCompanyCommission, array(
'id' => rand(1, 10),
'fee' => rand(1, 10)
));

$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$this->assertNotEmpty($client->getResponse()->getContent());
}

但失败并显示此消息:

PHP Fatal error:  Call to a member function getUser() on a non-object in /var/www/html/kraken/src/Wuelto/Company/ApprovalBundle/Controller/FeeCompanyController.php on line 47
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/pear/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/pear/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/TextUI/TestRunner.php:349
PHP 6. PHPUnit_Framework_TestSuite->runTest() /usr/share/pear/PHPUnit/Framework/TestSuite.php:745
PHP 7. PHPUnit_Framework_TestCase->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:775
PHP 8. PHPUnit_Framework_TestResult->run() /usr/share/pear/PHPUnit/Framework/TestCase.php:783
PHP 9. PHPUnit_Framework_TestCase->runBare() /usr/share/pear/PHPUnit/Framework/TestResult.php:648
PHP 10. PHPUnit_Framework_TestCase->runTest() /usr/share/pear/PHPUnit/Framework/TestCase.php:838
PHP 11. ReflectionMethod->invokeArgs() /usr/share/pear/PHPUnit/Framework/TestCase.php:983
PHP 12. Wuelto\Company\ApprovalBundle\Tests\Controller\FeeCompanyControllerTest->testmodifyCommissionAction() /usr/share/pear/PHPUnit/Framework/TestCase.php:983
PHP 13. Symfony\Component\BrowserKit\Client->request() /var/www/html/kraken/src/Wuelto/Company/ApprovalBundle/Tests/Controller/FeeCompanyControllerTest.php:71
PHP 14. Symfony\Bundle\FrameworkBundle\Client->doRequest() /var/www/html/kraken/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:325
PHP 15. Symfony\Component\HttpKernel\Client->doRequest() /var/www/html/kraken/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:111
PHP 16. Symfony\Component\HttpKernel\Kernel->handle() /var/www/html/kraken/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Client.php:81
PHP 17. Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle() /var/www/html/kraken/app/bootstrap.php.cache:2303
PHP 18. Symfony\Component\HttpKernel\HttpKernel->handle() /var/www/html/kraken/app/bootstrap.php.cache:3022
PHP 19. Symfony\Component\HttpKernel\HttpKernel->handleRaw() /var/www/html/kraken/app/bootstrap.php.cache:2883
PHP 20. call_user_func_array:{/var/www/html/kraken/app/bootstrap.php.cache:2911}() /var/www/html/kraken/app/bootstrap.php.cache:2911
PHP 21. Company\ApprovalBundle\Controller\FeeCompanyController->modifyCommissionAction() /var/www/html/kraken/app/bootstrap.php.cache:2911

我找不到问题出在哪里,有什么帮助吗?

解决方案:问题出在这一行:

$this->logIn($client, $user->getUser()->getAlias());

因为我试图传递 $user->getUser()->getAlias() 而不是 $user 对象。

编辑 2:排序代码并在尝试删除最近的测试用户时出错

现在我做了一些更改并将 $this->createUser() 传递给 setUp() 函数,以便在 $this- 上拥有 User 对象>用户变种。

public function setUp() {
static::$kernel = static::createKernel();
static::$kernel->boot();
$this->em = static::$kernel->getContainer()->get('doctrine')->getManager();

$fixture = new LoadFeeData();
$fixture->load($this->em);

$this->user = $this->createUser();

parent::setUp();
}

现在在 tearDown() 中,我试图删除我之前使用此代码创建的当前用户:

protected function tearDown() {
parent::tearDown();

$this->em->remove($this->user);
$this->em->flush();
$this->em->close();
}

但是得到这个错误:

1) Company\ApprovalBundle\Tests\Controller\FeeCompanyControllerTest::testmodifyCommissionAction
Undefined index: 0000000065c988b30000000022bd0894

/var/www/html/kraken/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2860
/var/www/html/kraken/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1736
/var/www/html/kraken/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1397
/var/www/html/kraken/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1677
/var/www/html/kraken/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1645
/var/www/html/kraken/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:647
/var/www/html/kraken/app/cache/test/jms_diextra/doctrine/EntityManager_533ab9b6c8c69.php:59
/var/www/html/kraken/src/Company/ApprovalBundle/Tests/Controller/FeeCompanyControllerTest.php:131

为什么我不能删除用户?

最佳答案

我通常做的是为测试环境启用 http_basic 安全性,然后执行以下操作以获得经过身份验证的客户端:

配置测试.yml

security:
firewalls:
wsse_secured: # This should be your firewall name
http_basic: true

接下来,确保你的测试扩展了 Symfony WebTestCase (Symfony\Bundle\FrameworkBundle\Test\WebTestCase)

然后你可以调用:

$client = $this->createClient([], [
'PHP_AUTH_USER' => $username,
'PHP_AUTH_PW' => $password,
]);

client->request("POST", $updateCompanyCommission, array(
'id' => rand(1, 10),
'fee' => rand(1, 10)
));

...

使用此方法,用户需要作为数据夹具加载,或者您可以编写一个方法在 setUp 中创建用户并在 tearDown< 中销毁用户。这是我用来创建用户进行测试的方法:

protected function createUser($password, $timezone, $roles)
{
$kernel = new \AppKernel('test', true);
$kernel->boot();

/** @var Account $user */
$this->container = $kernel->getContainer();
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->createUser();

$user->setEmail('unique-email-address@not-here.com');
$user->setUsername('auniqueusername');
$user->setPlainPassword($password);
$user->setTimezone($timezone);
$user->setEnabled(true);

foreach ($roles as $role) {
$user->addRole($role);
}

$userManager->updateUser($user);

return $user;
}

关于php - Symfony 或其他东西在执行测试时与登录用户混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22693300/

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