class ResourceFetcherTest
Same name in this branch
- 10 core/modules/media/tests/src/Functional/ResourceFetcherTest.php \Drupal\Tests\media\Functional\ResourceFetcherTest
Same name in other branches
- 9 core/modules/media/tests/src/Unit/ResourceFetcherTest.php \Drupal\Tests\media\Unit\ResourceFetcherTest
- 9 core/modules/media/tests/src/Kernel/ResourceFetcherTest.php \Drupal\Tests\media\Kernel\ResourceFetcherTest
- 9 core/modules/media/tests/src/Functional/ResourceFetcherTest.php \Drupal\Tests\media\Functional\ResourceFetcherTest
- 8.9.x core/modules/media/tests/src/Functional/ResourceFetcherTest.php \Drupal\Tests\media\Functional\ResourceFetcherTest
- 11.x core/modules/media/tests/src/Unit/ResourceFetcherTest.php \Drupal\Tests\media\Unit\ResourceFetcherTest
- 11.x core/modules/media/tests/src/Functional/ResourceFetcherTest.php \Drupal\Tests\media\Functional\ResourceFetcherTest
@group media
@coversDefaultClass \Drupal\media\OEmbed\ResourceFetcher
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\media\Unit\ResourceFetcherTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of ResourceFetcherTest
File
-
core/
modules/ media/ tests/ src/ Unit/ ResourceFetcherTest.php, line 23
Namespace
Drupal\Tests\media\UnitView source
class ResourceFetcherTest extends UnitTestCase {
/**
* Tests that resources are fetched with a hard-coded timeout.
*/
public function testFetchTimeout() : void {
$url = 'https://example.com/oembed?url=resource';
$headers = [
'Content-Type' => [
'text/javascript',
],
];
$body = Json::encode([
'version' => '1.0',
'type' => 'video',
'html' => 'test',
]);
$response = new Response(200, $headers, $body);
$client = $this->prophesize(Client::class);
$client->request('GET', $url, [
RequestOptions::TIMEOUT => 5,
])
->shouldBeCalled()
->willReturn($response);
$fetcher = new ResourceFetcher($client->reveal(), $this->createMock('\\Drupal\\media\\OEmbed\\ProviderRepositoryInterface'), new NullBackend('default'));
$fetcher->fetchResource($url);
}
/**
* Tests how the resource fetcher handles unknown Content-Type headers.
*
* @covers ::fetchResource
*/
public function testUnknownContentTypeHeader() : void {
$headers = [
'Content-Type' => [
'text/html',
],
];
$body = Json::encode([
'version' => '1.0',
'type' => 'video',
'html' => 'test',
]);
$valid_response = new Response(200, $headers, $body);
// Strip off the trailing '}' to produce a response that will cause a JSON
// parse error.
$invalid_response = new Response(200, $headers, rtrim($body, '}'));
// A response that is valid JSON, but does not decode to an array, should
// produce an exception as well.
$non_array_response = new Response(200, $headers, '"Valid JSON, but not an array..."');
$mock_handler = new MockHandler([
$valid_response,
$invalid_response,
$non_array_response,
]);
$client = new Client([
'handler' => HandlerStack::create($mock_handler),
]);
$providers = $this->createMock('\\Drupal\\media\\OEmbed\\ProviderRepositoryInterface');
$fetcher = new ResourceFetcher($client, $providers, new NullBackend('default'));
/** @var \Drupal\media\OEmbed\Resource $resource */
$resource = $fetcher->fetchResource('valid');
// The resource should have been successfully decoded as JSON.
$this->assertSame('video', $resource->getType());
$this->assertSame('test', $resource->getHtml());
// Invalid JSON should throw an exception.
try {
$fetcher->fetchResource('invalid');
$this->fail('Expected a ResourceException to be thrown for invalid JSON.');
} catch (ResourceException $e) {
$this->assertSame('Error decoding oEmbed resource: Syntax error', $e->getMessage());
}
// Valid JSON that does not produce an array should also throw an exception.
$this->expectException(ResourceException::class);
$this->expectExceptionMessage('The oEmbed resource could not be decoded.');
$fetcher->fetchResource('non_array');
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|---|
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | ||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | ||
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. | ||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | |
ResourceFetcherTest::testFetchTimeout | public | function | Tests that resources are fetched with a hard-coded timeout. | ||
ResourceFetcherTest::testUnknownContentTypeHeader | public | function | Tests how the resource fetcher handles unknown Content-Type headers. | ||
UnitTestCase::$root | protected | property | The app root. | 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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
UnitTestCase::setUp | protected | function | 358 | ||
UnitTestCase::setUpBeforeClass | public static | function | |||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.