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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:18:50+00:00 2026-05-22T02:18:50+00:00

Is there any way to define different mock-expects for different input arguments? For example,

  • 0

Is there any way to define different mock-expects for different input arguments? For example, I have database layer class called DB. This class has method called Query(string $query), that method takes an SQL query string on input. Can I create mock for this class (DB) and set different return values for different Query method calls that depends on input query string?

  • 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-22T02:18:51+00:00Added an answer on May 22, 2026 at 2:18 am

    The PHPUnit Mocking library (by default) determines whether an expectation matches based solely on the matcher passed to expects parameter and the constraint passed to method. Because of this, two expect calls that only differ in the arguments passed to with will fail because both will match but only one will verify as having the expected behavior. See the reproduction case after the actual working example.


    For you problem you need to use ->at() or ->will($this->returnCallback( as outlined in another question on the subject.

    Example:

    <?php
    
    class DB {
        public function Query($sSql) {
            return "";
        }
    }
    
    class fooTest extends PHPUnit_Framework_TestCase {
    
    
        public function testMock() {
    
            $mock = $this->getMock('DB', array('Query'));
    
            $mock
                ->expects($this->exactly(2))
                ->method('Query')
                ->with($this->logicalOr(
                     $this->equalTo('select * from roles'),
                     $this->equalTo('select * from users')
                 ))
                ->will($this->returnCallback(array($this, 'myCallback')));
    
            var_dump($mock->Query("select * from users"));
            var_dump($mock->Query("select * from roles"));
        }
    
        public function myCallback($foo) {
            return "Called back: $foo";
        }
    }
    

    Reproduces:

    phpunit foo.php
    PHPUnit 3.5.13 by Sebastian Bergmann.
    
    string(32) "Called back: select * from users"
    string(32) "Called back: select * from roles"
    .
    
    Time: 0 seconds, Memory: 4.25Mb
    
    OK (1 test, 1 assertion)
    


    Reproduce why two ->with() calls don’t work:

    <?php
    
    class DB {
        public function Query($sSql) {
            return "";
        }
    }
    
    class fooTest extends PHPUnit_Framework_TestCase {
    
    
        public function testMock() {
    
            $mock = $this->getMock('DB', array('Query'));
            $mock
                ->expects($this->once())
                ->method('Query')
                ->with($this->equalTo('select * from users'))
                ->will($this->returnValue(array('fred', 'wilma', 'barney')));
    
            $mock
                ->expects($this->once())
                ->method('Query')
                ->with($this->equalTo('select * from roles'))
                ->will($this->returnValue(array('admin', 'user')));
    
            var_dump($mock->Query("select * from users"));
            var_dump($mock->Query("select * from roles"));
        }
    
    }
    

    Results in

     phpunit foo.php
    PHPUnit 3.5.13 by Sebastian Bergmann.
    
    F
    
    Time: 0 seconds, Memory: 4.25Mb
    
    There was 1 failure:
    
    1) fooTest::testMock
    Failed asserting that two strings are equal.
    --- Expected
    +++ Actual
    @@ @@
    -select * from roles
    +select * from users
    
    /home/.../foo.php:27
    
    FAILURES!
    Tests: 1, Assertions: 0, Failures: 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to define an IDE Layout in visual studio 2008? Lets
Is there any way to define a variable that can be used in multiple
Hi All is there any way to locally define a variable in a function
Is there any syntactical way in jQuery to define multiple CSS attributes without stringing
is there any way how to execute validation directives saved in database ? See,
Is there any way to make a class instance return a value which is
Is there any way to make private variables (those defined in the constructor), available
Is there any way of defining macros (like tex macros o latex defines) in
If yes is there any way to access a var defined in another PHP
In a large Application is there any way to distinguish user-defined classes with built-in

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.