PHPunit call magic methods

999 views Asked by At

I have magic method getData. Is it possible to call following inside unit test:

    $this->attributeMock
        ->method('getData')
        ->with('additional_data')
        ->willReturn('some value');

    $this->attributeMock
        ->method('getData')
        ->with('is_default')
        ->willReturn('something');

When I do this, I got:

Failed asserting that two strings are equal.
--- Expected
+++ Actual @@ @@
-'is_default'
+'additional_data'

Is there some another way?

1

There are 1 answers

0
Matteo On BEST ANSWER

You should use the PHPUnit at() method to check method invocation at certain index.

So you can use the following code:

$this->attributeMock
    ->expects($this->at(1))
    ->method('getData')
    ->with('additional_data')
    ->willReturn('some value');

$this->attributeMock
    ->expects($this->at(0))
    ->method('getData')
    ->with('is_default')
    ->willReturn('something');

You can check the following article for some reference:

http://www.andrejfarkas.com/2012/07/phpunit-at-method-to-check-method-invocation-at-certain-index/