In this case you are making a database call, because you fetch a model from the database. So it looks like an integration test. Your test might look like this:
Note that I assume your method is inside a controller
class SomeController extends Controller
{
protected $country = null;
public function setCountry($data)
{
$cc = array_get($data, 'data.country_code');
$country = Country::where('cc', $cc)->first();
$this->country = $country->name;
}
public function getCountry()
{
return $this->country;
}
}
In your test you can simply call this method and be done with it
class SomeControllerTest extends TestCase
{
public function testSetCountryMethod()
{
$country = factory(App\Country::class)->create([
'country_code' => 'nl',
]);
$controller = new SomeController();
$controller->setCountry([
'data' => [
'country_code' => 'nl',
],
]);
// Here is your assertion
$this->assertEquals('nl', $controller->getCountry());
}
}
See here for the factory method that I'm using: https://laravel.com/docs/5.5/database-testing#introduction
If you want to unit test this you need to split your controller methods into smaller methods. For example
class SomeController extends Controller
{
protected = $country = null;
public function setCountry($data)
{
// Do your magic here
$cc = array_get($data, 'data.country_code');
$country = Country::where('cc', $cc)->first();
$this_>setCountryCode($country->name);
}
public function setCountryCode($countryCode)
{
$this->countryCode = $countryCode;
}
public function getCountryCode()
{
return $this->countryCode;
}
}
Now that you have split it up you can write a unit test for that specific method. That's homework for now ;)