You are here

public function LinkManagerTest::testGetPagerLinks in JSON:API 8

@covers ::getPagerLinks @dataProvider getPagerLinksProvider

1 call to LinkManagerTest::testGetPagerLinks()
LinkManagerTest::testGetPagerLinksError in tests/src/Unit/LinkManager/LinkManagerTest.php
Test errors.

File

tests/src/Unit/LinkManager/LinkManagerTest.php, line 48

Class

LinkManagerTest
@coversDefaultClass \Drupal\jsonapi\LinkManager\LinkManager @group jsonapi

Namespace

Drupal\Tests\jsonapi\Unit\LinkManager

Code

public function testGetPagerLinks($offset, $size, $has_next_page, $total, $include_count, array $pages) {
  $assembler = $this
    ->prophesize(UnroutedUrlAssemblerInterface::class);
  $assembler
    ->assemble(Argument::type('string'), Argument::type('array'), FALSE)
    ->will(function ($args) {
    return $args[0] . '?' . UrlHelper::buildQuery($args[1]['query']);
  });
  $container = new ContainerBuilder();
  $container
    ->set('unrouted_url_assembler', $assembler
    ->reveal());
  \Drupal::setContainer($container);

  // Add the extra stuff to the expected query.
  $pages = array_filter($pages);
  $pages = array_map(function ($page) {
    return 'https://example.com/drupal/jsonapi/node/article/07c870e9-491b-4173-8e2b-4e059400af72?amet=pax&page%5Boffset%5D=' . $page['offset'] . '&page%5Blimit%5D=' . $page['limit'];
  }, $pages);
  $request = $this
    ->prophesize(Request::class);

  // Have the request return the desired page parameter.
  $page_param = $this
    ->prophesize(OffsetPage::class);
  $page_param
    ->getOffset()
    ->willReturn($offset);
  $page_param
    ->getSize()
    ->willReturn($size);
  $request
    ->getUri()
    ->willReturn('https://example.com/drupal/jsonapi/node/article/07c870e9-491b-4173-8e2b-4e059400af72?amet=pax');
  $request
    ->getBaseUrl()
    ->willReturn('/drupal');
  $request
    ->getPathInfo()
    ->willReturn('');
  $request
    ->getSchemeAndHttpHost()
    ->willReturn('https://example.com');
  $request
    ->getBaseUrl()
    ->willReturn('/drupal');
  $request
    ->getPathInfo()
    ->willReturn('/jsonapi/node/article/07c870e9-491b-4173-8e2b-4e059400af72');
  $request
    ->get('_json_api_params')
    ->willReturn([
    'page' => $page_param
      ->reveal(),
  ]);
  $request->query = new ParameterBag([
    'amet' => 'pax',
  ]);
  $context = [
    'has_next_page' => $has_next_page,
  ];
  if ($include_count) {
    $context['total_count'] = $total;
  }
  $links = $this->linkManager
    ->getPagerLinks($request
    ->reveal(), $context);
  ksort($pages);
  ksort($links);
  $this
    ->assertSame($pages, $links);
}