You are here

class FieldNormalizerValueTest in JSON:API 8

@coversDefaultClass \Drupal\jsonapi\Normalizer\Value\FieldNormalizerValue @group jsonapi

@internal

Hierarchy

Expanded class hierarchy of FieldNormalizerValueTest

File

tests/src/Unit/Normalizer/Value/FieldNormalizerValueTest.php, line 18

Namespace

Drupal\Tests\jsonapi\Unit\Normalizer\Value
View source
class FieldNormalizerValueTest extends UnitTestCase {

  /**
   * The cache contexts manager.
   *
   * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cacheContextsManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->cacheContextsManager = $this
      ->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
      ->disableOriginalConstructor()
      ->getMock();
    $this->cacheContextsManager
      ->method('assertValidTokens')
      ->willReturn(TRUE);
    $container = new ContainerBuilder();
    $container
      ->set('cache_contexts_manager', $this->cacheContextsManager);
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::rasterizeValue
   * @covers ::__construct
   * @dataProvider rasterizeValueProvider
   */
  public function testRasterizeValue($values, $cardinality, $expected) {
    $object = new FieldNormalizerValue(AccessResult::allowed()
      ->cachePerUser()
      ->addCacheTags([
      'field:foo',
    ]), $values, $cardinality, 'attributes');
    $this
      ->assertEquals($expected, $object
      ->rasterizeValue());
    $this
      ->assertSame([
      'ccfoo',
      'user',
    ], $object
      ->getCacheContexts());
    $this
      ->assertSame([
      'ctfoo',
      'field:foo',
    ], $object
      ->getCacheTags());
    $this
      ->assertSame(15, $object
      ->getCacheMaxAge());
  }

  /**
   * Data provider for testRasterizeValue.
   */
  public function rasterizeValueProvider() {
    $uuid_raw = '4ae99eec-8b0e-41f7-9400-fbd65c174902';
    $uuid_value = $this
      ->prophesize(FieldItemNormalizerValue::class);
    $uuid_value
      ->rasterizeValue()
      ->willReturn('4ae99eec-8b0e-41f7-9400-fbd65c174902');
    $uuid_value
      ->getCacheContexts()
      ->willReturn([
      'ccfoo',
    ]);
    $uuid_value
      ->getCacheTags()
      ->willReturn([
      'ctfoo',
    ]);
    $uuid_value
      ->getCacheMaxAge()
      ->willReturn(15);
    return [
      [
        [
          $uuid_value
            ->reveal(),
        ],
        1,
        $uuid_raw,
      ],
      [
        [
          $uuid_value
            ->reveal(),
          $uuid_value
            ->reveal(),
        ],
        -1,
        [
          $uuid_raw,
          $uuid_raw,
        ],
      ],
    ];
  }

  /**
   * @covers ::rasterizeIncludes
   */
  public function testRasterizeIncludes() {
    $value = $this
      ->prophesize(RelationshipItemNormalizerValue::class);
    $include = $this
      ->prophesize('\\Drupal\\jsonapi\\Normalizer\\Value\\EntityNormalizerValue');
    $include
      ->rasterizeValue()
      ->willReturn('Lorem');
    $value
      ->getCacheContexts()
      ->willReturn([
      'ccfoo',
    ]);
    $value
      ->getCacheTags()
      ->willReturn([
      'ctfoo',
    ]);
    $value
      ->getCacheMaxAge()
      ->willReturn(15);
    $value
      ->getInclude()
      ->willReturn($include
      ->reveal());
    $object = new FieldNormalizerValue(AccessResult::allowed(), [
      $value
        ->reveal(),
    ], 1, 'attributes');
    $this
      ->assertEquals([
      'Lorem',
    ], $object
      ->rasterizeIncludes());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldNormalizerValueTest::$cacheContextsManager protected property The cache contexts manager.
FieldNormalizerValueTest::rasterizeValueProvider public function Data provider for testRasterizeValue.
FieldNormalizerValueTest::setUp protected function Overrides UnitTestCase::setUp
FieldNormalizerValueTest::testRasterizeIncludes public function @covers ::rasterizeIncludes
FieldNormalizerValueTest::testRasterizeValue public function @covers ::rasterizeValue @covers ::__construct @dataProvider rasterizeValueProvider
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.