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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:54:59+00:00 2026-05-30T15:54:59+00:00

In phpunit we can specify the method was called with particular ->with($this->equalTo(‘foobar’)) or any

  • 0

In phpunit we can specify the method was called with particular

->with($this->equalTo('foobar'))

or any

->with($this->anything())

parameter.

But is there a way to specify that the method has been called without parameters at all?

This is the test I expect to fail:

public function testZ()
{
    $a = $this->getMock('q');

    $a->expects($this->once())
        ->method('z')
        ->with(); // <--- what constraint to specify here?

    $a->z(1);
}

UPD:

The question has theoretical nature, so I have no any real life example. Some case it could be useful I can think of right now is:

public function testMe($object)
{
    $object->foo();
}

And let’s assume that testMe should (by design and by requirements) always call the method without parameters (assuming foo() has default ones). Because any non-default parameter (more precise: any parameter != to default one, which we don’t know yet and which probably could change independently) in this case causes fatal consequences.

  • 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-30T15:55:00+00:00Added an answer on May 30, 2026 at 3:55 pm

    While rdlowrey is correct that with() doesn’t make provisions for checking for no arguments passed, the problem doesn’t lie with PHPUnit but PHP itself.

    First, if your method doesn’t provide default values, the interpreter will raise a fatal error if you don’t pass any parameters. This is expected and not entirely relevant to the question at hand, but it’s important to state up front.

    Second, if your method does provide default values, calling the method without arguments will cause PHP to alter the call before PHPUnit gets involved to pass the defaults instead. Here’s a simple test that demonstrates PHP inserting itself before PHP can check the parameters. It’s key to realize that the mock class that PHP creates has the same signature as the mocked class–including the defaults.

    class MockTest extends PHPUnit_Framework_TestCase {
            public function test() {
                    $mock = $this->getMock('Foo', array('bar'));
                    $mock->expects($this->once())
                         ->method('bar')
                         ->with()    // does nothing, but it doesn't matter
                         ->will($this->returnArgument(0));
                    self::assertEquals('foobar', $mock->bar());  // PHP inserts 1 and 2
                    // assertion fails because 1 != 'foobar'
            }
    }
    
    class Foo {
            public function bar($x = 1, $y = 2) {
                    return $x + $y;
            }
    }
    

    This means you can verify that either nothing was passed or the default values were passed, but you cannot be more specific.

    Can you get around this limitation? You can remove default values from arguments when overriding methods, so you should be able to create a subclass and mock it. Is it worth it? My initial gut reaction is that this is a huge code smell. Either your design or your tests are doing the Wrong Thing(tm).

    If you can provide a real-world, concrete example where you actually need to do this kind of test, it’s worth spending some time pondering a solution. Until then, I’m satisfied with the purely academic answer of “don’t do that.” 🙂

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

Sidebar

Related Questions

PHPunit can create a multitude of result reports, but I am having trouble figuring
I read the other PHPUnit installation questions but haven't had any success. What is
I have Selenium IDE/RC, and PHPUnit 3.5 (PEAR) installed, but I can't seem to
I'd like to integrate PHPUnit to my framework. By this, I mean that I
The current version of PHPUnit is 3.6, but until I update my code I
After a fresh symfony2 install i can run phpunit -c app/ and phpunit tests
How can I get NetBeans to provide code-completion for PHPUnit classes and methods? I
I can't seem to correctly do this, the error message of the exception just
How can I make PHPUnit respect __autoload functions? For example, I have these three
Using PHPUnit, I wonder if we can mock an object to test if a

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.