Skip to content

Commit

Permalink
完善轻量路线规划接口
Browse files Browse the repository at this point in the history
  • Loading branch information
her-cat committed Oct 20, 2019
1 parent 47167ed commit 96aa7f9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
41 changes: 37 additions & 4 deletions src/WebApi/Direction/LiteClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use GuzzleHttp\Exception\GuzzleException;
use HerCat\BaiduMap\Kernel\BaseClient;
use HerCat\BaiduMap\Kernel\Exceptions\InvalidConfigException;
use HerCat\BaiduMap\Kernel\Exceptions\RuntimeException;
use HerCat\BaiduMap\Kernel\Http\Response;
use HerCat\BaiduMap\Kernel\Support\Collection;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -22,21 +23,33 @@
* Class LiteClient.
*
* @author her-cat <[email protected]>
*
* @method array|Response|Collection|mixed|object|ResponseInterface driving($origin, $destination, $options = [])
* @method array|Response|Collection|mixed|object|ResponseInterface riding($origin, $destination, $options = [])
* @method array|Response|Collection|mixed|object|ResponseInterface walking($origin, $destination, $options = [])
* @method array|Response|Collection|mixed|object|ResponseInterface transit($origin, $destination, $options = [])
*/
class LiteClient extends BaseClient
{
const ALLOWED_METHODS = ['driving', 'riding', 'walking', 'transit'];

/**
* @param string $method
* @param string|array $origin
* @param string|array $destination
* @param array $options
* @param array $options
*
* @return array|Response|Collection|mixed|object|ResponseInterface
*
* @throws GuzzleException
* @throws InvalidConfigException
* @throws RuntimeException
*/
public function get($origin, $destination, $options = [])
public function execute($method, $origin, $destination, $options = [])
{
if (!$this->isAllowedMethod($method)) {
throw new RuntimeException(sprintf('Method named "%s" not found.', $method));
}

$options = array_merge([
'origin' => implode(',', (array) $origin),
'destination' => implode(',', (array) $destination),
Expand All @@ -46,6 +59,26 @@ public function get($origin, $destination, $options = [])
$options['timestamp'] = time();
}

return $this->httpGet('directionlite/v1/driving', $options);
return $this->httpGet(sprintf('directionlite/v1/%s', $method), $options);
}

/**
* @param string $name
* @param array $arguments
*
* @return array|Response|Collection|mixed|object|ResponseInterface
*
* @throws GuzzleException
* @throws InvalidConfigException
* @throws RuntimeException
*/
public function __call($name, $arguments)
{
return $this->execute($name, ...$arguments);
}

public function isAllowedMethod($method)
{
return \in_array($method, static::ALLOWED_METHODS);
}
}
25 changes: 22 additions & 3 deletions tests/WebApi/Direction/LiteClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

namespace HerCat\BaiduMap\Tests\WebApi\Direction;

use HerCat\BaiduMap\Kernel\Exceptions\RuntimeException;
use HerCat\BaiduMap\Kernel\ServiceContainer;
use HerCat\BaiduMap\Tests\TestCase;
use HerCat\BaiduMap\WebApi\Direction\LiteClient;

class LiteClientTest extends TestCase
{
public function testGet()
public function testExecute()
{
$app = new ServiceContainer([
'ak' => 'mock-ak',
Expand All @@ -28,9 +29,27 @@ public function testGet()
$client->expects()->httpGet('directionlite/v1/driving', [
'origin' => 'mock-lat-1,mock-lng-1',
'destination' => 'mock-lat-2,mock-lng-2',
'foo' => 'bar',
])->andReturn('mock-result');

$this->assertSame('mock-result', $client->get('mock-lat-1,mock-lng-1', 'mock-lat-2,mock-lng-2'));
$this->assertSame('mock-result', $client->get(['mock-lat-1', 'mock-lng-1'], ['mock-lat-2', 'mock-lng-2']));
$this->assertSame('mock-result', $client->execute('driving', 'mock-lat-1,mock-lng-1', 'mock-lat-2,mock-lng-2', ['foo' => 'bar']));
$this->assertSame('mock-result', $client->execute('driving', ['mock-lat-1', 'mock-lng-1'], ['mock-lat-2', 'mock-lng-2'], ['foo' => 'bar']));

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Method named "error-method" not found.');

$client->execute('error-method', 'mock-lat-1,mock-lng-1', 'mock-lat-2,mock-lng-2');
}

public function testIsAllowedMethod()
{
$methods = LiteClient::ALLOWED_METHODS;

$method = $methods[mt_rand(0, count($methods))];

$client = $this->mockApiClient(LiteClient::class);

$this->assertTrue($client->isAllowedMethod($method));
$this->assertFalse($client->isAllowedMethod('error-method'));
}
}

0 comments on commit 96aa7f9

Please sign in to comment.