View source
<?php
namespace Drupal\Tests\http_client_manager\Unit;
use Drupal\http_client_manager\HttpClient;
use Drupal\http_client_manager\HttpServiceApiHandlerInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class HttpClientTest extends UnitTestCase {
protected $client;
public function setUp() {
parent::setUp();
$this
->initClient('test', $this
->getServiceApiInfo());
}
public function testGetApi() {
$this
->assertSame($this
->getServiceApiInfo(), $this->client
->getApi());
}
public function testGetCommands() {
$commands = $this->client
->getCommands();
$this
->assertCount(2, $commands);
$this
->assertArrayHasKey('FindPosts', $commands);
$this
->assertArrayHasKey('FindComments', $commands);
}
public function testGetCommand($commandName) {
$command = $this->client
->getCommand($commandName);
$this
->assertNotEmpty($command);
$this
->assertInstanceOf('Guzzle\\Service\\Description\\Operation', $command);
}
public function testGetCommandWhichDoesNotExists() {
$command = $this->client
->getCommand('Missing');
$this
->assertEmpty($command);
}
public function testCall() {
$result = $this->client
->call('FindPosts');
$this
->assertNotEmpty($result);
$this
->assertInternalType('array', $result);
$this
->assertGreaterThan(1, count($result));
$result = $this->client
->call('FindPosts', [
'postId' => 1,
]);
$this
->assertNotEmpty($result);
$this
->assertInternalType('array', $result);
$this
->assertArrayHasKey('id', $result);
$this
->assertEquals(1, $result['id']);
$this
->assertCount(4, $result);
$keys = [
'userId',
'id',
'title',
'body',
];
$this
->assertSame($keys, array_keys($result));
}
public function testCallWithWrongCommandName() {
$this->client
->call('Missing');
}
public function testMagicMethodCall() {
$result = $this->client
->findPosts();
$this
->assertNotEmpty($result);
$this
->assertInternalType('array', $result);
$this
->assertGreaterThan(1, count($result));
$result = $this->client
->findPosts([
'postId' => 1,
]);
$this
->assertNotEmpty($result);
$this
->assertInternalType('array', $result);
$this
->assertArrayHasKey('id', $result);
$this
->assertEquals(1, $result['id']);
$this
->assertCount(4, $result);
$keys = [
'userId',
'id',
'title',
'body',
];
$this
->assertSame($keys, array_keys($result));
}
public function testMagicMethodCallWithWrongCommandName() {
$this->client
->missing();
}
protected function getServiceApiInfo() {
return [
"title" => "TestApi",
"api_path" => "api/test_api.json",
"base_url" => "http://jsonplaceholder.typicode.com",
"config" => [],
'id' => "mock",
'provider' => "mock",
'source' => __DIR__ . "/api/test_api.json",
];
}
public function providerTestGetCommand() {
return [
[
'FindPosts',
],
[
'FindComments',
],
];
}
private function initClient($serviceApi, array $serviceInfo) {
$apiHandler = $this
->prophesize(HttpServiceApiHandlerInterface::class);
$apiHandler
->load(Argument::any())
->willReturn($serviceInfo);
$apiHandler = $apiHandler
->reveal();
$event_dispatcher = $this
->prophesize(EventDispatcherInterface::class)
->reveal();
$this->client = new HttpClient($serviceApi, $apiHandler, $event_dispatcher);
}
}