You are here

public function RelationshipNormalizerValueTest::rasterizeValueProvider in JSON:API 8

Data provider fortestRasterizeValue.

File

tests/src/Unit/Normalizer/Value/RelationshipNormalizerValueTest.php, line 75

Class

RelationshipNormalizerValueTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\Value\RelationshipNormalizerValue @group jsonapi

Namespace

Drupal\Tests\jsonapi\Unit\Normalizer\Value

Code

public function rasterizeValueProvider() {
  $uid_raw = 1;
  $uid1 = $this
    ->prophesize(RelationshipItemNormalizerValue::class);
  $uid1
    ->rasterizeValue()
    ->willReturn([
    'type' => 'user',
    'id' => $uid_raw++,
  ]);
  $uid1
    ->getInclude()
    ->willReturn(NULL);
  $uid1
    ->getCacheContexts()
    ->willReturn([
    'ccfoo',
  ]);
  $uid1
    ->getCacheTags()
    ->willReturn([
    'ctfoo',
  ]);
  $uid1
    ->getCacheMaxAge()
    ->willReturn(15);
  $uid2 = $this
    ->prophesize(RelationshipItemNormalizerValue::class);
  $uid2
    ->rasterizeValue()
    ->willReturn([
    'type' => 'user',
    'id' => $uid_raw,
  ]);
  $uid2
    ->getInclude()
    ->willReturn(NULL);
  $uid2
    ->getCacheContexts()
    ->willReturn([
    'ccbar',
  ]);
  $uid2
    ->getCacheTags()
    ->willReturn([
    'ctbar',
  ]);
  $uid2
    ->getCacheMaxAge()
    ->willReturn(10);
  $img_id = $this
    ->randomMachineName();
  $img1 = $this
    ->prophesize(RelationshipItemNormalizerValue::class);
  $img1
    ->rasterizeValue()
    ->willReturn([
    'type' => 'file--file',
    'id' => $img_id,
    'meta' => [
      'alt' => 'Cute llama',
      'title' => 'My spirit animal',
    ],
  ]);
  $img1
    ->getInclude()
    ->willReturn(NULL);
  $img1
    ->getCacheContexts()
    ->willReturn([
    'ccimg1',
  ]);
  $img1
    ->getCacheTags()
    ->willReturn([
    'ctimg1',
  ]);
  $img1
    ->getCacheMaxAge()
    ->willReturn(100);
  $img2 = $this
    ->prophesize(RelationshipItemNormalizerValue::class);
  $img2
    ->rasterizeValue()
    ->willReturn([
    'type' => 'file--file',
    'id' => $img_id,
    'meta' => [
      'alt' => 'Adorable llama',
      'title' => 'My spirit animal ๐Ÿ˜',
    ],
  ]);
  $img2
    ->getInclude()
    ->willReturn(NULL);
  $img2
    ->getCacheContexts()
    ->willReturn([
    'ccimg2',
  ]);
  $img2
    ->getCacheTags()
    ->willReturn([
    'ctimg2',
  ]);
  $img2
    ->getCacheMaxAge()
    ->willReturn(50);
  $links = [
    'self' => 'dummy_entity_link',
    'related' => 'dummy_entity_link',
  ];
  return [
    'single cardinality' => [
      [
        $uid1
          ->reveal(),
      ],
      1,
      [
        'data' => [
          'type' => 'user',
          'id' => 1,
        ],
        'links' => $links,
      ],
      (new CacheableMetadata())
        ->setCacheContexts([
        'ccfoo',
        'user',
      ])
        ->setCacheTags([
        'ctfoo',
        'relationship:foo',
      ])
        ->setCacheMaxAge(15),
    ],
    'multiple cardinality' => [
      [
        $uid1
          ->reveal(),
        $uid2
          ->reveal(),
      ],
      2,
      [
        'data' => [
          [
            'type' => 'user',
            'id' => 1,
          ],
          [
            'type' => 'user',
            'id' => 2,
          ],
        ],
        'links' => $links,
      ],
      (new CacheableMetadata())
        ->setCacheContexts([
        'ccbar',
        'ccfoo',
        'user',
      ])
        ->setCacheTags([
        'ctbar',
        'ctfoo',
        'relationship:foo',
      ])
        ->setCacheMaxAge(10),
    ],
    'multiple cardinality, all same values' => [
      [
        $uid1
          ->reveal(),
        $uid1
          ->reveal(),
      ],
      2,
      [
        'data' => [
          [
            'type' => 'user',
            'id' => 1,
            'meta' => [
              'arity' => 0,
            ],
          ],
          [
            'type' => 'user',
            'id' => 1,
            'meta' => [
              'arity' => 1,
            ],
          ],
        ],
        'links' => $links,
      ],
      (new CacheableMetadata())
        ->setCacheContexts([
        'ccfoo',
        'user',
      ])
        ->setCacheTags([
        'ctfoo',
        'relationship:foo',
      ])
        ->setCacheMaxAge(15),
    ],
    'multiple cardinality, some same values' => [
      [
        $uid1
          ->reveal(),
        $uid2
          ->reveal(),
        $uid1
          ->reveal(),
      ],
      2,
      [
        'data' => [
          [
            'type' => 'user',
            'id' => 1,
            'meta' => [
              'arity' => 0,
            ],
          ],
          [
            'type' => 'user',
            'id' => 2,
          ],
          [
            'type' => 'user',
            'id' => 1,
            'meta' => [
              'arity' => 1,
            ],
          ],
        ],
        'links' => $links,
      ],
      (new CacheableMetadata())
        ->setCacheContexts([
        'ccbar',
        'ccfoo',
        'user',
      ])
        ->setCacheTags([
        'ctbar',
        'ctfoo',
        'relationship:foo',
      ])
        ->setCacheMaxAge(10),
    ],
    'single cardinality, with meta' => [
      [
        $img1
          ->reveal(),
      ],
      1,
      [
        'data' => [
          'type' => 'file--file',
          'id' => $img_id,
          'meta' => [
            'alt' => 'Cute llama',
            'title' => 'My spirit animal',
          ],
        ],
        'links' => $links,
      ],
      (new CacheableMetadata())
        ->setCacheContexts([
        'ccimg1',
        'user',
      ])
        ->setCacheTags([
        'ctimg1',
        'relationship:foo',
      ])
        ->setCacheMaxAge(100),
    ],
    'multiple cardinality, all same values, with meta' => [
      [
        $img1
          ->reveal(),
        $img1
          ->reveal(),
      ],
      2,
      [
        'data' => [
          [
            'type' => 'file--file',
            'id' => $img_id,
            'meta' => [
              'alt' => 'Cute llama',
              'title' => 'My spirit animal',
              'arity' => 0,
            ],
          ],
          [
            'type' => 'file--file',
            'id' => $img_id,
            'meta' => [
              'alt' => 'Cute llama',
              'title' => 'My spirit animal',
              'arity' => 1,
            ],
          ],
        ],
        'links' => $links,
      ],
      (new CacheableMetadata())
        ->setCacheContexts([
        'ccimg1',
        'user',
      ])
        ->setCacheTags([
        'ctimg1',
        'relationship:foo',
      ])
        ->setCacheMaxAge(100),
    ],
    'multiple cardinality, some same values with same values but different meta' => [
      [
        $img1
          ->reveal(),
        $img1
          ->reveal(),
        $img2
          ->reveal(),
      ],
      2,
      [
        'data' => [
          [
            'type' => 'file--file',
            'id' => $img_id,
            'meta' => [
              'alt' => 'Cute llama',
              'title' => 'My spirit animal',
              'arity' => 0,
            ],
          ],
          [
            'type' => 'file--file',
            'id' => $img_id,
            'meta' => [
              'alt' => 'Cute llama',
              'title' => 'My spirit animal',
              'arity' => 1,
            ],
          ],
          [
            'type' => 'file--file',
            'id' => $img_id,
            'meta' => [
              'alt' => 'Adorable llama',
              'title' => 'My spirit animal ๐Ÿ˜',
              'arity' => 2,
            ],
          ],
        ],
        'links' => $links,
      ],
      (new CacheableMetadata())
        ->setCacheContexts([
        'ccimg1',
        'ccimg2',
        'user',
      ])
        ->setCacheTags([
        'ctimg1',
        'ctimg2',
        'relationship:foo',
      ])
        ->setCacheMaxAge(50),
    ],
    'all the edge cases!' => [
      [
        $img1
          ->reveal(),
        $img1
          ->reveal(),
        $img2
          ->reveal(),
        $uid1
          ->reveal(),
        $uid2
          ->reveal(),
        $uid1
          ->reveal(),
      ],
      10,
      [
        'data' => [
          [
            'type' => 'file--file',
            'id' => $img_id,
            'meta' => [
              'alt' => 'Cute llama',
              'title' => 'My spirit animal',
              'arity' => 0,
            ],
          ],
          [
            'type' => 'file--file',
            'id' => $img_id,
            'meta' => [
              'alt' => 'Cute llama',
              'title' => 'My spirit animal',
              'arity' => 1,
            ],
          ],
          [
            'type' => 'file--file',
            'id' => $img_id,
            'meta' => [
              'alt' => 'Adorable llama',
              'title' => 'My spirit animal ๐Ÿ˜',
              'arity' => 2,
            ],
          ],
          [
            'type' => 'user',
            'id' => 1,
            'meta' => [
              'arity' => 0,
            ],
          ],
          [
            'type' => 'user',
            'id' => 2,
          ],
          [
            'type' => 'user',
            'id' => 1,
            'meta' => [
              'arity' => 1,
            ],
          ],
        ],
        'links' => $links,
      ],
      (new CacheableMetadata())
        ->setCacheContexts([
        'ccbar',
        'ccfoo',
        'ccimg1',
        'ccimg2',
        'user',
      ])
        ->setCacheTags([
        'ctbar',
        'ctfoo',
        'ctimg1',
        'ctimg2',
        'relationship:foo',
      ])
        ->setCacheMaxAge(10),
    ],
  ];
}