Issues

ZF-10372: Zend_Mail_Transport_Mock

Description

Found the class in there: http://framework.zend.com/svn/framework/…

I think it would make a great addition. :-)

Comments

You have a proposal Zend_Mail_Transport_File in the incubator actualy (see http://framework.zend.com/wiki/pages/…). It is in the same vein, I think. But, I agree it may be easier to have a class that avoids parse for testing.

Not sure I understand the request.

Zend_Mail_Transport_Mock already exists in trunk/tests/Zend/Mail/MailTest.php

What are you proposing?

I'm talking about being able to mock Zend_Mail in my application.

The necessary class to mock Zend_Mail in my app (!) already exists, but it's hidden inside the tests. It would be nice if this code is instead put in library where it belongs.

Have you looked at Zend_Transport_Mail?

I'd like to see a usecase why this should be in the library dir. If a good one is provided however, I'll move it to the library folder in ZF2. Can't do so in 1.11 since it's a BC break / extra functionality and we wont have any other minor releases after 1.11.

My use case is -- bear with me -- testing!

We really do TDD and all that. And when I run my unit tests, I don't want to send any emails, yet part of it require a mail object to be build and used. I want to unit-test though and not involve a mailserver, etc...

Also, this is not a BC break since it doesn't break existing behavior. BC means "backward compatible". This is preserved by adding an additional driver.

I also mentioned Zend_Mail_Transport_File. Wouldn't that do it for you as well? Also, you can just include file that currently holds the mockclass.

Though it still has to be considered new functionality, I'll discuss with the release manager if he can make an exception for this.

Btw, yes I looked into {{Zend_Mail_Transport_Mail}} but I don't even want to generate any emails.

It's not the scope of my test. Instead I just want to execute my code and completely mock sending the email. I found that the class that is hidden in the unit tests for {{Zend_Mail}} does this very well. :)

I saw that you commented also, let me elaborate.

You are of course correct -- I can include it already but I usually don't distribute Zend Framework's test suite to staging and CI.

The problem with using another transport which requires configuration is, that this introduces another potential issues into my test suite. Probably small, but maybe not. The mock transport does exactly what it should do and it's painless.

With {{_File}}, at the bear minimum I need to configure where the files are left, fix it up with permissions and maybe also clean them up?

Our test suite contains 5000+ tests (and a lot more assertions) and I'm trying to keep test specific configuration at a bare minimum. Besides, I also enjoy faster tests which need additional disk i/o.

As someone who uses the _File transport (I copied the code into my own library and namespaced it), I can understand the potential cleanup issue. I have not tried this, but I suppose you could direct the files to /dev/null or something of the sort.

Here is a snippet from my application.ini:

;====================================== ; Mail ;====================================== ; default mail transport settings resources.mail.transport.type = "com.example\core\mail\transport\File" resources.mail.transport.path = BASE_PATH "/tmp/maillog" resources.mail.transport.register = true

The above block is under the common section. I configure the actual SMTP mailer under the production section. This way, mail only goes out via production; nothing else.

I personally like the _File transport because it is great for debugging (if necessary; however, I haven't had the need as of yet).

My unit tests also test that the file was written. I could certainly add a cleanup step (I probably should) but I haven't done this yet.

Having the _File transport as a part of the actual ZF library would be great as I would not have to maintain my own, but it works well enough in the interim.

If anyone would like a copy of what I use, just let me know.

{quote}Having the _File transport as a part of the actual ZF library would be great as I would not have to maintain my own, but it works well enough in the interim.{quote}

It already is. Or do you mean the ZF2 branch/repo? I expect to get to that before the end of this year (probably earlier).

Good point. Yes, I was referring to the 1.x branch. I "borrowed" the code I found from the ZF2 branch :)

I'm not sure I can follow. You are aware zf1.11 does also have Zend_Mail_Transport_File (though it's only prefixed, not namespaced)?

@Dolf:

Actually, I did not know that. I am still on 1.10.x. Thanks for the hint.

In your own unit tests, if you want a null transport, can't you use a PHPUnit mock for this? Something like


$transport = $this->getMock('Zend_Mail_Transport_Smtp'); // mocks all methods
$mail = new Zend_Mail;
$mail->setBodyText('body')
     ->setFrom('from@domain.com', 'my name')
     ->send($transport);

Or, maybe a class named Zend_Mail_Transport_Null in the library would make more sense than Zend_Mail_Transport_Mock, if what you're looking to do is have all calls to $mail->send() succeed but not actually send the mail within a dev environment.

For now I've moved the MockObject to the library dir rather than depending on the test directory.

Marc, your method looks pretty interesting. However, usually you'll want to be testing for the outcome of a method, rather than its input? Please convince me.

Dolf, you're right. My example was limited to needing a null transport when testing other higher level items and not wanting them to send mail :) Wouldn't work for examining the output.

Resolved in ZF2