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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘