2013-06-24 9 views
7

Próbuję sprawdzić e-mail w teście funkcjonalnym ...Jak sprawdzić e-mail w teście funkcjonalnym (Symfony2)

Mój kod źródłowy jest taka sama jak example of the cookbook,

kontroler:

public function sendEmailAction($name) 
{ 
    $message = \Swift_Message::newInstance() 
     ->setSubject('Hello Email') 
     ->setFrom('[email protected]') 
     ->setTo('[email protected]') 
     ->setBody('You should see me from the profiler!') 
    ; 

    $this->get('mailer')->send($message); 

    return $this->render(...); 
} 

a test:

// src/Acme/DemoBundle/Tests/Controller/MailControllerTest.php 
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 

class MailControllerTest extends WebTestCase 
{ 
    public function testMailIsSentAndContentIsOk() 
    { 
     $client = static::createClient(); 

     // Enable the profiler for the next request (it does nothing if the profiler is not available) 
     $client->enableProfiler(); 

     $crawler = $client->request('POST', '/path/to/above/action'); 

     $mailCollector = $client->getProfile()->getCollector('swiftmailer'); 

     // Check that an e-mail was sent 
     $this->assertEquals(1, $mailCollector->getMessageCount()); 

     $collectedMessages = $mailCollector->getMessages(); 
     $message = $collectedMessages[0]; 

     // Asserting e-mail data 
     $this->assertInstanceOf('Swift_Message', $message); 
     $this->assertEquals('Hello Email', $message->getSubject()); 
     $this->assertEquals('[email protected]', key($message->getFrom())); 
     $this->assertEquals('[email protected]', key($message->getTo())); 
     $this->assertEquals(
      'You should see me from the profiler!', 
      $message->getBody() 
     ); 
    } 
} 

jednak mam ten błąd:

PHP Fatal error: Call to a member function getCollector() on a non-object

Problem pochodzi z tej linii:

jakiś pomysł?

+2

była moja odpowiedź była pomocna? jeśli tak, proszę przegłosować/zaakceptować inaczej, proszę o komentarz, jeśli czegoś brakuje lub nie działa :-) – nifr

+0

Nie jestem w stanie sprawdzić na chwilę ... ale nie martw się, zachowam twoje rozwiązanie i wypróbuję je w przyszłym tygodniu;) – Ousmane

Odpowiedz

7

Wyjątek jest zgłaszany, ponieważ getProfile() zwraca wartość false, jeśli profiler nie jest włączony. patrz here.

public function getProfile() 
{ 
    if (!$this->kernel->getContainer()->has('profiler')) { 
     return false; 
    } 

    return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response); 
} 

Ponadto enableProfiler() tylko włącza profiler jeśli jest zarejestrowana usługa-kontenera aka włączone. patrz here.

public function enableProfiler() 
{ 
    if ($this->kernel->getContainer()->has('profiler')) { 
     $this->profiler = true; 
    } 
} 

Teraz musisz upewnić się, że profiler jest włączony w środowisku testowym. (Powinien być zwykle default setting)

config_test.yml

framework: 
    profiler: 
     enabled: true 

Można dodać coś takiego testu:

$this->assertEquals($this->kernel->getContainer()->has('profiler'), true); 
+0

Witam @ Frafr, to jest naprawdę pomocne dla mnie. thnx – kuldipem

+1

Uwaga dla czytelników: musisz wyłączyć przekierowania, jeśli chcesz użyć profilera po przesłaniu formularza. '$ client-> followRedirects (false); $ client-> enableProfiler(); $ client-> submit ($ form);/* użyj profilera */if ($ profile = $ client-> getProfile()) {...}/* otwórz stronę docelową */$ crawler = $ client-> request ('GET', $ client-> getResponse() - > headers-> get ('location')); ' –

Powiązane problemy