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?

2
  • So now you understand why static is evil :-)
    – zerkms
    May 9, 2012 at 22:29
  • I just saw some hating on static methods while research this. Is the correct thing to make an empty object and then call instance method on it?
    – Dave Stein
    May 9, 2012 at 22:32

2 Answers 2

1

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.
9
  • That's the article I was just reading that was bashing static :) When I mock the static function, it's still calling new on my object. Also even if I mock the static function I don't think my other object would have access to that. My other object wouldn't have a reference to $class
    – Dave Stein
    May 9, 2012 at 22:33
  • @Dave Stein: I'm not sure I understand the situation. You said that you need to count of calls of a static method. Here it is
    – zerkms
    May 9, 2012 at 22:34
  • "My other object wouldn't have a reference to $class" --- and it shouldn't have. You set up a mock in test method
    – zerkms
    May 9, 2012 at 22:35
  • I'm not good at explaining things so thanks for baring with me. My test is for Object B. I call a method in Object A which calls static method in Object B. The static method is just populating a table based on information from Object A. So my test is calling Object A's method, which calls B. I then verify that the last row for B is correct. I want to also verify that the method was only called once.
    – Dave Stein
    May 9, 2012 at 22:41
  • @Dave Stein: so the example above looks similar to what you just explained
    – zerkms
    May 9, 2012 at 22:47
0

You could use runkit to redefine the static method on the fly (you probably shouldn't, though). Other than that, you will have to restructure the code. Either use non-static calls and dependency injection (so object A receives object B from an external source, and your test can pass a mock instead) or use a dependency injection container so that the class name is not wired in and your test can create a mock subclass and make class A use it (this is more messy, but needs much less change in your non-test code as you can leave your methods static).

1
  • I heard of runkit in a few posts and it seemed like the thing not to use ( even youre saying not to use it at same time ). I was able to restructure code but am still running into some hurdles that I am asking about here: stackoverflow.com/questions/10541683/…
    – Dave Stein
    May 10, 2012 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.