You are here

class HttpClientTest in HTTP Client Manager 8.2

Same name and namespace in other branches
  1. 8 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

Expanded class hierarchy of HttpClientTest

File

tests/src/Unit/HttpClientTest.php, line 18

Namespace

Drupal\Tests\http_client_manager\Unit
View source
class HttpClientTest extends UnitTestCase {

  /**
   * The client.
   *
   * @var \Drupal\http_client_manager\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(3, $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('GuzzleHttp\\Command\\Guzzle\\Operation', $command);
  }

  /**
   * Tests HttpClient::getCommand() with wrong argument.
   *
   * @covers ::getCommand
   *
   * @expectedException \InvalidArgumentException
   * @expectedExceptionMessage No operation found named 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
      ->assertInstanceOf('GuzzleHttp\\Command\\Result', $result);
    $this
      ->assertGreaterThan(1, $result
      ->count());
    $result = $this->client
      ->call('FindPost', [
      'postId' => 1,
    ]);
    $this
      ->assertNotEmpty($result);
    $this
      ->assertInstanceOf('GuzzleHttp\\Command\\Result', $result);
    $result = $result
      ->toArray();
    $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 \InvalidArgumentException
   * @expectedExceptionMessage No operation found named Missing
   */
  public function testCallWithWrongCommandName() {
    $this->client
      ->call('Missing');
  }

  /**
   * Tests HttpClient::__call().
   *
   * @covers ::__call
   */
  public function testMagicMethodCall() {
    $result = $this->client
      ->findPosts();
    $this
      ->assertNotEmpty($result);
    $this
      ->assertInstanceOf('GuzzleHttp\\Command\\Result', $result);
    $this
      ->assertGreaterThan(1, count($result));
    $result = $this->client
      ->findPost([
      'postId' => 1,
    ]);
    $this
      ->assertNotEmpty($result);
    $this
      ->assertInstanceOf('GuzzleHttp\\Command\\Result', $result);
    $result = $result
      ->toArray();
    $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 \InvalidArgumentException
   * @expectedExceptionMessage No operation found named 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",
      "config" => [
        "base_uri" => "http://jsonplaceholder.typicode.com",
      ],
      '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 [
      [
        'FindPost',
      ],
      [
        '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

Namesort descending Modifiers Type Description Overrides
HttpClientTest::$client protected property The client.
HttpClientTest::getServiceApiInfo protected function Get Service api info.
HttpClientTest::initClient private function Initialize a new HttpClient instance.
HttpClientTest::providerTestGetCommand public function Data provider for testGetCommand().
HttpClientTest::setUp public function Overrides UnitTestCase::setUp
HttpClientTest::testCall public function Tests HttpClient::call().
HttpClientTest::testCallWithWrongCommandName public function Tests HttpClient::call() with wrong command name.
HttpClientTest::testGetApi public function Tests HttpClient::getApi().
HttpClientTest::testGetCommand public function Tests HttpClient::getCommand().
HttpClientTest::testGetCommands public function Tests HttpClient::getCommands().
HttpClientTest::testGetCommandWhichDoesNotExists public function Tests HttpClient::getCommand() with wrong argument.
HttpClientTest::testMagicMethodCall public function Tests HttpClient::__call().
HttpClientTest::testMagicMethodCallWithWrongCommandName public function Tests HttpClient::__call() with wrong command name.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.