2016-07-30 71 views
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; 

感謝

回答

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()); 
    } 

} 

希望能幫助別人。

相關問題