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

  • Home
  • SEARCH
  • 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 7925799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:27:50+00:00 2026-06-03T18:27:50+00:00

My test suite calls accepted in Object A. That function will then call insert

  • 0

My test suite calls accepted in Object A. That function will then call insert for Object B a certain number of times, depending on which test I’m running.

I want to verify that insert is being called the right amount of times in each test. I don’t think I can count it using mock since Object A wouldn’t be hitting the mock within my test.

I saw this question from 2 years ago:
PHPUnit Test How Many Times A Function Is Called

Using a global variable for counting isn’t ideal since I shouldn’t have code in my class that is specifically for a class.

EDIT

It would probably be helpful to note that insert is static. Even if I mock the class and specify I only want to mock that function, it still calls new on the mocked object which is another roadblock I’m facing.

ANSWER
The answer is no. I just want @zerkms to give that answer since he was the one helping me so I can accept it.

I ended up figuring I can use just one object but did hit another roadblock:
Why isn't PHPUnit counting this function as having ran?

  • 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-03T18:27:53+00:00Added an answer on June 3, 2026 at 6:27 pm

    Seems like in this particular case it is impossible.

    But in some specific cases you can mock static methods: http://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html

    class Foo
    {
        public static function doSomething()
        {
            return static::helper();
        }
    
        public static function helper()
        {
            return 'foo';
        }
    }
    

    test:

    public function testQQQ()
    {
        $class = $this->getMockClass(
            'Foo',          /* name of class to mock     */
            array('helper') /* list of methods to mock   */
        );
    
        $class::staticExpects($this->exactly(2))
            ->method('helper')
            ->will($this->returnValue('bar'));
    
        $this->assertEquals(
            'bar',
            $class::doSomething()
        );
    }
    

    Result:

    $ phpunit --filter QQQ
    PHPUnit 3.6.10 by Sebastian Bergmann.
    
    Configuration read from /var/www/.../phpunit.xml
    
    F
    
    Time: 1 second, Memory: 10.75Mb
    
    There was 1 failure:
    
    1) ...::testQQQ
    Expectation failed for method name is equal to <string:helper> when invoked 2 time(s).
    Method was expected to be called 2 times, actually called 1 times.
    
    
    FAILURES!
    Tests: 1, Assertions: 2, Failures: 1.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a test suite DLL written in C# that uses Selenium.This is then
A gem/plugin that I'm using to support my test suite calls some GNU commands
Is there a good test suite or tool set that can automate website navigation
I have a PHPUnit test suite that is currently causing a fatal error due
What's the best practice for making sure that certain ajax calls to certain pages
I have a test suite that needs to delete any existing data in my
If I create a test suite for a development project, should those classes be
When I run the test suite of django I get errors on the auth
When running an applications test suite I want to promote all Perl compile and
I have a test suite to perform smoke tests. I have all my script

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.