You are here

HttpClientTest.php in HTTP Client Manager 8

Same filename and directory in other branches
  1. 8.2 tests/src/Unit/HttpClientTest.php

File

tests/src/Unit/HttpClientTest.php
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.
 *
 * @package Drupal\Tests\http_client_manager\Unit
 * @coversDefaultClass \Drupal\http_client_manager\HttpClient
 * @group HttpClientManager
 */
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);
  }

}

Classes

Namesort descending Description
HttpClientTest Class HttpClientTest.