我正在尝试对受身份验证表单保护的页面进行功能测试。
我的代码基于以下链接: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;
由于
答案 0 :(得分:3)
我发现了问题和解决方案。
问题是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());
}
}
希望能帮助别人。