Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6034011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:34:04+00:00 2026-05-23T05:34:04+00:00

I’ve created a mail wrapper class. I know that there are lots of libraries

  • 0

I’ve created a mail wrapper class. I know that there are lots of libraries to send e-mails but i want to learn TDD… So, I’ve created some tests and i have some code. Now I can set the email address on constructor and validate it… if the email address is wrong, an exception raise up. The email address is the only one required field… I don’t have sets and gets because user will setup all email data on constructor.

Now, i’m going to write the send tests. I don’t know how to start it. How could i test if the values are there (subject, mail body, headers) if i don’t want to have setters and getters? How could I test if an email could be sent?

Real world TDD examples are hard to me. I’ve tried to learn about it, i’ve read lots of things but i cannot test real code.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:34:05+00:00Added an answer on May 23, 2026 at 5:34 am

    Since you linked to the mail function, the call to mail is likely hardcoded into your code. So have a look at

    • http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html

    Install the testhelper extension and mock the call to mail. Then have the mock validate that it got called with the correct values when your wrapper’s send method is called, e.g. define a custom mail function somewhere:

    function mail_mock()
    {
        $allThatWasPassedToTheFunction = func_get_args();
        return $allThatWasPassedToTheFunction;
    }
    

    Then in your send() test, do something like

    public function testSendReceivesExpectedValues()
    {
        // replace hardcoded call to mail() with mock function
        rename_function('mail', 'mail_orig');
        rename_function('mail_mock', 'mail');
    
        // use the wrapper
        $testClass = new MailWrapper('foo@example.com');
        $results = $testClass->send();
    
        // assert the result
        $this->assertSame('foo@example.com', $results[0]);
        $this->assertSame('Default Title', $results[1]);
        $this->assertSame('Default Message', $results[2]);
    }
    

    Note that the above assumes your send function will return the result of the mail() call.

    In general, you will always try to substitute an external subsystem, like sendmail or a database or the filesystem with a Mock or a Stub, so you can concentrate on testing your own code in isolation of the external subsystem. You dont need to test that mail actually works.

    Also see http://www.phpunit.de/manual/3.6/en/test-doubles.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.