You are here

class LinkManagerTest in JSON:API 8

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

@internal

Hierarchy

Expanded class hierarchy of LinkManagerTest

File

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

Namespace

Drupal\Tests\jsonapi\Unit\LinkManager
View source
class LinkManagerTest extends UnitTestCase {

  /**
   * The SUT.
   *
   * @var \Drupal\jsonapi\LinkManager\LinkManager
   */
  protected $linkManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $router = $this
      ->prophesize(ChainRouterInterface::class);
    $url_generator = $this
      ->prophesize(UrlGeneratorInterface::class);
    $url_generator
      ->generateFromRoute(Argument::cetera())
      ->willReturnArgument(2);
    $this->linkManager = new LinkManager($router
      ->reveal(), $url_generator
      ->reveal());
  }

  /**
   * @covers ::getPagerLinks
   * @dataProvider getPagerLinksProvider
   */
  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);
  }

  /**
   * Data provider for testGetPagerLinks.
   *
   * @return array
   *   The data for the test method.
   */
  public function getPagerLinksProvider() {
    return [
      [
        1,
        4,
        TRUE,
        8,
        TRUE,
        [
          'first' => [
            'offset' => 0,
            'limit' => 4,
          ],
          'prev' => [
            'offset' => 0,
            'limit' => 4,
          ],
          'next' => [
            'offset' => 5,
            'limit' => 4,
          ],
          'last' => [
            'offset' => 4,
            'limit' => 4,
          ],
        ],
      ],
      [
        6,
        4,
        FALSE,
        4,
        TRUE,
        [
          'first' => [
            'offset' => 0,
            'limit' => 4,
          ],
          'prev' => [
            'offset' => 2,
            'limit' => 4,
          ],
          'next' => NULL,
        ],
      ],
      [
        7,
        4,
        FALSE,
        5,
        FALSE,
        [
          'first' => [
            'offset' => 0,
            'limit' => 4,
          ],
          'prev' => [
            'offset' => 3,
            'limit' => 4,
          ],
          'next' => NULL,
        ],
      ],
      [
        10,
        4,
        FALSE,
        20,
        FALSE,
        [
          'first' => [
            'offset' => 0,
            'limit' => 4,
          ],
          'prev' => [
            'offset' => 6,
            'limit' => 4,
          ],
          'next' => NULL,
        ],
      ],
      [
        5,
        4,
        TRUE,
        30,
        FALSE,
        [
          'first' => [
            'offset' => 0,
            'limit' => 4,
          ],
          'prev' => [
            'offset' => 1,
            'limit' => 4,
          ],
          'next' => [
            'offset' => 9,
            'limit' => 4,
          ],
        ],
      ],
      [
        0,
        4,
        TRUE,
        100,
        TRUE,
        [
          'first' => NULL,
          'prev' => NULL,
          'next' => [
            'offset' => 4,
            'limit' => 4,
          ],
          'last' => [
            'offset' => 96,
            'limit' => 4,
          ],
        ],
      ],
      [
        0,
        1,
        FALSE,
        1,
        FALSE,
        [
          'first' => NULL,
          'prev' => NULL,
          'next' => NULL,
        ],
      ],
      [
        0,
        1,
        FALSE,
        2,
        FALSE,
        [
          'first' => NULL,
          'prev' => NULL,
          'next' => NULL,
        ],
      ],
    ];
  }

  /**
   * Test errors.
   *
   * @covers ::getPagerLinks
   * @dataProvider getPagerLinksErrorProvider
   */
  public function testGetPagerLinksError($offset, $size, $has_next_page, $total, $include_count, array $pages) {
    $this
      ->setExpectedException(BadRequestHttpException::class);
    $this
      ->testGetPagerLinks($offset, $size, $has_next_page, $total, $include_count, $pages);
  }

  /**
   * Data provider for testGetPagerLinksError.
   *
   * @return array
   *   The data for the test method.
   */
  public function getPagerLinksErrorProvider() {
    return [
      [
        0,
        -5,
        FALSE,
        10,
        TRUE,
        [
          'first' => NULL,
          'prev' => NULL,
          'last' => NULL,
          'next' => NULL,
        ],
      ],
    ];
  }

  /**
   * @covers ::getRequestLink
   */
  public function testGetRequestLink() {
    $assembler = $this
      ->prophesize(UnroutedUrlAssemblerInterface::class);
    $assembler
      ->assemble(Argument::type('string'), [
      'external' => TRUE,
      'query' => [
        'dolor' => 'sid',
      ],
    ], FALSE)
      ->will(function ($args) {
      return $args[0] . '?dolor=sid';
    })
      ->shouldBeCalled();
    $container = new ContainerBuilder();
    $container
      ->set('unrouted_url_assembler', $assembler
      ->reveal());
    \Drupal::setContainer($container);
    $request = $this
      ->prophesize(Request::class);
    $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');
    $this
      ->assertSame('https://example.com/drupal/jsonapi/node/article/07c870e9-491b-4173-8e2b-4e059400af72?dolor=sid', $this->linkManager
      ->getRequestLink($request
      ->reveal(), [
      'dolor' => 'sid',
    ]));

    // Get the default query from the request object.
    $this
      ->assertSame('https://example.com/drupal/jsonapi/node/article/07c870e9-491b-4173-8e2b-4e059400af72?amet=pax', $this->linkManager
      ->getRequestLink($request
      ->reveal()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkManagerTest::$linkManager protected property The SUT.
LinkManagerTest::getPagerLinksErrorProvider public function Data provider for testGetPagerLinksError.
LinkManagerTest::getPagerLinksProvider public function Data provider for testGetPagerLinks.
LinkManagerTest::setUp protected function Overrides UnitTestCase::setUp
LinkManagerTest::testGetPagerLinks public function @covers ::getPagerLinks @dataProvider getPagerLinksProvider
LinkManagerTest::testGetPagerLinksError public function Test errors.
LinkManagerTest::testGetRequestLink public function @covers ::getRequestLink
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.