duanpu4143 2017-06-23 11:31
浏览 48
已采纳

Symfony会话和phpunit麻烦

I have code in my controller like this:

/**
 * @param Request $request
 * @param $verifiedResponse

 * @Route(...)

 * @return mixed
 */
public function someAction(Request $request)
{
    $session = $request->getSession();
    $session->set('key', mt_rand(0, 999));

    if ($request->get('key') === $session->get('key')) {
       return true;
    } else {
       return $session->get('key');
    }
    throw $this->createAccessDeniedException();
}

And here is my 2 unit tests:

public function testSomeActionFirst()
{
   $client = static::createClient();
   $client->request('POST', $this->generateRoute('my_route'));
   return $client->getResponse()->getContent();
}

public function testSomeActionSecond($key)
{
   $client = static::createClient();
   $client->request('POST', $this->generateRoute('my_route'), [], [
       'key' => $key
   ]);
   $this->assertTrue($client->getResponse()->isOk()
}

And second test always fails, because response code is 403, because sessions does not saving between requests. How I can fix it?

  • 写回答

1条回答 默认 最新

  • dpoxk64080 2017-06-29 13:28
    关注

    Each test method is suppose to be run in isolation. Otherwise you'd run into hard to debug issues as one test would be influencing others. Single test failure would lead to failures of other tests. Such a test suite will give you very little value, as failing tests won't tell you what went wrong. You might not recognise this is a problem at first, but maintaining such a test suite quickly becomes very hard.

    Therefore you should be able to run each test separately.

    You should also be able to run tests in any order.

    The web client you're using in your tests already follows these rules and is being reset before each test method is run. Although it's technically possible to introduce test dependencies, even with such an approach the client would be reset. It's a good thing!

    If you need to perform some common actions in each test case, create helper methods that will encapsulate (and name!) that behaviour. For example:

    class MyTest extends WebTestCase
    {
        private $client;
    
        protected function setUp()
        {
            parent::setUp();
            $this->client = static::createClient();
        }
    
        public function testSomeActionSecond()
        {
           $key = $this->requestKey();
    
           $this->client->request(
               'POST',
               $this->generateRoute('my_route'), 
               [],
               ['key' => $key]
           );
    
           $this->assertTrue($this->client->getResponse()->isOk()
        }
    
        private function requestKey()
        {
           $this->client->request('POST', $this->generateRoute('my_route'));
    
           return $this->client->getResponse()->getContent();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵