douyinjiao9351 2019-05-09 08:05
浏览 222
已采纳

使用PHPUnit测试Controller,但它会抛出401错误

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->client->request('GET','/create');

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

public function testIndex()
{

    $this->client->request('GET','/');

    $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();
    
        $this->logIn();
    }
    
    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 https://symfony.com/doc/current/reference/configuration/security.html#firewall-context
        $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('Test@Test.com', ['ROLE_ADMIN'],
            array("employeeid" => array(420),
            "mail" => array("testing@test.com")), false);
        $token = new UsernamePasswordToken($testUser, null, $firewallName, ['ROLE_ADMIN']);
        //var_dump($token);
        $session->set('_security_' . $firewallContext, serialize($token));
        $session->save();
    
        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);
    }
    
    
    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,
        ]);
    
        $this->client->submit($form);
    }
    
    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP