Php Symfony或其他东西在执行测试时与登录用户混淆
经过大量的研究,没有发现任何有用的东西,我来这里看看是否能得到一些帮助。我正在为我的代码构建一些功能测试,其中一个测试需要一个假用户进行工作,因为在控制器的功能中,我需要使用以下代码访问Php Symfony或其他东西在执行测试时与登录用户混淆,php,symfony,testing,functional-testing,Php,Symfony,Testing,Functional Testing,经过大量的研究,没有发现任何有用的东西,我来这里看看是否能得到一些帮助。我正在为我的代码构建一些功能测试,其中一个测试需要一个假用户进行工作,因为在控制器的功能中,我需要使用以下代码访问user对象: $account = $this->getUser(); $user = $account->getUser(); 这是我测试中的代码: public function testmodifyCommissionAction() { $this->logIn();
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);
}
现在,问题出在哪里?如果我使用有效凭据登录应用程序并运行测试,则会出现以下错误:
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
会话启动后无法设置会话ID。(500)内部
服务器错误)
如果我注销并运行相同的测试,它会工作,所以这里有问题,我无法得到什么,有什么帮助吗?建议?提示?工作代码的示例
编辑:创建用户并尝试使用当前创建的用户登录
我对方法做了一些修改,现在这是我的当前代码:
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\u基本安全性,然后执行以下操作以获得经过身份验证的客户端: config_test.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)
));
...
使用此方法,用户需要作为数据固定装置加载,或者您可以编写一个方法,在设置中创建用户,并在拆卸中销毁用户。以下是我用来为测试创建用户的方法:
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;
}
不起作用,存在加载prod
和其他不同test
环境的安全设置的任何方法?每个环境的安全设置在调用$kernel=new\AppKernel('test',true)中相应的.yml文件(config_prod.yml,config_dev.yml,config_test.yml)中定义代码>您可以设置正在加载的环境。错误似乎出现在您的FeeCompanyController
中。你能发布包含第47行的方法的代码吗?我解决了这个问题,但当我试图删除为测试而创建的用户时,会出现另一个问题,你能看到并告诉我是否有问题吗?如果用户对象($this->User
)是从createUser
函数返回的对象,则它不是条令管理的对象。在您的tearDown
中,对您的用户存储库执行$refreshedUser=$userRepository->find($userId)
,然后将返回的用户传递给$this->em->remove($refreshedUser)