如何在 Silex php 框架的功能测试中模拟经过身份验证的用户
How to simulate authenticated users in functional tests in Silex php framework
我正在尝试对受身份验证表单保护的页面进行功能测试。
我的代码基于此link:http://symfony.com/doc/current/testing/simulating_authentication.html
但我还是不行。
这是我收到的错误消息:
PHP Fatal error: Call to undefined method Symfony\Component\HttpKernel\Client::getContainer()
这是我的代码:
class ClientTest extends WebTestCase {
public function createApplication()
{
return require __DIR__.'/../app.php';
}
/**
* @test
*/
public function index_when_no_clients()
{
$this->client = $this->createClient();
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Clients")'));
$this->assertCount(1, $crawler->filter('span[class="glyphicon glyphicon-plus"]'));
}
/**
* @test
*/
public function add_get_route_is_valid()
{
$client = $this->createClient();
$session = $client->getContainer()->get('session');
// the firewall context (defaults to the firewall name)
$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());
$client->getCookieJar()->set($cookie);
$crawler = $client->request('GET', '/add');
$this->assertTrue($client->getResponse()->isOk());
}
}
这是我在 index.php 中设置身份验证的方式:
$app->register(new \Silex\Provider\SessionServiceProvider(), [
'session.test' => 'test' === $app['env'],
]);
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'admin' => array(
'pattern' => '^/add',
'form' => array('login_path' => '/login', 'check_path' => '/add/login_check'),
'users' => $app->share(function() use ($app) {
$config = Configuration::getInstance();
$pdoQueries = new PdoQueries(PdoConnection::getInstance($config));
return new UserProvider($pdoQueries);
}),
),
)
));
$app->get('/login', function(Request $request) use ($app, $arrConfig) {
return $app['twig']->render('auth/form.twig', array(
'error' => $app['security.last_error']($request),
'last_username' => $app['session']->get('_security.last_username'),
'title' => 'Login',
'assetsUrl' => $arrConfig['assets_url']
));
});
这里是在 createApplication 中调用的 app.php 的内容:
<?php
require __DIR__ ."/../www/index.php";
unset($app['exception_handler']);
return $app;
谢谢
我找到了问题和解决方案。
问题出在 getContainer 语句上。在 silex 中以不同的方式调用依赖注入。刚刚打电话
$app['session']
直接解决问题
这里是完整的脚本,其中包含要导入的 symfony 类。
<?php
use Silex\WebTestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\BrowserKit\Cookie;
class ClientTest extends WebTestCase {
public function createApplication()
{
return require __DIR__.'/../app.php';
}
private function logIn($client)
{
$session = $this->app['session'];
$firewall = 'admin';
$token = new UsernamePasswordToken('admin', null, $firewall, array('ROLE_ADMIN'));
$session->set('_security_'.$firewall, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$client->getCookieJar()->set($cookie);
return $client;
}
/**
* @test
*/
public function index_when_no_clients()
{
$this->client = $this->createClient();
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Clients")'));
$this->assertCount(1, $crawler->filter('span[class="glyphicon glyphicon-plus"]'));
}
/**
* @test
*/
public function add_get_route_is_valid()
{
$client = $this->createClient();
$client = $this->logIn($client);
$crawler = $client->request('GET', '/add');
$this->assertTrue($client->getResponse()->isOk());
}
}
希望对其他人有所帮助。
我正在尝试对受身份验证表单保护的页面进行功能测试。
我的代码基于此link:http://symfony.com/doc/current/testing/simulating_authentication.html
但我还是不行。
这是我收到的错误消息:
PHP Fatal error: Call to undefined method Symfony\Component\HttpKernel\Client::getContainer()
这是我的代码:
class ClientTest extends WebTestCase {
public function createApplication()
{
return require __DIR__.'/../app.php';
}
/**
* @test
*/
public function index_when_no_clients()
{
$this->client = $this->createClient();
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Clients")'));
$this->assertCount(1, $crawler->filter('span[class="glyphicon glyphicon-plus"]'));
}
/**
* @test
*/
public function add_get_route_is_valid()
{
$client = $this->createClient();
$session = $client->getContainer()->get('session');
// the firewall context (defaults to the firewall name)
$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());
$client->getCookieJar()->set($cookie);
$crawler = $client->request('GET', '/add');
$this->assertTrue($client->getResponse()->isOk());
}
}
这是我在 index.php 中设置身份验证的方式:
$app->register(new \Silex\Provider\SessionServiceProvider(), [
'session.test' => 'test' === $app['env'],
]);
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'admin' => array(
'pattern' => '^/add',
'form' => array('login_path' => '/login', 'check_path' => '/add/login_check'),
'users' => $app->share(function() use ($app) {
$config = Configuration::getInstance();
$pdoQueries = new PdoQueries(PdoConnection::getInstance($config));
return new UserProvider($pdoQueries);
}),
),
)
));
$app->get('/login', function(Request $request) use ($app, $arrConfig) {
return $app['twig']->render('auth/form.twig', array(
'error' => $app['security.last_error']($request),
'last_username' => $app['session']->get('_security.last_username'),
'title' => 'Login',
'assetsUrl' => $arrConfig['assets_url']
));
});
这里是在 createApplication 中调用的 app.php 的内容:
<?php
require __DIR__ ."/../www/index.php";
unset($app['exception_handler']);
return $app;
谢谢
我找到了问题和解决方案。
问题出在 getContainer 语句上。在 silex 中以不同的方式调用依赖注入。刚刚打电话
$app['session']
直接解决问题
这里是完整的脚本,其中包含要导入的 symfony 类。
<?php
use Silex\WebTestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\BrowserKit\Cookie;
class ClientTest extends WebTestCase {
public function createApplication()
{
return require __DIR__.'/../app.php';
}
private function logIn($client)
{
$session = $this->app['session'];
$firewall = 'admin';
$token = new UsernamePasswordToken('admin', null, $firewall, array('ROLE_ADMIN'));
$session->set('_security_'.$firewall, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$client->getCookieJar()->set($cookie);
return $client;
}
/**
* @test
*/
public function index_when_no_clients()
{
$this->client = $this->createClient();
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Clients")'));
$this->assertCount(1, $crawler->filter('span[class="glyphicon glyphicon-plus"]'));
}
/**
* @test
*/
public function add_get_route_is_valid()
{
$client = $this->createClient();
$client = $this->logIn($client);
$crawler = $client->request('GET', '/add');
$this->assertTrue($client->getResponse()->isOk());
}
}
希望对其他人有所帮助。