PHPUnit: Mocking __get() results in "__get() must take exactly 1 argument ..." PHPUnit: Mocking __get() results in "__get() must take exactly 1 argument ..." php php

PHPUnit: Mocking __get() results in "__get() must take exactly 1 argument ..."


__get() takes an argument, so you need to provide the mock with one:

/** * @test */public function shouldReturnSnickers(){    $this->mock->expects($this->once())               ->method('__get')               ->with($this->equalTo('snack'))               ->will($this->returnValue('snickers'));    $this->assertEquals('snickers', $this->sut->getSnack());}

The with() method sets the argument for the mocked method in PHPUnit. You can find more details in the section on Test Doubles.


Look in the mocked magic method __get. Probably you call there one more __get method from another and not properly mocked object.


What you are doing in the setUp method of your GetSnackTest class is incorrect.If you want the code of the __get method to be executed (which would be the point of your test> I suppose), you have to change the way you call setMethods in the setup method.Here's the complete explanation, but here's the relevant part:

Passing an array containing method names

The methods you have identified:

Are all stubs, All return null by default, Are easily overridable

So, you need to call setMethods by passing null, or by passing an array that contains some methods (the ones that you really want to stub), but not- __get (because you actually want the code of that method to be executed).

The, in the shouldReturnSnickers method, you will simply want to want to call $this->assertEquals('snickers', $this->sut->getSnack());, without the preceding lines with the expect part.This will ensure the code of your __get method is actually executed and tested.