Home > Mobile >  Mocked facade with function defined in stdClass undefined
Mocked facade with function defined in stdClass undefined

Time:02-04

Using the following code below I was hoping to bypass the fit() function requirement in the code below:

For context, Image is a facade.

Test

$image = new stdClass;
$image->fit = function ($x, $y){};

Image::shouldReceive('make')->once()->andReturn(
    $image
);

Implementation

$image = Image::make($path);
$image->fit(150, 150);

Error

Error: Call to undefined method stdClass::fit()

I have tried making the function fit() static

CodePudding user response:

In your example $image->fit is a class property, not a method. You can't call functions inside a property as if it were a method (that would cause issues if you had a property and a method with the same name, for example).

You can try and use an anonymous class instead:

$image = new class() {
    public function fit($x, $y) {
        // some code
    }
};

Image::shouldReceive('make')->once()->andReturn(
    $image
);

You just write it as you would a normal class.

  •  Tags:  
  • Related