Symfony2 - Tests with FOSUserBundle

后端 未结 2 1955
生来不讨喜
生来不讨喜 2020-12-28 09:18

i would write a test for Symfony2 with FOSUserBundle.

At the moment i tried some ways and no one works.

I need a function like \"createAuthClient\".

相关标签:
2条回答
  • 2020-12-28 09:42

    Create an AbstractControllerTest and create an authorized client on setUp() as follow:

    <?php
    // ...
    use Symfony\Component\BrowserKit\Cookie;
    
    abstract class AbstractControllerTest extends WebTestCase
    {
        /**
         * @var Client
         */
        protected $client = null;
    
    
        public function setUp()
        {
            $this->client = $this->createAuthorizedClient();
        }
    
        /**
         * @return Client
         */
        protected function createAuthorizedClient()
        {
            $client = static::createClient();
            $container = $client->getContainer();
    
            $session = $container->get('session');
            /** @var $userManager \FOS\UserBundle\Doctrine\UserManager */
            $userManager = $container->get('fos_user.user_manager');
            /** @var $loginManager \FOS\UserBundle\Security\LoginManager */
            $loginManager = $container->get('fos_user.security.login_manager');
            $firewallName = $container->getParameter('fos_user.firewall_name');
    
            $user = $userManager->findUserBy(array('username' => 'REPLACE_WITH_YOUR_TEST_USERNAME'));
            $loginManager->loginUser($firewallName, $user);
    
            // save the login token into the session and put it in a cookie
            $container->get('session')->set('_security_' . $firewallName,
                serialize($container->get('security.context')->getToken()));
            $container->get('session')->save();
            $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));
    
            return $client;
        }
    } 
    

    NOTE: Please, replace the username with your test username.

    Then, extends the AbstractControllerTest and use the global $client to make requests as follow:

    class ControllerTest extends AbstractControllerTest
    {
        public function testIndexAction()
        {
            $crawler = $this->client->request('GET', '/admin/');
    
            $this->assertEquals(
                Response::HTTP_OK,
                $this->client->getResponse()->getStatusCode()
            );
        }
    }
    

    This method tested and works fine

    0 讨论(0)
  • 2020-12-28 09:53

    The best way I have found to test with an authenticated user is to just visit your login page and submit the form with user name and password you have loaded from a fixture. This may seem slow and cumbersome but will test what the user will actually do. You can even create your own method to make using it quick and easy.

    public function doLogin($username, $password) {
       $crawler = $this->client->request('GET', '/login');
       $form = $crawler->selectButton('_submit')->form(array(
           '_username'  => $username,
           '_password'  => $password,
           ));     
       $this->client->submit($form);
    
       $this->assertTrue($this->client->getResponse()->isRedirect());
    
       $crawler = $this->client->followRedirect();
    }
    
    0 讨论(0)
提交回复
热议问题