如果测试的操作重定向,则电子邮件的功能测试失败

时间:2015-01-01 19:32:34

标签: php symfony functional-testing swiftmailer

控制器操作会导致发送电子邮件,然后重定向。由于重定向,此操作的功能测试失败。如果重写控制器以呈现模板,则测试通过。 [这似乎是正确的;可以使用[Symfony的文档] [1]中的代码复制这种情况。

编辑:测试失败

  

“/ usr / bin / php”“/ usr / bin / phpunit”“ - colors”“ - log-junit”   “/tmp/nb-phpunit-log.xml”“ - bootstrap”   “/home/george/volunteer/app/bootstrap.php.cache”“ - configuration”   “/home/george/volunteer/app/phpunit.xml.dist”“ - filter”   “%\ btestActivateOrganization \ B%”   “/home/george/netbeans-8.0.1/php/phpunit/NetBeansSuite.php”   “--run = /家庭/乔治/志愿者/ SRC /特拉基/ MatchingBundle /测试/控制器/ AdminControllerTest.php”   Sebastian Bergmann的PHPUnit 3.7.28。

     

从/home/george/volunteer/app/phpunit.xml.dist读取的配置

     

˚F

     

时间:1.36秒,内存:40.75Mb

     

有1次失败:

     

1)   特拉基\ MatchingBundle \测试\控制器\ AdminControllerTest :: testActivateOrganization   声明0匹配预期为1失败。

     

/home/george/volunteer/src/Truckee/MatchingBundle/Tests/Controller/AdminControllerTest.php:64

     

FAILURES!测试:1,断言:1,失败:1。

     

完成。

控制器

public function activateOrgAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $organization = $em->getRepository("TruckeeMatchingBundle:Organization")->find($id);
    $temp = $organization->getTemp();
    if (true === $temp) {
        $organization->setTemp(false);
        $organization->setActive(true);
        $orgName = $organization->getOrgName();
        $em->persist($organization);
        $em->flush();
        $to = $em->getRepository("TruckeeMatchingBundle:Staff")->getActivePersons($id);
        $mailer = $this->container->get('admin.mailer');
        $mailer->activateOrgMail($organization, $to);
        $flash = $this->get('braincrafted_bootstrap.flash');
        $flash->success("$orgName has been activated");
    }

    return $this->redirect($this->generateUrl('admin_home'));
}

编辑3:更完整的测试夹具

class AdminControllerTest extends WebTestCase
{

    private $client;

    public function setUp()
    {
        $classes = array(
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadFocusSkillData',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadAdminUser',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadStaffUserGlenshire',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadStaffUserMelanzane',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadTemplateData',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadOpportunity',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadVolunteer',
        );
        $this->loadFixtures($classes);
        $this->client = $this->createClient();
        $this->client->followRedirects();
    }

    public function login($user)
    {
        $crawler = $this->client->request('GET', '/login');
        $form = $crawler->selectButton('Login')->form();
        $form['_username'] = $user;
        $form['_password'] = '123Abcd';
        $crawler = $this->client->submit($form);

        return $crawler;
    }
    public function testActivateOrganization()
    {
        $crawler = $this->login('admin');
        $link = $crawler->selectLink('Accept organization')->link();
        $crawler = $this->client->click($link);

        $mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
        $this->assertEquals(1, $mailCollector->getMessageCount());
    }
...
}

1 个答案:

答案 0 :(得分:1)

将此添加到您的单元测试中:

 $this->client->followRedirects(false);

请参阅Symfony docs on Testing。重定向不会自动跟踪,但您可以将其设置为执行此操作。如果您想在测试电子邮件后遵循下一个重定向,则可以调用

$crawler = $client->followRedirect();

如果您想要更改以遵循所有重定向,请致电:

$client->followRedirects();