You are here

public function OpenIDConnectAuthmapTest::testUserLoadBySubMethod in OpenID Connect / OAuth client 8

Test for the userLoadBySub method.

@dataProvider getUserLoadBySubParameters

Parameters

string $sub: The sub to test.

string $client: The client to test.

array $results: The results to return.

File

tests/src/Unit/OpenIDConnectAuthmapTest.php, line 217

Class

OpenIDConnectAuthmapTest
Test the OpenIdConnectAuthmap class.

Namespace

Drupal\Tests\openid_connect\Unit

Code

public function testUserLoadBySubMethod(string $sub, string $client, array $results) : void {
  if (!empty($results)) {
    $account = $this
      ->createMock(User::class);
    $this->userStorage
      ->expects($this
      ->once())
      ->method('load')
      ->willReturn($account);
  }
  $selectMock = $this
    ->createMock(SelectInterface::class);
  $selectMock
    ->expects($this
    ->once())
    ->method('fields')
    ->with('a', [
    'uid',
  ])
    ->willReturnSelf();
  $selectMock
    ->expects($this
    ->exactly(2))
    ->method('condition')
    ->withConsecutive([
    'client_name',
    $client,
    '=',
  ], [
    'sub',
    $sub,
    '=',
  ])
    ->willReturnSelf();
  $selectMock
    ->expects($this
    ->once())
    ->method('execute')
    ->willReturn($results);
  $this->connection
    ->expects($this
    ->once())
    ->method('select')
    ->willReturn($selectMock);
  $authMapClass = new OpenIDConnectAuthmap($this->connection, $this->entityTypeManager);
  $actualResult = $authMapClass
    ->userLoadBySub($sub, $client);
  if (empty($results)) {
    $this
      ->assertEquals(FALSE, $actualResult);
  }
  else {
    $this
      ->assertInstanceOf('\\Drupal\\Core\\Entity\\EntityInterface', $actualResult);
  }
}