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 3226996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:31:22+00:00 2026-05-17T16:31:22+00:00

I am trying to write tests for a date helper class. I am wondering

  • 0

I am trying to write tests for a date helper class. I am wondering what is best practice for this.
Here is an example in PHP.

    public static function get_date($offset = 0){
        $date = mktime(0, 0, 0, date('m')  , date('d') + $offset, date('Y'));
        return array(
            'day'   => date('D',$date),
            'month' => date('M',$date),
            'year'  => date('Y',$date)
        );
    }

    public static function today(){
        return self::get_date();
    }

    public static function tomorrow(){
        return self::get_date(1);
    }

    public static function yesterday(){
        return self::get_date(-1);
    }

So what I am looking for is examples of tests that could test these functions or a new way to write these functions so they are intuitively testable.

I have found examples in Java but they seem pretty inelegant and I really don’t know Java, unless you count Actionscript 3 as Java 😉

Solutions in Javascript or Ruby would also be super helpful as examples but my class is written in PHP so that would be ideal.

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-17T16:31:22+00:00Added an answer on May 17, 2026 at 4:31 pm

    I usually do something like this:

    <?php
    
    class Date
    {
        protected $date;
    
        public function __construct($date = null)
        {
            $this->date = $date ? $date : time();
        }
    
        public function today()
        {
            // to something with $this->date
        }
    }
    
    
    class DateTest extends PHPUnit_Framework_TestCase
    {
        public function testToday()
        {
            $date = new Date(strtotime('2010-10-17 00:00:00'));
            $this->assertEquals($expectedValue, $date->today());
        }
    }
    

    It follows the same principle that date() does, which is to receive a second optional argument, the reference time.

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

Sidebar

Related Questions

I am trying to write some unit tests and I was wondering if there
We've been trying write unit tests for a worker class written in C#, which
I'm trying to write tests for some legacy code with PHPUnit 3.4.9, but it
I am trying to write integration tests. Therefore I need the start methods of
I am trying to write contract tests for some widely used interfaces: Along the
I'm trying to write unit tests in MSTest and I've created two TestClasses. When
I am trying to write unit tests for a bit of code involving Events.
I am currently trying to write functional tests for a charging form which gets
Im trying to write some unit tests for a couple of merthods that are
I'm trying to write some Mspec tests against some EF4 objects. However they are

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.