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 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥30 关于#wpf# devexpress的问题:选中时是黑色未选中是白色字体,想要更改未选中时字体的颜色(语言-c#)
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答