0
我试图通过身份验证表单进行功能测试。如何在Silex php框架中的功能测试中模拟经过身份验证的用户
我的代码是基于此链接: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;
感谢