ZF-10372: Zend_Mail_Transport_Mock

Issue Type: Improvement Created: 2010-08-23T14:10:06.000+0000 Last Updated: 2010-12-04T14:18:27.000+0000 Status: Resolved Fix version(s): - Next Major Release ()

Reporter: Till Klampaeckel (till) Assignee: Dolf Schimmel (Freeaqingme) (freak) Tags: - Zend_Mail

Related issues: Attachments:


Found the class in there:…

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


Posted by Benoît Durand (intiilapa) on 2010-08-24T10:11:06.000+0000

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

Posted by Marc Hodgins (mjh_ca) on 2010-11-06T03:13:34.000+0000

Not sure I understand the request.

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

What are you proposing?

Posted by Till Klampaeckel (till) on 2010-11-06T03:23:09.000+0000

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.

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2010-11-06T07:34:51.000+0000

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.

Posted by Till Klampaeckel (till) on 2010-11-06T12:01:35.000+0000

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.

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2010-11-06T12:09:44.000+0000

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.

Posted by Till Klampaeckel (till) on 2010-11-06T12:24:56.000+0000

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. :)

Posted by Till Klampaeckel (till) on 2010-11-06T12:30:36.000+0000

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.

Posted by Wil Moore III (wilmoore) (wilmoore) on 2010-11-06T12:51:31.000+0000

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.

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2010-11-06T12:58:16.000+0000

{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).

Posted by Wil Moore III (wilmoore) (wilmoore) on 2010-11-06T13:02:37.000+0000

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

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2010-11-06T13:39:22.000+0000

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)?

Posted by Wil Moore III (wilmoore) (wilmoore) on 2010-11-06T13:45:39.000+0000


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

Posted by Marc Hodgins (mjh_ca) on 2010-11-06T16:00:48.000+0000

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

<pre class="highlight">
$transport = $this->getMock('Zend_Mail_Transport_Smtp'); // mocks all methods
$mail = new Zend_Mail;
     ->setFrom('', 'my name')

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.

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2010-12-02T21:23:16.000+0000

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.

Posted by Marc Hodgins (mjh_ca) on 2010-12-02T22:06:07.000+0000

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.

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2010-12-04T14:18:27.000+0000

Resolved in ZF2

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.