views:

38

answers:

2

Hello,

is there a way to test with PHPUnit (or maybe other testing framework for PHP) if mail is sent correctly? I have to test a code which uses PHP function mail() . With custom mailer class i could always make a mock, but for mail() ... ? Maybe there is some plugin which is capable to use IMAP and verify if mail is received? (and it should be OS-agnostic if it is possible...)

+1  A: 
Sarfraz
Right on, because the only failure you can reliably detect is the local delivery failure. Checking if it has been received in an inbox can fail for myriad reasons.
Vinko Vrsalovic
it is not for check if mail was sent or not. it's for unit testing - i need to know not only if mail was sent (no problem as i pass return of mail() ), but also if it was well formed (headers, content, attachements etc) and delivered to proper account)
ts
@ts I'm not really sure whether this is suitable for unit testing - they are so many things that could go wrong depending on the server setup. I'd say this calls for a mock.
Pekka
i agree, but mocking native php functions is quite beyond me now ;)
ts
@ts: Also as @Artefacto pointed out, `mail` is already thoroughly checked. You may be interested in SwiftMailer for example then.
Sarfraz
+1  A: 

The solution here would be to wrap mail in a class that could be mocked and use that instead.

I don't see the point in testing mail() itself, I'm sure it's been thoroughly tested already.

Artefacto
my problem is that mail() is only part of larger method
ts
@ts Artefacto's point still stands: The best idea would be to set up a wrapper class that can mock `mail()` in the unit test (and, say, always return `true` unless the recipient E-Mail address is not a valid one.)
Pekka