class LinkTest
Same name in other branches
- 9 core/modules/jsonapi/tests/src/Unit/JsonApiResource/LinkTest.php \Drupal\Tests\jsonapi\Unit\JsonApiResource\LinkTest
- 8.9.x core/modules/jsonapi/tests/src/Unit/JsonApiResource/LinkTest.php \Drupal\Tests\jsonapi\Unit\JsonApiResource\LinkTest
- 10 core/modules/jsonapi/tests/src/Unit/JsonApiResource/LinkTest.php \Drupal\Tests\jsonapi\Unit\JsonApiResource\LinkTest
@coversDefaultClass \Drupal\jsonapi\JsonApiResource\Link @group jsonapi
@internal
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\jsonapi\Unit\JsonApiResource\LinkTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of LinkTest
File
-
core/
modules/ jsonapi/ tests/ src/ Unit/ JsonApiResource/ LinkTest.php, line 21
Namespace
Drupal\Tests\jsonapi\Unit\JsonApiResourceView source
class LinkTest extends UnitTestCase {
/**
* @covers ::compare
* @dataProvider linkComparisonProvider
*/
public function testLinkComparison(array $a, array $b, bool $expected) : void {
$this->mockUrlAssembler();
$link_a = new Link(new CacheableMetadata(), Url::fromUri($a[0]), $a[1], $a[2] ?? []);
$link_b = new Link(new CacheableMetadata(), Url::fromUri($b[0]), $b[1], $b[2] ?? []);
$actual = Link::compare($link_a, $link_b);
$this->assertSame($expected, $actual === 0);
}
/**
* Provides test data for link comparison.
*/
public static function linkComparisonProvider() : \Generator {
(yield 'same href and same link relation type' => [
[
'https://jsonapi.org/foo',
'self',
],
[
'https://jsonapi.org/foo',
'self',
],
TRUE,
]);
(yield 'different href and same link relation type' => [
[
'https://jsonapi.org/foo',
'self',
],
[
'https://jsonapi.org/bar',
'self',
],
FALSE,
]);
(yield 'same href and different link relation type' => [
[
'https://jsonapi.org/foo',
'self',
],
[
'https://jsonapi.org/foo',
'related',
],
FALSE,
]);
(yield 'same href and same link relation type and empty target attributes' => [
[
'https://jsonapi.org/foo',
'self',
[],
],
[
'https://jsonapi.org/foo',
'self',
[],
],
TRUE,
]);
(yield 'same href and same link relation type and same target attributes' => [
[
'https://jsonapi.org/foo',
'self',
[
'anchor' => 'https://jsonapi.org',
],
],
[
'https://jsonapi.org/foo',
'self',
[
'anchor' => 'https://jsonapi.org',
],
],
TRUE,
]);
// These links are not considered equivalent because it would while the
// `href` remains the same, the anchor changes the context of the link.
(yield 'same href and same link relation type and different target attributes' => [
[
'https://jsonapi.org/boy',
'self',
[
'title' => 'sue',
],
],
[
'https://jsonapi.org/boy',
'self',
[
'anchor' => '/sob',
'title' => 'pa',
],
],
FALSE,
]);
(yield 'same href and same link relation type and same nested target attributes' => [
[
'https://jsonapi.org/foo',
'self',
[
'data' => [
'foo' => 'bar',
],
],
],
[
'https://jsonapi.org/foo',
'self',
[
'data' => [
'foo' => 'bar',
],
],
],
TRUE,
]);
(yield 'same href and same link relation type and different nested target attributes' => [
[
'https://jsonapi.org/foo',
'self',
[
'data' => [
'foo' => 'bar',
],
],
],
[
'https://jsonapi.org/foo',
'self',
[
'data' => [
'foo' => 'baz',
],
],
],
FALSE,
]);
// These links are not considered equivalent because it would be unclear
// which title corresponds to which link relation type.
(yield 'same href and different link relation types and different target attributes' => [
[
'https://jsonapi.org/boy',
'self',
[
'title' => 'A boy named Sue',
],
],
[
'https://jsonapi.org/boy',
'edit',
[
'title' => 'Change name to Bill or George',
],
],
FALSE,
]);
}
/**
* @covers ::merge
* @dataProvider linkMergeProvider
*/
public function testLinkMerge(array $a, array $b, array $expected) : void {
$this->mockUrlAssembler();
$link_a = new Link((new CacheableMetadata())->addCacheTags($a[0]), Url::fromUri($a[1]), $a[2]);
$link_b = new Link((new CacheableMetadata())->addCacheTags($b[0]), Url::fromUri($b[1]), $b[2]);
$link_expected = new Link((new CacheableMetadata())->addCacheTags($expected[0]), Url::fromUri($expected[1]), $expected[2]);
$this->assertSame($link_expected->getCacheTags(), Link::merge($link_a, $link_b)->getCacheTags());
}
/**
* Provides test data for link merging.
*/
public static function linkMergeProvider() : \Generator {
(yield 'same everything' => [
[
[
'foo',
],
'https://jsonapi.org/foo',
'self',
],
[
[
'foo',
],
'https://jsonapi.org/foo',
'self',
],
[
[
'foo',
],
'https://jsonapi.org/foo',
'self',
],
]);
(yield 'different cache tags' => [
[
[
'foo',
],
'https://jsonapi.org/foo',
'self',
],
[
[
'bar',
],
'https://jsonapi.org/foo',
'self',
],
[
[
'foo',
'bar',
],
'https://jsonapi.org/foo',
'self',
],
]);
}
/**
* @covers ::getLinkRelationType
*/
public function testGetLinkRelationType() : void {
$this->mockUrlAssembler();
$link = new Link((new CacheableMetadata())->addCacheTags([
'foo',
]), Url::fromUri('https://jsonapi.org/foo'), 'self');
$this->assertSame('self', $link->getLinkRelationType());
}
/**
* Mocks the unrouted URL assembler.
*/
protected function mockUrlAssembler() {
$url_assembler = $this->getMockBuilder(UnroutedUrlAssemblerInterface::class)
->disableOriginalConstructor()
->getMock();
$url_assembler->method('assemble')
->willReturnCallback(function ($uri) {
return (new GeneratedUrl())->setGeneratedUrl($uri);
});
$container = new ContainerBuilder();
$container->set('unrouted_url_assembler', $url_assembler);
\Drupal::setContainer($container);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
LinkTest::linkComparisonProvider | public static | function | Provides test data for link comparison. | |
LinkTest::linkMergeProvider | public static | function | Provides test data for link merging. | |
LinkTest::mockUrlAssembler | protected | function | Mocks the unrouted URL assembler. | |
LinkTest::testGetLinkRelationType | public | function | @covers ::getLinkRelationType | |
LinkTest::testLinkComparison | public | function | @covers ::compare @dataProvider linkComparisonProvider |
|
LinkTest::testLinkMerge | public | function | @covers ::merge @dataProvider linkMergeProvider |
|
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
UnitTestCase::$root | protected | property | The app root. | |
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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setUp | protected | function | 355 | |
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.