Symfony2 无法从测试中启用探查器

Symfony2 cannot enable profiler from test

我正在测试一个注册案例,用户填写表格,然后提交并收到一封包含他的凭据的电子邮件,同时被登录并重定向到他的个人 space。

所以我需要检查发送的电子邮件以验证凭据,我这样设置 config_test.yml

framework:
    profiler:
        enabled: true
        collect: true
    test: ~
    session:
        storage_id: session.storage.mock_file        

web_profiler:
    toolbar: false
    intercept_redirects: true

一切正常我成功恢复了电子邮件内容,但我想在我的其他测试用例上禁用 profiller 集合,所以我将 "collect: " 参数设置为 false 并添加了一个调用以从测试用例(参见 http://symfony.com/doc/current/cookbook/testing/profiling.html)但它不起作用。

我的代码片段:

public function testRegistration()
{
    $client = static::createClient();
    $client->enableProfiler();

    $crawler = $client->request('GET', '/registration');
    $this->assertTrue($client->getResponse()->isSuccessful());

    $boutonForm = $crawler->selectButton('InscriptionParticulier_envoyer');
        $formulaire = $boutonForm->form(array(
            'InscriptionParticulier[prenom]' => 'Hermes',
            'InscriptionParticulier[nom]' => 'Conrad',
    ));
    $client->submit($formulaire);

    var_dump((boolean)$client->getKernel()->getContainer()->has('profiler'));
    var_dump((boolean)$client->getProfile());
}

将收集参数(来自配置)设置为 true 我得到:

bool(true)
bool(true)

将收集参数(来自配置)设置为 false 我得到:

bool(true)
bool(false)

分析器似乎已设置,但我无法获取它,我在这里缺少什么?

我解决了,表单提交导致意外重定向。

所以我将测试分为两个子任务:

表格值测试:

Select 表单 -> 填写字段 -> 提交 -> 按照重定向 -> 获取并分析结果

public function testInscriptionParticulierFormOk()
{
    $client = static::createClient();

    $crawler = $client->request('GET', '/compte/inscription/particulier');
    $this->assertTrue($client->getResponse()->isSuccessful());

    $boutonForm = $crawler->selectButton('InscriptionParticulier_envoyer');
    $formulaire = $boutonForm->form(array(
        'InscriptionParticulier[prenom]' => 'Philip',
        'InscriptionParticulier[nom]' => 'J. Fry',
        'InscriptionParticulier[email][first]' => 'philip@planetexpress.com',
        'InscriptionParticulier[email][second]' => 'philip@planetexpress.com',
        'InscriptionParticulier[captcha]' => '12345',
    ));
    $client->submit($formulaire);

    //Enable redirects
    $client->followRedirect();

    //Test result on redirected page
    $this->assertRegExp('/Bienvenue sur le site, accueil/', $client->getResponse()->getContent());        
}

测试邮件内容:

发送带有表单数据的 post 请求 -> 启用分析器 -> 获取并分析邮件内容

public function testInscriptionParticulierFormEmail()
{
    $client = static::createClient();
    $client->enableProfiler();

    $csrfToken = $client->getContainer()->get('form.csrf_provider')->generateCsrfToken('InscriptionParticulier');

    $formData = 
        array('InscriptionParticulier' => array(
            '_token' => $csrfToken,
            'prenom' => 'Amy', 
            'nom' => 'Wong',
            'email' => array(
                'first' => 'amy@planetexpress.com', 
                'second' => 'amy@planetexpress.com'
            ),
            'captcha' => '12345'
        )
    );

    $client->request('POST', '/compte/inscription/particulier', $formData);

    //Get email data
    $mailCollector = $client->getProfile()->getCollector('swiftmailer');

    //Email sent?
    $this->assertEquals(1, $mailCollector->getMessageCount());

    //Get some messages
    $collectedMessages = $mailCollector->getMessages();
    $message = $collectedMessages[0];

    //Check the mail content
    $this->assertInstanceOf('Swift_Message', $message);
    $this->assertEquals('Bienvenue sur le site !', $message->getSubject());
    $this->assertEquals('xxx@xxx.com', key($message->getTo()));
    //Check if the mail have the credentials
    $this->assertRegExp('/Identifiant : [a-zA-Z0-9\.-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z0-9\.-]{2,5}/', $message->getBody());
    $this->assertRegExp('/Mot de passe : [a-zA-Z0-9]+/', $message->getBody());
}