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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:53:38+00:00 2026-05-19T04:53:38+00:00

I want to mock a method of a class and execute a callback which

  • 0

I want to mock a method of a class and execute a callback which modifies the object given as parameter (using PHP 5.3 with PHPUnit 3.5.5).

Let´s say I have the following class:

class A
{
  function foobar($object) 
  {
    doSomething();
  }
}

And this setup code:

$mock = $this->getMockBuilder('A')->getMock();
$mock->expects($this->any())->method('foobar')->will(
  $this->returnCallback(function($object) {
    $object->property = something;
  }));

For some reason the object does not get modified. On var_dumping $object I see it is the right object. Could it be that the object gets passed by value? How can I configure the mock to receive a reference?

  • 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-19T04:53:38+00:00Added an answer on May 19, 2026 at 4:53 am

    He Alex,

    i talked to Sebastian (the phpunit creator) about that problem and yes: The argument gets cloneed before it is passed to the callback.

    From the top of my head i can’t offer you any workaround but i choose to answer anyway to at least tell you that you are doing nothing wrong and that this is expected behavior.

    To quote Sebastians comment on IRC on why it clones the argument:

    It’s a long-going debate between me, myself, and users of PHPUnit whether or not this is right 😉

    To have something copy/pasteable:

    Assertion 3 in this codesample will fail. (The variable is only changed in the returned object)

    <?php
    class A
    {
        function foobar($o)
        {
            $o->x = mt_rand(5, 100);
        }
    }
    
    class Test extends PHPUnit_Framework_TestCase
    {
        public function testFoo()
        {
            $mock = $this->getMock('A');
            $mock->expects($this->any())
                 ->method('foobar')
                 ->will($this->returnCallback(function($o) { $o->x = 2; return $o; }));
    
            $o = new StdClass;
            $o->x = 1;
    
            $this->assertEquals(1, $o->x);
            $return = $mock->foobar($o);
    
            $this->assertEquals(2, $return->x);
            $this->assertEquals(2, $o->x);
        }
    }
    

    Update:

    Starting with PHPUnit 3.7 the cloning can be turned off. See the last argument off:

    public function getMock(
        $originalClassName, 
        $methods = array(), 
        array $arguments = array(), 
        $mockClassName = '', 
        $callOriginalConstructor = TRUE, 
        $callOriginalClone = TRUE, 
        $callAutoload = TRUE, 
        $cloneArguments = FALSE
    );
    

    It might even be off by default 🙂

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

Sidebar

Related Questions

I want to mock only the GetValue method of the following class, using Moq:
I have a class which has a internal method and i want to mock
I want to mock an instance method for all instances of a class using
I want to mock a particular method of a class, problem i am facing
I'm trying to patch a class method using mock as described in the documentation
I want to mock a method in the same class that I am testing.
I'm using Mockito 1.9.0. I want mock the behaviour for a single method of
I want to mock a method that returns a complex datatyp class aClass {
I want to make a mock installer using NSIS so we can demo what
The class I want to test is my ArticleManager class, specifically the LoadArticle method:

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.