2013-06-24 22 views
7

Tôi đang cố gắng để kiểm tra email trong thử nghiệm chức năng ...Làm thế nào để kiểm tra email trong thử nghiệm chức năng (Symfony2)

mã nguồn của tôi là giống như example of the cookbook,

bộ điều khiển:

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(...); 
} 

Và kiểm tra:

// 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() 
     ); 
    } 
} 

tuy nhiên tôi đã nhận lỗi này:

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

vấn đề này xuất phát từ dòng này:

bất kỳ ý tưởng?

+2

là câu trả lời của tôi hữu ích không? nếu có xin vui lòng upvote/chấp nhận nếu không xin vui lòng bình luận nếu một cái gì đó bị thiếu hoặc không làm việc :-) – nifr

+0

Tôi không thể kiểm tra trong khi ... nhưng đừng lo lắng, tôi giữ giải pháp của bạn và sẽ thử nó ra vào tuần tới;) – Ousmane

Trả lời

7

Trường hợp ngoại lệ bị ném vì getProfile() trả về false nếu profiler không được bật. xem here.

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

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

Hơn nữa, enableProfiler() chỉ cho phép profiler nếu đăng ký với dịch vụ-container được bật. xem here.

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

Bây giờ bạn phải đảm bảo rằng trình lược tả được kích hoạt trong môi trường thử nghiệm. (Bình thường nên là default setting)

config_test.yml

framework: 
    profiler: 
     enabled: true 

Bạn có thể thêm một cái gì đó như thế này để thử nghiệm của bạn:

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

Xin chào @nifr, nó thực sự hữu ích cho tôi. thnx – kuldipem

+1

Lưu ý cho người đọc: bạn phải vô hiệu hóa chuyển hướng nếu bạn muốn sử dụng profiler sau khi đã gửi biểu mẫu. '$ client-> followRedirects (sai); $ client-> enableProfiler(); $ client-> submit ($ form);/* sử dụng profiler */if ($ profile = $ client-> getProfile()) {...}/* mở trang đích */$ crawler = $ client-> request ('GET', $ client-> getResponse() - > headers-> get ('location')); ' –

Các vấn đề liên quan