douyinjiao9351 2019-05-09 08:05
浏览 222


I'm doing some tests with PHPUnit, but I have some problems especially when testing the Controller.

I want to test a route of a Controller by just calling the route and check if the response is a HTTP-Status 200. But alwas the Console has the smae errors.

Token Error It says something about a token that is missing. I don't have a clue what kind of token this is.

401 Error

Also this is the 401 Error he gets.

My PHPUnit-Test looks like this:

namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PersonControllerTest extends WebTestCase { private $client;

protected function setUp()
    $this->client = static::createClient([], [
        'PHP_AUTH_USER' => 'user',
        'PHP_AUTH_PW'   => 'pass',

public function testCreate()


    $this->assertEquals(200, $this->client->getResponse()->getStatusCode());

public function testIndex()


    $this->assertEquals(200, $this->client->getResponse()->getStatusCode());


I have tried it with the authentication in the setUp Method -> didn't work.

Do you have any clue?

Thanks for all Answers or/and Comments

  • 写回答

2条回答 默认 最新

  • dongwen2896 2019-05-13 10:01
    protected function setUp()
        $this->client = static::createClient();
    private function logIn()
        $session = $this->client->getContainer()->get('session');
        $firewallName = 'test';
        // if you don't define multiple connected firewalls, the context defaults to the firewall name
        // See
        $firewallContext = 'test';
        // you may need to use a different token class depending on your application.
        // for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken
        $testUser = new WebserviceUser('', ['ROLE_ADMIN'],
            array("employeeid" => array(420),
            "mail" => array("")), false);
        $token = new UsernamePasswordToken($testUser, null, $firewallName, ['ROLE_ADMIN']);
        $session->set('_security_' . $firewallContext, serialize($token));
        $cookie = new Cookie($session->getName(), $session->getId());
    public function testCreate()
        $crawler = $this->client->request('GET', '/create');
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
        $form = $crawler->filter('form')->form([
            'person[name]' => 'Chruit',
            'person[birthdate][month]' => 2,
            'person[birthdate][day]' => 15,
            'person[birthdate][year]' => 2014,
    public function testIndex()
        $this->client->request('GET', '/');
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());


    The Solution is just the logIn-Function. The $testUser needs to be in your Active Directory or your network. The rest of the Parameters doesn't matter.

    My Firewall blocked it so i couldn't do it the normal way.

    If You are doing it like this, you have to add someting to the secourity.yaml that was created from the installation of PHPUnit.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥50 悬赏帮写C++编程 诚信
  • ¥15 在竞速练习时输出和输入可以同时进行
  • ¥15 端口连接数为什么会有限制
  • ¥15 安卓数据提交之后格式不对
  • ¥15 需要数据库运行的图片
  • ¥15 如何获取vue-video-editor?
  • ¥100 vs2019 mfc程序如何实现64*64/48*48大小的真彩色工具栏
  • ¥15 全志v3s耳机音频输出口怎么外接功放
  • ¥15 华为ensp使用基本ACL限制公司网络访问
  • ¥15 帮我做下照片上的PLC题