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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:29:03+00:00 2026-05-24T18:29:03+00:00

PHPUnit’s getMock($classname, $mockmethods) creates a new object based on the given class name and

  • 0

PHPUnit’s getMock($classname, $mockmethods) creates a new object based on the given class name and lets me change/test the behavior of the methods I specified.

I long for something different; it’s changing the behavior of methods of an existing object – without constructing a new object.

Is that possible? If yes, how?


When thinking about the problem I came to the conclusion that it would be possible by serializing the object, changing the serialized string to let the object be instance of a new class that extends the old class plus the mocked methods.
I’d like some code for that – or maybe there is such code already somewhere.


While it would certainly be possible to create the to-be-mocked object again, it’s just too complicated to do it in my test. Thus I don’t want to do that if I don’t really really really have to. It’s a TYPO3 TSFE instance, and setting that up once in the bootstrapping process is hard enough already.

  • 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-24T18:29:06+00:00Added an answer on May 24, 2026 at 6:29 pm

    Let me start of by saying: Welcome to the dark side of unit testing.

    Meaning: You usually don’t want to do this but as you explained you have what seems to be a valid use case.

    runkit

    What you can do quite easily, well not trivial but easier than changing your application architecture, is to change class behavior on the fly by using runkit

    runkit_method_rename(
        get_class($object), 'methodToMock', 'methodToMock_old'
    );
    runkit_method_add(
        get_class($object), 'methodToMock', '$param1, $param2', 'return 7;'
    );
    

    runkit::method_add

    and after the test to a method_remove and the rename again. I don’t know of any framework / component that helps you with that but it’s not that much to implement on your own in a UglyTestsBaseTest extends PHPUnit_Framework_TestCase.

    Well…

    If all you have access to is a reference to that object (as in: The $x in $x = new Foo();) i don’t know of any way to say: $x, you are now of type SomethingElse and all other variables pointing to that object should change too.

    I’m going to assume you already know things like testing your privates but it doesn’t help you because you don’t have control over the objects life cycle.

    The php test helpers extension

    Note: the Test-Helper extension is superseded by https://github.com/krakjoe/uopz

    What also might help you out here is: Stubbing Hard-Coded Dependencies using the php-test-helpers extension that allows you do to things like Intercepting Object Creation.

    That means while your application calls $x = new Something(); you can hack PHP to make it so that $x then contains an instance of YourSpecialCraftedSomething.

    You might create that classing using the PHPUnit Mocking API or write it yourself.


    As far as i know those are your options. If it’s worth going there (or just writing integration / selenium tests for that project) is something you have to figure out on your own as it heavily depends on your circumstances.

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

Sidebar

Related Questions

With PHPUnit I can successfully test if a specific call to a class properly
I am using PHPUnit to test insertion of objects via my storage object. Each
I've got a PHPUnit test that tests a class called HelpTokenizerTest. This class implements
I have a PHPUnit test case class (consisting of some test functions). I would
I've got a PHPUnit mock object that returns 'return value' no matter what its
I have a series of PHPUnit tests to validate a class, Class A. I
I am getting my head around PHPUnit, and trying to build a test case
I'm starting to try and test my Doctrine objects with PHPUnit, and would like
I am trying to work out how to configure PHPUnit to use the test
Is there a simple Web interface to running PHPUnit test suites? i.e. a PHP

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.