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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:19:02+00:00 2026-06-05T01:19:02+00:00

Almost all of the phpUnit asserts are three parameters: Expected result Actual value Optional

  • 0

Almost all of the phpUnit asserts are three parameters:

  1. Expected result
  2. Actual value
  3. Optional message to print on failure

(Some, such as assertTrue(), have an implicit expected result, so just have two parameters.)

But, what to do if expected result is not so clean? It may be optional parameters, or it may simply be that there is more to test than one value. As one concrete example, I have assertTimestamp($expected_time,$actual_time,$tolerance=0,$msg='') where tolerance allows for a bit of clock drift. (I also use it to test a created timestamp in a database, to assert that the record was created in the last 30 seconds, i.e. by a previous function in the current unit test run.)

Does that look reasonable? I.e. always push $msg to the end, and keep actual as the second parameter. assertTag has a isHTML flag, which comes last, after $msg.

As a more complex example I’ve a function that takes a json string for the $actual parameter. It runs json_decode, extracts a few values, and checks each of them. I currently have something like this:

function assertJsonPersonFromDBResponse($name,$gender,$age,$actual,$expectSomething=false,$msg='')

Would you do it differently? I am now wondering if it might be better to squeeze all expected parameters (including optional flags) into the first parameter, which would be an associative array:

function assertJsonPersonFromDBResponse($expected,$actual,$msg='')
...
$this->assertJsonPersonFromDBResponse(array('name'=>'Darren','age'=>21,
    'gender'=>'M','expectSomething'=>true),$s);

That feels cleaner. But is there an existing convention for these kind of situations?

  • 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-06-05T01:19:04+00:00Added an answer on June 5, 2026 at 1:19 am

    The pattern I see throught the phpunit code base is:

    [$expected (if needed)], $actual, $message = '', [other optional paramters]
    

    For exaples take a look at the Framework/Assert.php file

    public static function assertEquals(
        $expected, 
        $actual, 
        $message = '', 
        $delta = 0, 
        $maxDepth = 10, 
        $canonicalize = FALSE, 
        $ignoreCase = FALSE
    )
    

    Or to be more precisce since $actual can be composed of more than one variable (class & attribute for example):

    [$allExpectedParamsIfNeeded], $allActualParams, $message='', [$allOtherSwitches]
    

    For example:

    public static function assertAttributeInternalType(
        $expected,
        $attributeName,
        $classOrObject,
        $message = ''
    )
    

    or

    public static function assertSelectRegExp(
        $selector,
        $pattern,
        $count,
        $actual,
        $message = '',
        $isHtml = TRUE
    )
    

    General advice for assertions:

    Try to minimize the amount of parameters and use more assert functions if possible:

    $this->assertStuff($a, $b, true, false, 'Oh dear', 6, true);
    

    is just not readable and confusing to me.

    Having

    $this->assertStuffForSpecificCaseWithSubcaseSix($a, $b, 'Oh dear'); 
    

    at least is "inline documentation" at every place it’s used.

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

Sidebar

Related Questions

On almost all of my vb files, at some point I have to enter
Almost all the applications I worked on involve some look-up values. For example, a
Almost all compiler will return a line number along with error message. I am
Almost all of the examples in the jQuery tutorials that I've read, usually use
Almost all of my JUnit tests are written with the following signature: public void
In almost all of my projects I have few files that are project wide
I noticed that almost all email messages I get do not embed images, but
I have tried almost all the solutions from SO but no success :(. I
I have carried the method here on almost all of the areas where I
I've been using px for almost all the measurement related styling, but I wanted

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.