2016-09-02 5 views
1

私はPHPUnitのログイン機能を持っており、これをテストユーザとして認可に使用しています。phpunitからログインすると、現在のユーザのオブジェクトはsymfonyコントローラでNULLです

$checkResult = $this->get('dwd.service.coupon')->checkCoupon(
      $coupon, 
      $request->query->all(), 
      $this->getUser()->getPortalId() 
      // Here $this->getUser returned NULL 
     ); 
ここ

私がテスト

public function setUp() 
{ 
    parent::setUp(); 

    $this->client = static::createClient(); 
    $this->logIn($this->client); 

} 
+0

リポジトリがユーザオブジェクトを配列ではないと確信していますか? 'findOne()'ではありませんか? – malcolm

+0

@malcolmうん、それはオブジェクトを返します。 http://prntscr.com/cdad5q –

答えて

0

のセットアップで私のテストユーザーを()認可しましたが、何とかのgetUser()がnullを返した関数:テストアクションで

private function logIn(Client $client) 
{ 
    $session = $client->getContainer()->get('session'); 

    /** @var User $user */ 
    $user = $client->getContainer()->get('doctrine')->getRepository('DWDAdminBundle:User')->find(1); 
    //Here I have user object 

    $firewall = 'main'; 
    $token = new UsernamePasswordToken($user, null, $firewall, ['ROLE_ADMIN']); 
    $client->getContainer()->get('security.token_storage')->setToken($token); 
    $session->set('_security_'.$firewall, serialize($token)); 
    $session->save(); 

    $cookie = new Cookie($session->getName(), $session->getId()); 
    $client->getCookieJar()->set($cookie); 
} 

は、このコードを持っていますテストされたアクションは同じファイアウォールの下にありますか?

+0

はい、私は 'メイン'ファイアウォールを使用します –

関連する問題