class HttpClientTest in HTTP Client Manager 8
Same name and namespace in other branches
- 8.2 tests/src/Unit/HttpClientTest.php \Drupal\Tests\http_client_manager\Unit\HttpClientTest
Class HttpClientTest.
@package Drupal\Tests\http_client_manager\Unit @coversDefaultClass \Drupal\http_client_manager\HttpClient @group HttpClientManager
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\http_client_manager\Unit\HttpClientTest
Expanded class hierarchy of HttpClientTest
File
- tests/
src/ Unit/ HttpClientTest.php, line 18
Namespace
Drupal\Tests\http_client_manager\UnitView source
class HttpClientTest extends UnitTestCase {
/**
* @var HttpClient
*/
protected $client;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this
->initClient('test', $this
->getServiceApiInfo());
}
/**
* Tests HttpClient::getApi().
*
* @covers ::getApi
*/
public function testGetApi() {
$this
->assertSame($this
->getServiceApiInfo(), $this->client
->getApi());
}
/**
* Tests HttpClient::getCommands().
*
* @covers ::getCommands
*/
public function testGetCommands() {
$commands = $this->client
->getCommands();
$this
->assertCount(2, $commands);
$this
->assertArrayHasKey('FindPosts', $commands);
$this
->assertArrayHasKey('FindComments', $commands);
}
/**
* Tests HttpClient::getCommand().
*
* @param string $commandName
* A Guzzle Command name.
*
* @covers ::getCommand
*
* @dataProvider providerTestGetCommand
*/
public function testGetCommand($commandName) {
$command = $this->client
->getCommand($commandName);
$this
->assertNotEmpty($command);
$this
->assertInstanceOf('Guzzle\\Service\\Description\\Operation', $command);
}
/**
* Tests HttpClient::getCommand() with wrong argument.
*
* @covers ::getCommand
*
* @expectedException \Guzzle\Common\Exception\InvalidArgumentException
* @expectedExceptionMessage Command was not found matching Missing
*/
public function testGetCommandWhichDoesNotExists() {
$command = $this->client
->getCommand('Missing');
$this
->assertEmpty($command);
}
/**
* Tests HttpClient::call().
*
* @covers ::call
*/
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));
}
/**
* Tests HttpClient::call() with wrong command name.
*
* @covers ::call
*
* @expectedException \Guzzle\Common\Exception\InvalidArgumentException
* @expectedExceptionMessage Command was not found matching Missing
*/
public function testCallWithWrongCommandName() {
$this->client
->call('Missing');
}
/**
* Tests HttpClient::__call().
*
* @covers ::__call
*/
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));
}
/**
* Tests HttpClient::__call() with wrong command name.
*
* @covers ::__call
*
* @expectedException \Guzzle\Common\Exception\InvalidArgumentException
* @expectedExceptionMessage Command was not found matching missing
*/
public function testMagicMethodCallWithWrongCommandName() {
$this->client
->missing();
}
/**
* Get Service api info.
*
* @return array
* An array of services api info.
*/
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",
];
}
/**
* Data provider for testGetCommand().
*
* @return array
* An array of command names.
*/
public function providerTestGetCommand() {
return [
[
'FindPosts',
],
[
'FindComments',
],
];
}
/**
* Initialize a new HttpClient instance.
*
* @param string $serviceApi
* The service api name.
* @param array $serviceInfo
* An array of service info as described into an http_services_api.yml file.
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HttpClientTest:: |
protected | property | ||
HttpClientTest:: |
protected | function | Get Service api info. | |
HttpClientTest:: |
private | function | Initialize a new HttpClient instance. | |
HttpClientTest:: |
public | function | Data provider for testGetCommand(). | |
HttpClientTest:: |
public | function |
Overrides UnitTestCase:: |
|
HttpClientTest:: |
public | function | Tests HttpClient::call(). | |
HttpClientTest:: |
public | function | Tests HttpClient::call() with wrong command name. | |
HttpClientTest:: |
public | function | Tests HttpClient::getApi(). | |
HttpClientTest:: |
public | function | Tests HttpClient::getCommand(). | |
HttpClientTest:: |
public | function | Tests HttpClient::getCommands(). | |
HttpClientTest:: |
public | function | Tests HttpClient::getCommand() with wrong argument. | |
HttpClientTest:: |
public | function | Tests HttpClient::__call(). | |
HttpClientTest:: |
public | function | Tests HttpClient::__call() with wrong command name. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |