You are here

class EntityCollectorTest in Simple OAuth (OAuth2) & OpenID Connect 8.2

Same name and namespace in other branches
  1. 8.4 tests/src/Unit/EntityCollectorTest.php \Drupal\Tests\simple_oauth\Unit\EntityCollectorTest
  2. 8.3 tests/src/Unit/EntityCollectorTest.php \Drupal\Tests\simple_oauth\Unit\EntityCollectorTest
  3. 5.x tests/src/Unit/EntityCollectorTest.php \Drupal\Tests\simple_oauth\Unit\EntityCollectorTest

@coversDefaultClass \Drupal\simple_oauth\ExpiredCollector @group simple_oauth

Hierarchy

Expanded class hierarchy of EntityCollectorTest

File

tests/src/Unit/EntityCollectorTest.php, line 21

Namespace

Drupal\Tests\simple_oauth\Unit
View source
class EntityCollectorTest extends UnitTestCase {

  /**
   * @covers ::collect
   */
  public function testCollect() {
    list($expired_collector, $query, ) = $this
      ->buildProphecies();
    $query
      ->condition('expire', 42, '<')
      ->shouldBeCalledTimes(1);
    $this
      ->assertArrayEquals([
      1,
      52,
    ], array_map(function ($entity) {
      return $entity
        ->id();
    }, $expired_collector
      ->collect()));
  }

  /**
   * @covers ::collectForClient
   */
  public function testCollectForClient() {
    list($expired_collector, $query, ) = $this
      ->buildProphecies();
    $client = $this
      ->prophesize(Oauth2ClientInterface::class);
    $client
      ->id()
      ->willReturn(35);
    $query
      ->condition('client', 35)
      ->shouldBeCalledTimes(1);
    $tokens = $expired_collector
      ->collectForClient($client
      ->reveal());
    $this
      ->assertArrayEquals([
      1,
      52,
    ], array_map(function ($entity) {
      return $entity
        ->id();
    }, $tokens));
  }

  /**
   * @covers ::collectForAccount
   */
  public function testCollectForAccount() {
    list($expired_collector, $token_query, , , $client_storage) = $this
      ->buildProphecies();
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->id()
      ->willReturn(22);
    $token_query
      ->condition('auth_user_id', 22)
      ->shouldBeCalledTimes(1);
    $client_storage
      ->loadByProperties([
      'user_id' => 22,
    ])
      ->shouldBeCalledTimes(1);
    $token_query
      ->condition('client', 6)
      ->shouldBeCalledTimes(1);
    $tokens = $expired_collector
      ->collectForAccount($account
      ->reveal());
    $this
      ->assertArrayEquals([
      1,
      52,
    ], array_map(function ($entity) {
      return $entity
        ->id();
    }, $tokens));
  }

  /**
   * @covers ::collect
   */
  public function testDeleteMultipleTokens() {
    list($expired_collector, , $storage) = $this
      ->buildProphecies();
    $storage
      ->delete([
      'foo',
    ])
      ->shouldBeCalledTimes(1);
    $expired_collector
      ->deleteMultipleTokens([
      'foo',
    ]);
  }
  protected function buildProphecies() {
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $token_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $token_query = $this
      ->prophesize(QueryInterface::class);
    $token_query
      ->execute()
      ->willReturn([
      1 => '1',
      52 => '52',
    ]);
    $token_storage
      ->getQuery()
      ->willReturn($token_query
      ->reveal());
    $token1 = $this
      ->prophesize(Oauth2Token::class);
    $token1
      ->id()
      ->willReturn(1);
    $token52 = $this
      ->prophesize(Oauth2Token::class);
    $token52
      ->id()
      ->willReturn(52);
    $token_storage
      ->loadMultiple([
      '1',
      '52',
    ])
      ->willReturn([
      1 => $token1
        ->reveal(),
      52 => $token52
        ->reveal(),
    ]);
    $client_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $client_query = $this
      ->prophesize(QueryInterface::class);
    $client_query
      ->execute()
      ->willReturn([
      6 => '6',
    ]);
    $client_storage
      ->getQuery()
      ->willReturn($client_query
      ->reveal());
    $client6 = $this
      ->prophesize(Oauth2Client::class);
    $client6
      ->id()
      ->willReturn(6);
    $client_storage
      ->loadByProperties([
      'user_id' => 22,
    ])
      ->willReturn([
      6 => $client6
        ->reveal(),
    ]);
    $entity_type_manager
      ->getStorage('oauth2_token')
      ->willReturn($token_storage
      ->reveal());
    $entity_type_manager
      ->getStorage('oauth2_client')
      ->willReturn($client_storage
      ->reveal());
    $date_time = $this
      ->prophesize(TimeInterface::class);
    $date_time
      ->getRequestTime()
      ->willReturn(42);
    $expired_collector = new ExpiredCollector($entity_type_manager
      ->reveal(), $date_time
      ->reveal());
    return [
      $expired_collector,
      $token_query,
      $token_storage,
      $client_query,
      $client_storage,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityCollectorTest::buildProphecies protected function
EntityCollectorTest::testCollect public function @covers ::collect
EntityCollectorTest::testCollectForAccount public function @covers ::collectForAccount
EntityCollectorTest::testCollectForClient public function @covers ::collectForClient
EntityCollectorTest::testDeleteMultipleTokens public function @covers ::collect
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.
UnitTestCase::setUp protected function 340