Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CyberList's avatar

How to test (and mock?) Intervention Images V3 ?

I installed Intervention Image with

$ composer require intervention/image-laravel

And in my test, i tried :

Image::shouldReceive('read')->andReturnTrue();

i have this error :

Mockery\Exception: The class \Intervention\Image\ImageManager is marked final and its methods cannot be replaced. Classes marked final can be passed in to \Mockery::mock() as instantiated objects to create a partial mock, but only if the mock is not subject to type hinting checks.

Also tried :

$mock = Mockery::mock(Image::class);
$mock->shouldReceive('read');
PHP Fatal error:  Cannot redeclare Mockery_3_Intervention_Image_Laravel_Facades_Image::shouldReceive() in /Users/xxx/Sites/xxx/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php(24) : eval()'d code on line 983
PHP Stack trace:

I have no idea how to do it ?

0 likes
7 replies
jlrdw's avatar

Wouldn't you just be retesting what the Intervention team has confirmed (tested) anyway?

May be better off ensuring your code is actually calling (hitting endpoint) the code. But once the code is hit (meaning that endpoint) any code to upload (whatever action) would have been tested already.

CyberList's avatar

@jlrdw

I want to test what happens when Intervention Image doesn't work and be sure that there is a redirection and an error flash message.

// at the end of the test 

response = $this->actingAs($admin)
->get(route('edit', $post))
->assertStatus(302)
->assertSessionHas('error');

CyberList's avatar

@jlrdw Thank you but I am in my PhpUnit Test for intervention/image and not for the intervention/validation library.

CyberList's avatar

Yes, that's exactly what I did in my code, I have several try and catch.

But I wanted to write a test for this and check that everything is ok, even when there is a problem with Intervention images.

CyberList's avatar
try {
	return Storage::disk('public')->put("img/youtubethumb/$videoId-min.jpg", Image::read(Http::get($YoutubeThumbUrl)->body())->resize(350, 197)->toJpeg(80));
} catch (Throwable $e) {
				return false;
}

Please or to participate in this conversation.