I am trying to write a phpunit test with laravel 5.2 for a console command.
Few resources seems promising, but so far I failed making it work. I am trying to follow info given here:
The test is calling a command that 'binds' ExternalAPI class in its constructor. In the test, I mock the ExternalAPI.
The ExternalAPI class:
Class ExternalAPI {
protected $lang;
public function __construct($lang)
{
$this->lang = $lang;
}
public function call_api($myparam){
//do the api stuff here
}
}
The command:
class MyCommand extends Command
{
protected $signature = 'check:mycommand';
protected $externalAPI ;
public function __construct(ExternalAPI $externalAPI)
{
$this->externalAPI = $externalAPI;
parent::__construct();
}
public function handle()
{
$res = $this->externalAPI->call_api('test');
}
}
The test:
class MyCommandTest extends TestCase
{
public function setUp()
{
parent::setUp();
$methods_to_stub = ['call_api'];
$mock = $this->getMockBuilder('\ExternalAPI')
->setConstructorArgs(array('param_value'))
->setMethods($methods_to_stub)
->getMock();
$mock->method('call_api')->willReturn(44.44);
$this->app->instance(ExternalAPI::class, $mock);
}
public function test1()
{
$output = new BufferedOutput;
Artisan::call('check:mycommand', [],$output);
echo "output:$output\n\n";
}
}
The error is:
Illuminate\Contracts\Container\BindingResolutionException: Unresolvable dependency resolving [Parameter #0 [ $myparam]] in class App\MyLibrary\ExternalAPI