View source  
  <?php
namespace Drupal\Tests\search_api_location_geocoder\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Geocoder\Model\Address;
use Geocoder\Model\AddressCollection;
use Geocoder\Model\Coordinates;
class GeocodeTest extends KernelTestBase {
  
  public static $modules = [
    'user',
    'search_api',
    'search_api_location',
    'search_api_location_geocoder',
    'geocoder',
  ];
  
  protected $sut;
  
  public function setUp() {
    parent::setUp();
    $ghent = new AddressCollection([
      new Address(new Coordinates(51.037455, 3.7192784)),
    ]);
    
    $geocoder = $this
      ->getMockBuilder('\\Drupal\\geocoder\\Geocoder')
      ->disableOriginalConstructor()
      ->getMock();
    $geocoder
      ->expects($this
      ->any())
      ->method('geocode')
      ->with('Ghent')
      ->willReturn($ghent);
    
    $this->container
      ->set('geocoder', $geocoder);
    \Drupal::setContainer($this->container);
    $configuration = [
      'plugins' => [
        'openstreetmap' => [
          'checked' => TRUE,
          'weight' => '-3',
        ],
        'llama' => [
          'checked' => FALSE,
          'weight' => '-3',
        ],
      ],
    ];
    $this->sut = $this->container
      ->get('plugin.manager.search_api_location.location_input')
      ->createInstance('geocode', $configuration);
  }
  
  public function testGetParsedInput() {
    $input['value'] = 'Ghent';
    $parsed = $this->sut
      ->getParsedInput($input);
    list($lat, $lng) = explode(',', $parsed);
    $this
      ->assertEquals(round($lat, 0, PHP_ROUND_HALF_DOWN), 51);
    $this
      ->assertEquals(round($lng, 0, PHP_ROUND_HALF_DOWN), 4);
  }
  
  public function testWithUnexpectedInput() {
    $input = [
      'animal' => 'llama',
    ];
    $this
      ->expectException(\InvalidArgumentException::class);
    $this->sut
      ->getParsedInput($input);
  }
  
  public function testWithNonArrayInput() {
    $input = [
      'llama',
    ];
    $this
      ->expectException(\InvalidArgumentException::class);
    $this->sut
      ->getParsedInput($input);
  }
}