Strict Standards: Redefining already defined constructor for class reCAPTCHA in /home/phpntips/www/wp-content/plugins/wp-recaptcha/recaptcha.php on line 17

Strict Standards: Redefining already defined constructor for class WPPlugin in /home/phpntips/www/wp-content/plugins/wp-recaptcha/wp-plugin.php on line 28

Strict Standards: Redefining already defined constructor for class MailHide in /home/phpntips/www/wp-content/plugins/wp-recaptcha/mailhide.php on line 15
PHPUnit: Assert/mock a method for consecutive calls | PHP and tipsPHP and tips

PHPUnit: Assert/mock a method for consecutive calls

I’m testing a class that is calling a method twice and with different parameters. I want to assert that this method (addSourceLinkAndUpdateCounts) is called twice and with different argument for each call.

The matcher to use is the “InvokedAtIndex”. See PHPUnit 3.6 documentation

Following, an example (the method addSourceLinkAndUpdateCounts is mocked and I assert that is called twice. The first time with arguments (http://www.link1.com, 12), and the second time with arguments (http://www.link2.com, 12)

// assert save function is called twice and with the two args
        $this->object
             ->expects($this->exactly(2))
             ->method('addSourceLinkAndUpdateCounts')
        ;

        // assert calls return the two links
        $this->object
             ->expects($this->at(1))
             ->method('addSourceLinkAndUpdateCounts')
             ->with('http://www.link1.com', 12)
        ;

        $this->object
             ->expects($this->at(2))
             ->method('addSourceLinkAndUpdateCounts')
             ->with('http://www.link2.com', 12)
        ;
Loading Facebook Comments ...

1 comment

  1. Hi,
    nice example. Just one thing – the calls are indexed from 0, so the first time you need to use $this->at(0), then $this->at(1) and so on :).

    Ivan