I have Client class which send request to API.
class Client {
public static function getCategories() : Array
{
return Request::sendRequest("GET","categories");
}
Class Request have 1 static function and return json data.
class Request
{
public static function sendRequest($request_type, $url, $param = null) : Array
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "backend_service.com/api/$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
switch ($request_type) {
case "GET":
break;
case "POST":
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=$param");
break;
case "PUT":
break;
case "DELETE":
break;
}
$server_output = json_decode(curl_exec($ch));
curl_close($ch);
return $server_output;
}
}
Here's my Controller
class CategoriesController extends Controller
{
public function index()
{
$response = Client::getCategories();
return view("welcome", compact("response"));
}
public function store(Request $request)
{
$response = Client::createCategory($request->name);
return view("createCategory", compact("response"));
}
}
Also I have service provider:
public function register()
{
$this->app->singleton(Client::class, function () {
return new Client();
});
}
I'm trying to test it like this:
private $mock;
public function setUp() {
parent::setUp();
$this->mock = Mockery::mock(Client::class);
}
public function tearDown() {
parent::tearDown();
Mockery::close();
}
public function testGetCategories()
{
$input = ['name', 'Canaan'];
$this->mock
->shouldReceive('getCategories')
->once()
->andReturn($input);
$this->app->instance('Client', $this->mock);
$this->call('Get', '/');
}
}
And I got stuck with error:
Method getCategories() from Mockery_0_App_Lib_Client should be called exactly 1 times but called 0 times.
I've tried many version of this test from internet, but can't fix this error.
P.S. stackoverflow says that my post is mostly code and ask me to add some more details. But I think my problem is clear and I has provided all needed information.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire