i dont know how to write mock test for my service
here is my compressContract Interface
interface compressContract
{
public function compress();
}
here is my ZipCompress class
<?php
namespace App\Http\Controllers\My\compression;
class ZipCompress implements compressContract
{
public function compress()
{
var_dump('im zip compression');
}
}
and here is my compressManager class
class compressManager
{
public compressContract $compressContract;
public function __construct(compressContract $compressContract)
{
$this->compressContract = $compressContract;
}
public function compressFile(Request $request)
{
//do somthing
}
}
and here is AppServiceProvider class in the boot method
public function boot()
{
$this->app->bind(CompressContract::class , ZipCompress::class);
}
i read articles about mocking a class but i cant write mock for these class
Firstly basics, class names are pascal cased. compressContract
should be CompressContract
.
A unit test, mocking the compression contract would look like this. Laravel has helpers in the test cases to help you actual bind the mocked instance to the container. This is described in the documentation here.
/** @test **/
public function your_test()
{
$this->mock(CompressContract::class, function (MockInterface $mock) {
$mock->shouldReceive('compress')->once()->andReturn('If you want correct return');
});
// call your class or http test your controller.
}