1

我正在为使用 FOSUserBundle 的 Symfony 2 应用程序编写单元测试。与这个类似的问题不同: FOSUserBundle 单元测试

但是,我不使用 HTTP 身份验证(仅登录表单),并且我需要使用实际的用户实体而不是伪造的内存中的实体。

尽管进行了很多搜索和尝试,但我根本无法让它工作,整个过程是如此不透明,我什至不知道从哪里开始。这是我的代码:

protected $em;
protected $client;
protected $testuser;

public function setUp() { 
    $kernel = static::createKernel();
    $kernel->boot();
    $this->em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
    $this->em->beginTransaction();

    $this->client = static::createClient();

    $usermanager = $kernel->getContainer()->get('fos_user.user_manager');
    $this->testuser = $usermanager->createUser();
    $this->testuser->setUsername('test');
    $this->testuser->setEmail('test@lemuria.org');
    $this->testuser->setPlainPassword('test');
    $usermanager->updateUser($this->testuser);
}


public function testLogin() {
    $crawler = $this->client->request('GET', '/en/login');
    $form = $crawler->selectButton('_submit')->form(array(
        '_username'  => 'test',
        '_password'  => 'test',
        ));     
    $this->client->submit($form);

    $this->assertTrue($this->client->getResponse()->isRedirect(), 'should be redirected');
    $this->assertTrue($this->client->getResponse()->isRedirect('http://localhost/en/account'), 'should be redirected to account page');

    $crawler = $this->client->followRedirect();

它在第二个断言上失败了。据我所知,它重定向回登录页面。

我被困住了,我什至不知道从哪里开始寻找解决方案,因为显然不可能简单地找出登录失败的原因。

4

2 回答 2

2

您可以尝试添加echo $this->client->getResponse()->getContent()以向您显示可以查找错误的实际响应

于 2013-02-16T20:57:33.700 回答
1

创建一个AbstractControllerTest并创建一个授权客户端,setUp()如下所示:

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;
    }
} 

注意:请用您的测试用户名替换用户名。

然后,扩展AbstractControllerTest并使用全局$client发出请求,如下所示:

class ControllerTest extends AbstractControllerTest
{
    public function testIndexAction()
    {
        $crawler = $this->client->request('GET', '/admin/');

        $this->assertEquals(
            Response::HTTP_OK,
            $this->client->getResponse()->getStatusCode()
        );
    }
}

该方法经过测试并且工作正常

于 2014-12-01T07:03:23.710 回答