You are here

class JsonApiParamEnhancerTest in JSON:API 8

@coversDefaultClass \Drupal\jsonapi\Routing\JsonApiParamEnhancer @group jsonapi @group jsonapi_param_enhancer @group legacy

@internal

Hierarchy

Expanded class hierarchy of JsonApiParamEnhancerTest

File

tests/src/Unit/Routing/JsonApiParamEnhancerTest.php, line 26

Namespace

Drupal\Tests\jsonapi\Unit\Routing
View source
class JsonApiParamEnhancerTest extends UnitTestCase {

  /**
   * @covers ::applies
   */
  public function testApplies() {
    list($filter_normalizer, $sort_normalizer, $page_normalizer) = $this
      ->getMockNormalizers();
    $object = new JsonApiParamEnhancer($filter_normalizer, $sort_normalizer, $page_normalizer);
    $this
      ->assertTrue($object
      ->applies(new Route('/jsonapi/foo/bar', [
      RouteObjectInterface::CONTROLLER_NAME => Routes::FRONT_CONTROLLER,
      Routes::JSON_API_ROUTE_FLAG_KEY => TRUE,
      Routes::RESOURCE_TYPE_KEY => 'foo--bar',
    ])));
    $this
      ->assertFalse($object
      ->applies(new Route('/llama/foo/bar', [
      RouteObjectInterface::CONTROLLER_NAME => 'failing',
    ])));
  }

  /**
   * @covers ::enhance
   */
  public function testEnhancePage() {
    list($filter_normalizer, $sort_normalizer, $page_normalizer) = $this
      ->getMockNormalizers();
    $object = new JsonApiParamEnhancer($filter_normalizer, $sort_normalizer, $page_normalizer);
    $defaults = $object
      ->enhance([
      RouteObjectInterface::CONTROLLER_NAME => Routes::FRONT_CONTROLLER,
      Routes::RESOURCE_TYPE_KEY => new ResourceType('foo', 'bar', NULL),
      Routes::JSON_API_ROUTE_FLAG_KEY => TRUE,
    ], Request::create('/jsonapi/foo/bar', 'GET', [
      'page' => [
        'cursor' => 'lorem',
      ],
    ]));
    $this
      ->assertInstanceOf(OffsetPage::class, $defaults['_json_api_params']['page']);
    $this
      ->assertTrue(empty($defaults['_json_api_params']['filter']));
    $this
      ->assertTrue(empty($defaults['_json_api_params']['sort']));
  }

  /**
   * @covers ::enhance
   */
  public function testEnhanceSort() {
    list($filter_normalizer, $sort_normalizer, $page_normalizer) = $this
      ->getMockNormalizers();
    $object = new JsonApiParamEnhancer($filter_normalizer, $sort_normalizer, $page_normalizer);
    $defaults = $object
      ->enhance([
      RouteObjectInterface::CONTROLLER_NAME => Routes::FRONT_CONTROLLER,
      Routes::RESOURCE_TYPE_KEY => new ResourceType('foo', 'bar', NULL),
      Routes::JSON_API_ROUTE_FLAG_KEY => TRUE,
    ], Request::create('/jsonapi/foo/bar', 'GET', [
      'sort' => '-lorem',
    ]));
    $this
      ->assertInstanceOf(Sort::class, $defaults['_json_api_params']['sort']);
    $this
      ->assertInstanceOf(OffsetPage::class, $defaults['_json_api_params']['page']);
    $this
      ->assertTrue(empty($defaults['_json_api_params']['filter']));
  }

  /**
   * Builds mock normalizers.
   */
  public function getMockNormalizers() {
    $filter_normalizer = $this
      ->prophesize(DenormalizerInterface::class);
    $filter_normalizer
      ->denormalize(Argument::any(), Filter::class, Argument::any(), Argument::any())
      ->willReturn($this
      ->prophesize(Filter::class)
      ->reveal());
    $sort_normalizer = $this
      ->prophesize(DenormalizerInterface::class);
    $sort_normalizer
      ->denormalize(Argument::any(), Sort::class, Argument::any(), Argument::any())
      ->willReturn($this
      ->prophesize(Sort::class)
      ->reveal());
    $page_normalizer = $this
      ->prophesize(DenormalizerInterface::class);
    $page_normalizer
      ->denormalize(Argument::any(), OffsetPage::class)
      ->willReturn($this
      ->prophesize(OffsetPage::class)
      ->reveal());
    return [
      $filter_normalizer
        ->reveal(),
      $sort_normalizer
        ->reveal(),
      $page_normalizer
        ->reveal(),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonApiParamEnhancerTest::getMockNormalizers public function Builds mock normalizers.
JsonApiParamEnhancerTest::testApplies public function @covers ::applies
JsonApiParamEnhancerTest::testEnhancePage public function @covers ::enhance
JsonApiParamEnhancerTest::testEnhanceSort public function @covers ::enhance
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