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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?