我正在测试一个注册案例,其中用户填写表单,然后提交 - 并在登录时获取包含其凭据的电子邮件并重定向到他的个人空间。
所以我需要检查发送的电子邮件以验证凭据,我设置了这样的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并添加了一个调用以从测试用例启用Profiler(见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());
}
将collect参数(来自config)设置为true,我得到:
bool(true)
bool(true)
将collect参数(来自config)设置为false,我得到:
bool(true)
bool(false)
分析器似乎已经设置但我无法理解,我在这里缺少什么?
答案 0 :(得分:0)
我解决了,表单提交导致了意外的重定向。
所以我把测试分成了两个子栏目:
测试表单值:
选择表单 - >填写字段 - >提交 - >按照重定向 - >获取并分析结果
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());
}
测试邮件内容:
发送包含表单数据的帖子请求 - >启用分析器 - >获取并分析邮件内容
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());
}