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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:37:47+00:00 2026-06-17T06:37:47+00:00

If I have a class A that interacts with class B then in my

  • 0

If I have a class A that interacts with class B then in my tests I want to mock class B so that I can test class A in isolation.

This is easy to do in PHPUnit with "getMock('classname')".

My problem currently is: If class A uses multiple instances of class B I can’t simulate this with "getMock('B')" because it appears that "getMock" will not return multiple instances if called multiple times but always the same mock of class B.

Following Example:

<?php

class A()
{
    private class_b_1;
    private class_b_2;


    public function setClassB1(B $class_b)
    {
        $this->class_b_1 = $class_b;
    }

    public function setClassB2(B $class_b)
    {
        $this->class_b_2 = $class_b;
    }   
}

And in my tests:

$mock_one_of_class_b = $this -> getMock('B');
$mock_two_of_class_b = $this -> getMock('B');

Then $mock_one_of_class_b is the same object as $mock_two_of_class_b.

How can I mock multiple instances of a class with PHPUnit?

Thanks in advance!

  • 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-17T06:37:48+00:00Added an answer on June 17, 2026 at 6:37 am

    Actually the getMock method creates different instances of mocked class. Take a look on this example:

    class Foo
    {
        protected $value;
    
        public function setValue($value)
        {
            $this->value = $value;
        }
    
        public function getValue()
        {
            return $this->value;
        }
    }
    

    Now we create test for it:

    class FooTest extends \PHPUnit_Framework_TestCase 
    {
        public function testFoo()
        {
            $a = $this->getMock('Foo', array('someNonExistingMethod'));
            $b = $this->getMock('Foo', array('someNonExistingMethod'));
    
            $a->setValue(1);
            $b->setValue(234);
    
            $this->assertEquals(1, $a->getValue(), 'This test will fail if "a" and "b" objects are the same');
        }
    }
    

    Pay attention on the second argument in getMock method. In that argument you tell phpunit which methods will be mocked. If you don’t pass any argument at all – then phpunit assume you want mock all methods from the object. So, if you pass array('someNonExistingMethod') then there won’t be any mocked method (and “real” methods will be called on invocations).

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

Sidebar

Related Questions

I have a php class that interacts with mysql database and then fetches an
I have a class that instantiates two classes which implement interfaces. I want one
I have class that looks like this: class A { public: class variables_map vm
In my Java course I have to create a GUI class that interacts with
I have a url that looks like this: http://localhost/store/mens/category/t-shirts/item/a-t-shirt I have a class called
How can I enforce that my test class covers a particular interface? Also is
If we have a class that inherits from multiple interfaces, and the interfaces have
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that represents users. Users are divided into two groups with different
I have class grades that I need to check if a specific grade is

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.