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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:40:00+00:00 2026-06-14T08:40:00+00:00

Is there a way to create a mock class, as opposed to a mock

  • 0

Is there a way to create a mock class, as opposed to a mock object, with phpunit? I’m looking for a way to do dependency injection without having to explicitly pass every object a class might need to work with in the constructor (or wherever). Something that will return “true” for all of these cases:

public function testAAAA()
{
  $foo = $this->getMock('foo', array('bar'));
  var_dump(class_exists('foo', false));
  var_dump(method_exists('foo', 'bar'));
  var_dump(method_exists($foo, 'bar'));
}

This prints:

bool(true)
bool(false)
bool(true)

indicating that while it did successfully create a fake ‘foo’ class it did not bind a ‘bar’ method to it.

I am using phpunit 3.7.5.

  • 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-14T08:40:02+00:00Added an answer on June 14, 2026 at 8:40 am

    I suspect that you don’t actually want to do this (as you can disable constructors and so on with PHPUnit’s mockbuilder, see the docs ), but assuming you do want or need to, this should do the trick:

    $foo = $this->getMockBuilder('nonexistant')
            ->setMockClassName('foo')
            ->setMethods(array('bar'))
            ->getMock();
    
        var_dump(class_exists('foo', false));
        var_dump(method_exists('foo', 'bar'));
        var_dump(method_exists($foo, 'bar'));
    
        $cls = new ReflectionClass('foo');
        var_dump($cls->hasMethod('bar'));
    

    I’m honestly not sure about the specifics of why you need to specify different names (nonexistant and foo) above, but it seems to have to do with PHPUnit’s behavior when the class being mocked doesn’t exist yet, and having setMockClassName generate a class extending that class. Or Something. It’s probably effectively working around a bug/edge-case — this is odd usage of the library. You should be able to do the same thing through the getMock function alone, it’s just uglier.

    As an aside, it sounds like you should probably get familiar with php’s reflection capabilities. It’s not the most powerful reflection library out there, but it’s pretty nice. I’ve used it to generate meta-information about required and optional fields for a class based on their constructor arguments and properties for a “model” library, where that meta-information is used to generate forms that accept the correct types of values. That is, generate typed forms without instances of the class the form is for, and without hand-writing a stupid amount of code — it’s about 100 lines in all for the entire feature. Obviously I don’t know what you’re trying to do, but from the small amount of info in your post, I’d guess it’s closer to that type of meta-thing than not.

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

Sidebar

Related Questions

Is there a way to create a mock object based on an already existing
Is there a way to mock object construction using JMock in Java? For example,
I'm trying to create a Mockito mock object of a class with some rather
Is there a way to create a callback function that fires when a list
Is there a way to create a layout like the notification bar (the sliding
Is there any way to create a dynamic Pinterest button? I'm trying to add
Is there a way to create a StringBuilder from a byte[] ? I want
Is there a way to create a NuGet package where when the package is
Is there any way to create breakpoints in clojurescript? Either in the repl or
Is there a way to create a temporary table using the RMySQL package? If

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.