From fa53ec00956032b62d15f9404c39dfee9e9a56b4 Mon Sep 17 00:00:00 2001 From: SupunKavinda Date: Thu, 24 Oct 2024 05:34:00 +0530 Subject: [PATCH 1/3] Wip --- src/Link/LastRequestRecorder.php | 28 ++++++++++++++++++++++++++++ src/Link/Link.php | 2 ++ src/UnfoldConfig.php | 11 +++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/Link/LastRequestRecorder.php diff --git a/src/Link/LastRequestRecorder.php b/src/Link/LastRequestRecorder.php new file mode 100644 index 0000000..465155b --- /dev/null +++ b/src/Link/LastRequestRecorder.php @@ -0,0 +1,28 @@ +request = $request; + } + + public function addFailure(RequestInterface $request, ClientExceptionInterface $exception): void + { + } + + public function getLastRequest(): RequestInterface + { + return $this->request; + } +} \ No newline at end of file diff --git a/src/Link/Link.php b/src/Link/Link.php index a223464..509aa70 100644 --- a/src/Link/Link.php +++ b/src/Link/Link.php @@ -37,6 +37,8 @@ public function scrape(): string throw new LinkScrapeException("Unable to scrape link. HTTP status code: $status"); } + $lastRequest = $this->config->httpClient->getLastRequest(); + return $response->getBody()->getContents(); } diff --git a/src/UnfoldConfig.php b/src/UnfoldConfig.php index 6b001c5..48e4c3e 100644 --- a/src/UnfoldConfig.php +++ b/src/UnfoldConfig.php @@ -2,6 +2,7 @@ namespace Hyvor\Unfold; +use Http\Client\Common\Plugin\HistoryPlugin; use Http\Client\Common\Plugin\RedirectPlugin; use Http\Client\Common\PluginClient; use Http\Discovery\Psr18ClientDiscovery; @@ -68,7 +69,8 @@ public function __construct( public ?string $facebookAccessToken = null, // CACHE - ) { + ) + { $this->setHttpClient($httpClient); } @@ -77,9 +79,14 @@ private function setHttpClient(?ClientInterface $httpClient): void $httpClient ??= Psr18ClientDiscovery::find(); $redirectPlugin = new RedirectPlugin(); + $historyPlugin = new HistoryPlugin(); + $this->httpClient = new PluginClient( $httpClient, - [$redirectPlugin], + [ + $historyPlugin, + $redirectPlugin + ], [ 'max_restarts' => $this->httpMaxRedirects, ] From 9f0812847b678b99da05df3ea36775ae5c76c480 Mon Sep 17 00:00:00 2001 From: SupunKavinda Date: Thu, 24 Oct 2024 06:41:46 +0530 Subject: [PATCH 2/3] Config refactor wip --- src/Embed/Embed.php | 29 ++++---- src/Embed/EmbedParserAbstract.php | 24 +------ src/Embed/Platforms/FacebookPage.php | 2 +- src/Embed/Platforms/FacebookPost.php | 2 +- src/Embed/Platforms/FacebookVideo.php | 2 +- src/Embed/Platforms/GithubGist.php | 2 +- src/Embed/Platforms/Youtube.php | 6 +- src/Link/Link.php | 33 ++++++--- src/Link/Metadata/MetadataParser.php | 4 +- src/Link/Metadata/Parsers/LinkParser.php | 10 ++- ...equestRecorder.php => RequestRecorder.php} | 16 +++-- src/Unfold.php | 10 +-- src/UnfoldCallContext.php | 22 ------ src/UnfoldConfig.php | 52 ++++++++------ src/Unfolded/Unfolded.php | 32 +++++---- tests/Feature/UnfoldLinkTest.php | 67 ++++++++++++++++++- tests/Unit/Embed/OEmbedTest.php | 23 +++---- .../Unit/Embed/Platforms/FacebookPageTest.php | 6 +- .../Unit/Link/Metadata/MetadataParserTest.php | 12 ++-- 19 files changed, 206 insertions(+), 148 deletions(-) rename src/Link/{LastRequestRecorder.php => RequestRecorder.php} (57%) delete mode 100644 src/UnfoldCallContext.php diff --git a/src/Embed/Embed.php b/src/Embed/Embed.php index 218eb41..d0cfa39 100644 --- a/src/Embed/Embed.php +++ b/src/Embed/Embed.php @@ -5,9 +5,9 @@ use Hyvor\Unfold\Exception\EmbedParserException; use Hyvor\Unfold\Exception\EmbedUnableToResolveException; use Hyvor\Unfold\Exception\UnfoldException; -use Hyvor\Unfold\UnfoldCallContext; use Hyvor\Unfold\UnfoldConfig; use Hyvor\Unfold\Unfolded\Unfolded; +use Hyvor\Unfold\UnfoldMethod; class Embed { @@ -16,14 +16,14 @@ class Embed */ public static function getParsers(): array { - $namespace = __NAMESPACE__.'\\Platforms\\'; + $namespace = __NAMESPACE__ . '\\Platforms\\'; $parsers = array_map( - fn ($file) => $namespace.pathinfo((string) $file, PATHINFO_FILENAME), - (array) glob(__DIR__.'/Platforms/*.php') + fn($file) => $namespace . pathinfo((string)$file, PATHINFO_FILENAME), + (array)glob(__DIR__ . '/Platforms/*.php') ); - usort($parsers, fn ($a, $b) => $b::PRIORITY <=> $a::PRIORITY); + usort($parsers, fn($a, $b) => $b::PRIORITY <=> $a::PRIORITY); return $parsers; } @@ -31,18 +31,16 @@ public static function getParsers(): array /** * @throws EmbedParserException */ - public static function parse( - string $url, - ?UnfoldConfig $config = null, - ): EmbedResponseObject { + public static function parse(UnfoldConfig $config): EmbedResponseObject + { foreach (self::getParsers() as $parserClass) { /** @var EmbedParserAbstract $parser */ - $parser = new $parserClass($url, $config); + $parser = new $parserClass($config); if ($matches = $parser->match()) { return $parser->parse($matches); } } - throw new EmbedUnableToResolveException; + throw new EmbedUnableToResolveException(); } /** @@ -52,7 +50,7 @@ public static function getMatchingParser(string $url): ?array { foreach (self::getParsers() as $parserClass) { /** @var EmbedParserAbstract $parser */ - $parser = new $parserClass($url); + $parser = new $parserClass(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); if ($parser->match()) { return [ 'parser' => $parser, @@ -68,14 +66,13 @@ public static function getMatchingParser(string $url): ?array * @throws UnfoldException */ public static function getUnfoldedObject( - UnfoldCallContext $context, + UnfoldConfig $config, ): Unfolded { - $oembed = self::parse($context->url, $context->config); + $oembed = self::parse($config); return Unfolded::fromEmbed( $oembed, - $context->url, - $context + $config ); } } diff --git a/src/Embed/EmbedParserAbstract.php b/src/Embed/EmbedParserAbstract.php index e780e75..3267c16 100644 --- a/src/Embed/EmbedParserAbstract.php +++ b/src/Embed/EmbedParserAbstract.php @@ -19,26 +19,8 @@ abstract class EmbedParserAbstract // define priority relatively public const PRIORITY = 0; - protected UnfoldConfig $config; - - public function __construct( - protected string $url, - ?UnfoldConfig $config = null, - ) { - $this->config = $config ?? new UnfoldConfig(); - } - - - /** - * TODO: This is not yet used - * If the URL needs to be replaced before sending to the oEmbed endpoint, - * return the new URL here. Otherwise, return null - * Ex: m.facebook.com -> www.facebook.com - * @codeCoverageIgnore - */ - public function replaceUrl(): ?string + public function __construct(protected UnfoldConfig $config) { - return null; } /** @@ -65,7 +47,7 @@ public function match(): false|array $regex = $this->regex(); foreach ($regex as $reg) { - if (preg_match("~$reg~", $this->url, $matches)) { + if (preg_match("~$reg~", $this->config->url, $matches)) { return $matches; } } @@ -108,7 +90,7 @@ private function parseOEmbed(): EmbedResponseObject $uri = Uri::withQueryValues( new Uri($oEmbedUrl), [ - 'url' => $this->url, + 'url' => $this->config->url, 'format' => 'json' ] ); diff --git a/src/Embed/Platforms/FacebookPage.php b/src/Embed/Platforms/FacebookPage.php index 6702801..103717e 100644 --- a/src/Embed/Platforms/FacebookPage.php +++ b/src/Embed/Platforms/FacebookPage.php @@ -23,7 +23,7 @@ public function regex() public function getEmbedHtml(array $matches): string { - $url = $this->url; + $url = $this->config->url; $name = explode('/', $url)[3] ?? ''; $sdk = FacebookHelper::sdkScript(); diff --git a/src/Embed/Platforms/FacebookPost.php b/src/Embed/Platforms/FacebookPost.php index 35731ae..b73be17 100644 --- a/src/Embed/Platforms/FacebookPost.php +++ b/src/Embed/Platforms/FacebookPost.php @@ -39,7 +39,7 @@ public function regex() public function getEmbedHtml(array $matches): string { - $url = $this->url; + $url = $this->config->url; $sdk = FacebookHelper::sdkScript(); return <<url; + $url = $this->config->url; return << diff --git a/src/Embed/Platforms/GithubGist.php b/src/Embed/Platforms/GithubGist.php index 0fa3ad2..55ef3b3 100644 --- a/src/Embed/Platforms/GithubGist.php +++ b/src/Embed/Platforms/GithubGist.php @@ -16,6 +16,6 @@ public function regex() public function getEmbedHtml($matches): string { - return ""; + return ""; } } diff --git a/src/Embed/Platforms/Youtube.php b/src/Embed/Platforms/Youtube.php index a21fa54..310d040 100644 --- a/src/Embed/Platforms/Youtube.php +++ b/src/Embed/Platforms/Youtube.php @@ -57,9 +57,9 @@ public function getEmbedHtml($matches): string { $id = $matches[1] ?? ''; - $isShort = str_contains($this->url, '/shorts/'); - $isPlaylist = str_contains($this->url, '/playlist?list='); - $isUser = str_contains($this->url, '/user/'); + $isShort = str_contains($this->config->url, '/shorts/'); + $isPlaylist = str_contains($this->config->url, '/playlist?list='); + $isUser = str_contains($this->config->url, '/user/'); $padding = round(100 * ($isShort ? 16 / 9 : 9 / 16), 2); diff --git a/src/Link/Link.php b/src/Link/Link.php index 4d91d13..3f97711 100644 --- a/src/Link/Link.php +++ b/src/Link/Link.php @@ -3,31 +3,39 @@ namespace Hyvor\Unfold\Link; use GuzzleHttp\Psr7\Request; +use Http\Client\Common\Exception\LoopException; use Hyvor\Unfold\Exception\LinkScrapeException; use Hyvor\Unfold\Link\Metadata\MetadataParser; -use Hyvor\Unfold\UnfoldCallContext; use Hyvor\Unfold\UnfoldConfig; use Hyvor\Unfold\Unfolded\Unfolded; use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Message\RequestInterface; class Link { + + private ?RequestInterface $lastRequest = null; + public function __construct( - private string $url, private UnfoldConfig $config, - ) {} + ) { + } public function scrape(): string { $request = new Request( 'GET', - $this->url + $this->config->url ); try { $response = $this->config->httpClient->sendRequest($request); } catch (ClientExceptionInterface $e) { - throw new LinkScrapeException($e->getMessage()); + $message = match (true) { + $e instanceof LoopException => 'Too many redirects', + default => $e->getMessage(), + }; + throw new LinkScrapeException($message); } $status = $response->getStatusCode(); @@ -36,21 +44,24 @@ public function scrape(): string throw new LinkScrapeException("Unable to scrape link. HTTP status code: $status"); } - $lastRequest = $this->config->httpClient->getLastRequest(); + $this->lastRequest = $this->config->httpRequestRecorder->getLastRequest(); return $response->getBody()->getContents(); } public static function getUnfoldedObject( - UnfoldCallContext $context, + UnfoldConfig $config, ): Unfolded { - $content = (new Link($context->url, $context->config))->scrape(); - $metadata = (new MetadataParser($content, $context))->parse(); + $link = new Link($config); + $content = $link->scrape(); + $lastRequest = $link->lastRequest; + + $metadata = (new MetadataParser($content, $config))->parse(); return Unfolded::fromMetadata( - $context->url, + $config, $metadata, - $context, + $lastRequest, ); } } diff --git a/src/Link/Metadata/MetadataParser.php b/src/Link/Metadata/MetadataParser.php index 52d087e..9482df8 100644 --- a/src/Link/Metadata/MetadataParser.php +++ b/src/Link/Metadata/MetadataParser.php @@ -12,7 +12,7 @@ use Hyvor\Unfold\Link\Metadata\Parsers\OgParser; use Hyvor\Unfold\Link\Metadata\Parsers\TitleParser; use Hyvor\Unfold\Link\Metadata\Parsers\TwitterParser; -use Hyvor\Unfold\UnfoldCallContext; +use Hyvor\Unfold\UnfoldConfig; use Symfony\Component\DomCrawler\Crawler; class MetadataParser @@ -28,7 +28,7 @@ class MetadataParser public function __construct( private string $content, - public UnfoldCallContext $context, + public UnfoldConfig $config, ) { $this->crawler = new Crawler($this->content); $this->crawlerMeta = $this->crawler->filterXPath('//meta'); diff --git a/src/Link/Metadata/Parsers/LinkParser.php b/src/Link/Metadata/Parsers/LinkParser.php index 5200c92..b5d748e 100644 --- a/src/Link/Metadata/Parsers/LinkParser.php +++ b/src/Link/Metadata/Parsers/LinkParser.php @@ -15,11 +15,15 @@ public function add(): void $href = $node->attr('href'); if ($rel === 'canonical' && is_string($href)) { - $this->parser->addMetadata(new MetadataObject(MetadataKeyType::CANONICAL_URL, $this->fixIfRelativeUrl($href))); + $this->parser->addMetadata( + new MetadataObject(MetadataKeyType::CANONICAL_URL, $this->fixIfRelativeUrl($href)) + ); } if ($rel === 'icon' && is_string($href)) { - $this->parser->addMetadata(new MetadataObject(MetadataKeyType::FAVICON_URL, $this->fixIfRelativeUrl($href))); + $this->parser->addMetadata( + new MetadataObject(MetadataKeyType::FAVICON_URL, $this->fixIfRelativeUrl($href)) + ); } }); } @@ -30,7 +34,7 @@ public function fixIfRelativeUrl(string $url): string if (isset($parsedUrl['host'])) { return $url; } else { - return Uri::fromBaseUri($url, $this->parser->context->url)->toString(); + return Uri::fromBaseUri($url, $this->parser->config->url)->toString(); } } } diff --git a/src/Link/LastRequestRecorder.php b/src/Link/RequestRecorder.php similarity index 57% rename from src/Link/LastRequestRecorder.php rename to src/Link/RequestRecorder.php index 465155b..ec5935c 100644 --- a/src/Link/LastRequestRecorder.php +++ b/src/Link/RequestRecorder.php @@ -7,22 +7,24 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -class LastRequestRecorder implements Journal +class RequestRecorder implements Journal { - - private RequestInterface $request; + /** @var RequestInterface[] */ + private array $requests = []; public function addSuccess(RequestInterface $request, ResponseInterface $response): void { - $this->request = $request; + $this->requests[] = $request; } public function addFailure(RequestInterface $request, ClientExceptionInterface $exception): void { } - public function getLastRequest(): RequestInterface + public function getLastRequest(): ?RequestInterface { - return $this->request; + return count($this->requests) > 0 ? + $this->requests[count($this->requests) - 1] : + null; } -} \ No newline at end of file +} diff --git a/src/Unfold.php b/src/Unfold.php index a8b7d25..3e506c4 100644 --- a/src/Unfold.php +++ b/src/Unfold.php @@ -18,16 +18,12 @@ public static function unfold( UnfoldConfig $config = null, ): Unfolded { $config ??= new UnfoldConfig(); - $context = new UnfoldCallContext( - $url, - $method, - $config, - ); + $config->start($url, $method); if ($method === UnfoldMethod::LINK) { - return Link::getUnfoldedObject($context); + return Link::getUnfoldedObject($config); } elseif ($method === UnfoldMethod::EMBED) { - return Embed::getUnfoldedObject($context); + return Embed::getUnfoldedObject($config); } else { // both // TODO: diff --git a/src/UnfoldCallContext.php b/src/UnfoldCallContext.php deleted file mode 100644 index 9eef41b..0000000 --- a/src/UnfoldCallContext.php +++ /dev/null @@ -1,22 +0,0 @@ -startTime = microtime(true); - } - - public function duration(): int - { - return (int)((microtime(true) - $this->startTime) * 1000); - } - -} diff --git a/src/UnfoldConfig.php b/src/UnfoldConfig.php index 48e4c3e..981abbe 100644 --- a/src/UnfoldConfig.php +++ b/src/UnfoldConfig.php @@ -6,6 +6,7 @@ use Http\Client\Common\Plugin\RedirectPlugin; use Http\Client\Common\PluginClient; use Http\Discovery\Psr18ClientDiscovery; +use Hyvor\Unfold\Link\RequestRecorder; use Psr\Http\Client\ClientInterface; class UnfoldConfig @@ -15,24 +16,14 @@ class UnfoldConfig */ public ClientInterface $httpClient; - public function __construct( + // These are set later in the Unfold::unfold method for internal use + public string $url; + public UnfoldMethod $method; - /** - * UnfoldMethod::LINK: - * - Fetch metadata of the link. - * - $embed is null in Unfolded return - * - Other fields are set based on the metadata (as best as possible) - * UnfoldMethod::EMBED: - * - Tries to get the embed HTML using parsers (see $embedMetaFallback as well) - * - If fails, an error is thrown - * - If successful, $embed is the embed HTML - * - All other fields of Unfolded are not set - * UnfoldMethod::LINK_EMBED: - * - Fetch metadata of the link, and also tries to get the embed HTML using parsers - * - $embed is the embed HTML is successful, otherwise null (no error thrown on failure) - * - All other fields are set as in the same as UnfoldMethod::LINK - */ - public UnfoldMethod $method = UnfoldMethod::LINK, + public RequestRecorder $httpRequestRecorder; + public float $startTime; + + public function __construct( /** * If the $method is UnfoldMethod::EMBED or UnfoldMethod::EMBED_LINK, @@ -74,22 +65,45 @@ public function __construct( $this->setHttpClient($httpClient); } + public function start(string $url, UnfoldMethod $method): self + { + $this->url = $url; + $this->method = $method; + $this->startTime = microtime(true); + return $this; + } + private function setHttpClient(?ClientInterface $httpClient): void { $httpClient ??= Psr18ClientDiscovery::find(); $redirectPlugin = new RedirectPlugin(); - $historyPlugin = new HistoryPlugin(); + $this->httpRequestRecorder = new RequestRecorder(); + $historyPlugin = new HistoryPlugin($this->httpRequestRecorder); $this->httpClient = new PluginClient( $httpClient, [ + $redirectPlugin, $historyPlugin, - $redirectPlugin ], [ 'max_restarts' => $this->httpMaxRedirects, ] ); } + + public function duration(): int + { + return (int)((microtime(true) - $this->startTime) * 1000); + } + + public static function withUrlAndMethod( + string $url, + UnfoldMethod $method + ): self { + $config = new self(); + $config->start($url, $method); + return $config; + } } diff --git a/src/Unfolded/Unfolded.php b/src/Unfolded/Unfolded.php index 85ad5ba..8044841 100644 --- a/src/Unfolded/Unfolded.php +++ b/src/Unfolded/Unfolded.php @@ -6,8 +6,9 @@ use Hyvor\Unfold\Embed\EmbedResponseObject; use Hyvor\Unfold\Link\Metadata\MetadataObject; use Hyvor\Unfold\Link\Metadata\MetadataPriority; -use Hyvor\Unfold\UnfoldCallContext; +use Hyvor\Unfold\UnfoldConfig; use Hyvor\Unfold\UnfoldMethod; +use Psr\Http\Message\RequestInterface; class Unfolded { @@ -19,6 +20,7 @@ class Unfolded */ public function __construct( public UnfoldMethod $method, + public string $originalUrl, public string $url, public ?string $embed, public ?string $title, @@ -42,43 +44,49 @@ public function __construct( * @param MetadataObject[] $metadata */ public static function fromMetadata( - string $url, + UnfoldConfig $config, array $metadata, - UnfoldCallContext $context, + ?RequestInterface $lastRequest, ): self { $metadataPriority = new MetadataPriority($metadata); + $currentUrl = $config->url; + if ($lastRequest && (string)$lastRequest->getUri() !== $currentUrl) { + $currentUrl = (string)$lastRequest->getUri(); + } + return new self( - $context->method, - $url, + $config->method, + $config->url, + $currentUrl, null, $metadataPriority->title(), $metadataPriority->description(), $metadataPriority->authors(), $metadataPriority->tags(), $metadataPriority->siteName(), - $metadataPriority->siteUrl($url), + $metadataPriority->siteUrl($currentUrl), $metadataPriority->canonicalUrl(), $metadataPriority->publishedTime(), $metadataPriority->modifiedTime(), $metadataPriority->thumbnailUrl(), $metadataPriority->iconUrl(), $metadataPriority->locale(), - $context->duration() + $config->duration() ); } public static function fromEmbed( EmbedResponseObject $embed, - string $url, - UnfoldCallContext $context, + UnfoldConfig $config, ): self { $authors = $embed->author_url || $embed->author_name ? [new UnfoldedAuthor($embed->author_name, $embed->author_url)] : []; return new self( - $context->method, - $url, + $config->method, + $config->url, + $config->url, $embed->html, $embed->title, null, @@ -92,7 +100,7 @@ public static function fromEmbed( $embed->thumbnail_url, null, null, - $context->duration() + $config->duration() ); } diff --git a/tests/Feature/UnfoldLinkTest.php b/tests/Feature/UnfoldLinkTest.php index e0c8e74..349386a 100644 --- a/tests/Feature/UnfoldLinkTest.php +++ b/tests/Feature/UnfoldLinkTest.php @@ -54,6 +54,8 @@ ) ); + expect($response->url)->toBe('https://hyvor.com'); + expect($response->originalUrl)->toBe('https://hyvor.com'); expect($response->title)->toBe('HYVOR'); expect($response->description)->toBe('We craft privacy-first, user-friendly tools for websites.'); expect($response->publishedTime->format('Y-m-d'))->toBe('2021-09-01'); @@ -80,7 +82,7 @@ ]); $client = new Client(['handler' => $mock]); - expect(fn () => Unfold::unfold( + expect(fn() => Unfold::unfold( 'https://hyvor.com', config: new UnfoldConfig( httpClient: $client @@ -100,7 +102,7 @@ ]); $client = new Client(['handler' => $mock]); - expect(fn () => Unfold::unfold( + expect(fn() => Unfold::unfold( 'https://hyvor.com', config: new UnfoldConfig( httpClient: $client @@ -110,3 +112,64 @@ 'Error Communicating with Server' ); }); + + +it('redirect', function () { + $mock = new MockHandler([ + new Response( + 301, + ['Location' => 'https://hyvor.com/redirected'], + ), + new Response( + 200, + [], + << + + Redirected + + +HTML + ) + ]); + $client = new Client(['handler' => $mock]); + + $response = Unfold::unfold( + 'https://hyvor.com', + config: new UnfoldConfig( + httpClient: $client + ) + ); + + expect($response->originalUrl)->toBe('https://hyvor.com'); + expect($response->url)->toBe('https://hyvor.com/redirected'); + expect($response->title)->toBe('Redirected'); +}); + +it('max redirects', function () { + $mock = new MockHandler([ + new Response( + 301, + ['Location' => 'https://hyvor.com/redirected'], + ), + new Response( + 301, + ['Location' => 'https://hyvor.com/redirected-2'], + ), + new Response( + 200, + [], + "Redirected" + ) + ]); + + $client = new Client(['handler' => $mock]); + + expect(fn() => Unfold::unfold( + 'https://hyvor.com', + config: new UnfoldConfig( + httpClient: $client, + httpMaxRedirects: 1 + ) + ))->toThrow(LinkScrapeException::class, 'Too many redirects'); +}); \ No newline at end of file diff --git a/tests/Unit/Embed/OEmbedTest.php b/tests/Unit/Embed/OEmbedTest.php index d170a50..0005569 100644 --- a/tests/Unit/Embed/OEmbedTest.php +++ b/tests/Unit/Embed/OEmbedTest.php @@ -12,6 +12,7 @@ use Hyvor\Unfold\Embed\OEmbedTypeEnum; use Hyvor\Unfold\Exception\EmbedParserException; use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; class OEmbedTestPlatform extends EmbedParserAbstract implements EmbedParserOEmbedInterface { @@ -29,7 +30,7 @@ public function oEmbedUrl(): string } it('not matching', function () { - $platform = new OEmbedTestPlatform('https://example.com'); + $platform = new OEmbedTestPlatform(UnfoldConfig::withUrlAndMethod('https://example.com', UnfoldMethod::EMBED)); expect($platform->match())->toBeFalse(); }); @@ -115,10 +116,9 @@ public function oEmbedUrl(): string $client = new Client(['handler' => $handlerStack]); $platform = new OEmbedTestPlatform( - 'https://hyvor.com/123', - new UnfoldConfig( + (new UnfoldConfig( httpClient: $client - ) + ))->start('https://hyvor.com/123', UnfoldMethod::EMBED) ); $response = $platform->parse(); @@ -141,10 +141,9 @@ public function oEmbedUrl(): string $client = new Client(['handler' => $handlerStack]); $platform = new OEmbedTestPlatform( - 'https://hyvor.com/123', - new UnfoldConfig( + (new UnfoldConfig( httpClient: $client - ) + ))->start('https://hyvor.com/123', UnfoldMethod::EMBED) ); $exception = null; @@ -167,10 +166,9 @@ public function oEmbedUrl(): string $client = new Client(['handler' => $handlerStack]); $platform = new OEmbedTestPlatform( - 'https://hyvor.com/123', - new UnfoldConfig( + (new UnfoldConfig( httpClient: $client - ) + ))->start('https://hyvor.com/123', UnfoldMethod::EMBED) ); $exception = null; @@ -193,10 +191,9 @@ public function oEmbedUrl(): string $client = new Client(['handler' => $handlerStack]); $platform = new OEmbedTestPlatform( - 'https://hyvor.com/123', - new UnfoldConfig( + (new UnfoldConfig( httpClient: $client - ) + ))->start('https://hyvor.com/123', UnfoldMethod::EMBED) ); $exception = null; diff --git a/tests/Unit/Embed/Platforms/FacebookPageTest.php b/tests/Unit/Embed/Platforms/FacebookPageTest.php index d005327..795640f 100644 --- a/tests/Unit/Embed/Platforms/FacebookPageTest.php +++ b/tests/Unit/Embed/Platforms/FacebookPageTest.php @@ -5,6 +5,8 @@ use Hyvor\Unfold\Embed\Embed; use Hyvor\Unfold\Embed\Platforms\FacebookPage; use Hyvor\Unfold\Embed\Platforms\FacebookPost; +use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; //it('matches a facebook page', function () { // $url = 'https://www.facebook.com/GMANetwork/about'; @@ -18,7 +20,7 @@ it('embeds facebook page', function () { $url = 'https://www.facebook.com/geonarah'; - $parser = new FacebookPage($url); + $parser = new FacebookPage(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); $response = $parser->parse($match); @@ -42,7 +44,7 @@ ]); it('matches', function ($url) { - $parser = new FacebookPage($url); + $parser = new FacebookPage(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); expect($parser->match())->toBeArray(); })->with([ 'https://www.facebook.com/MyPage', diff --git a/tests/Unit/Link/Metadata/MetadataParserTest.php b/tests/Unit/Link/Metadata/MetadataParserTest.php index a20b962..0884a78 100644 --- a/tests/Unit/Link/Metadata/MetadataParserTest.php +++ b/tests/Unit/Link/Metadata/MetadataParserTest.php @@ -7,7 +7,6 @@ use Hyvor\Unfold\Link\Metadata\MetadataKeyType; use Hyvor\Unfold\Link\Metadata\MetadataObject; use Hyvor\Unfold\Link\Metadata\MetadataParser; -use Hyvor\Unfold\UnfoldCallContext; use Hyvor\Unfold\UnfoldConfig; use Hyvor\Unfold\Unfolded\UnfoldedAuthor; use Hyvor\Unfold\Unfolded\UnfoldedTag; @@ -223,7 +222,10 @@ ]); it('parses metadata', function (string $content, array $metadata) { - $metadataParser = new MetadataParser($content, new UnfoldCallContext('https://nadil.io', UnfoldMethod::LINK, new UnfoldConfig)); + $metadataParser = new MetadataParser( + $content, + UnfoldConfig::withUrlAndMethod('https://nadil.io', UnfoldMethod::LINK) + ); $parsedMetadata = $metadataParser->parse(); expect($parsedMetadata)->toEqual($metadata); @@ -255,11 +257,13 @@ }); it('returns absolute url when relative favicon url is given', function ($contents, $expected) { - $metadataParser = new MetadataParser($contents, new UnfoldCallContext('https://example.com', UnfoldMethod::LINK, new UnfoldConfig)); + $metadataParser = new MetadataParser( + $contents, + UnfoldConfig::withUrlAndMethod('https://example.com', UnfoldMethod::LINK) + ); $metadata = $metadataParser->parse(); expect($metadata)->toEqual($expected); - })->with([ [ '', From aa82bd1cb443342cca18ea74ece3acfb260685ce Mon Sep 17 00:00:00 2001 From: SupunKavinda Date: Thu, 24 Oct 2024 06:46:22 +0530 Subject: [PATCH 3/3] test fixes --- tests/Unit/Embed/OEmbedTest.php | 5 ++--- tests/Unit/Embed/Platforms/FacebookPageTest.php | 2 +- tests/Unit/Embed/Platforms/FacebookPostTest.php | 6 ++++-- tests/Unit/Embed/Platforms/FacebookVideoTest.php | 6 ++++-- tests/Unit/Embed/Platforms/GithubGistTest.php | 6 ++++-- tests/Unit/Embed/Platforms/InstagramTest.php | 10 ++++++---- tests/Unit/Embed/Platforms/RedditTest.php | 13 +++++++++---- tests/Unit/Embed/Platforms/TiktokTest.php | 11 ++++++++--- tests/Unit/Embed/Platforms/TwitterTest.php | 9 +++++++-- tests/Unit/Embed/Platforms/YoutubeTest.php | 12 +++++++----- 10 files changed, 52 insertions(+), 28 deletions(-) diff --git a/tests/Unit/Embed/OEmbedTest.php b/tests/Unit/Embed/OEmbedTest.php index 0005569..16e9485 100644 --- a/tests/Unit/Embed/OEmbedTest.php +++ b/tests/Unit/Embed/OEmbedTest.php @@ -61,10 +61,9 @@ public function oEmbedUrl(): string $client = new Client(['handler' => $handlerStack]); $platform = new OEmbedTestPlatform( - 'https://hyvor.com/123', - new UnfoldConfig( + (new UnfoldConfig( httpClient: $client - ) + ))->start('https://hyvor.com/123', UnfoldMethod::EMBED) ); $response = $platform->parse(); diff --git a/tests/Unit/Embed/Platforms/FacebookPageTest.php b/tests/Unit/Embed/Platforms/FacebookPageTest.php index 795640f..cc4b6dc 100644 --- a/tests/Unit/Embed/Platforms/FacebookPageTest.php +++ b/tests/Unit/Embed/Platforms/FacebookPageTest.php @@ -56,7 +56,7 @@ ]); it('does not match', function ($url) { - $parser = new FacebookPage($url); + $parser = new FacebookPage(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); expect($parser->match())->toBeFalse(); })->with([ 'https://www.facebook.com/Mypage/other', diff --git a/tests/Unit/Embed/Platforms/FacebookPostTest.php b/tests/Unit/Embed/Platforms/FacebookPostTest.php index cedee52..73a0366 100644 --- a/tests/Unit/Embed/Platforms/FacebookPostTest.php +++ b/tests/Unit/Embed/Platforms/FacebookPostTest.php @@ -3,11 +3,13 @@ namespace Unit\Parsers; use Hyvor\Unfold\Embed\Platforms\FacebookPost; +use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; it('parses facebook embeds', function () { $url = 'https://www.facebook.com/geonarah/posts/pfbid027mqcVugNt1ChK6dwvjR2SkVJrtN754X1toi1XA1auyHgrng1g3bb3Ph2DoYANAhnl'; - $parser = new FacebookPost($url); + $parser = new FacebookPost(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); $response = $parser->parse($match); @@ -19,7 +21,7 @@ }); it('matches', function ($url) { - $parser = new FacebookPost($url); + $parser = new FacebookPost(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match)->toBeArray(); })->with([ diff --git a/tests/Unit/Embed/Platforms/FacebookVideoTest.php b/tests/Unit/Embed/Platforms/FacebookVideoTest.php index c282c86..482b5a0 100644 --- a/tests/Unit/Embed/Platforms/FacebookVideoTest.php +++ b/tests/Unit/Embed/Platforms/FacebookVideoTest.php @@ -3,6 +3,8 @@ namespace Unit\Parsers; use Hyvor\Unfold\Embed\Platforms\FacebookVideo; +use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; //it('manual', function () { // $url = 'https://www.facebook.com/reel/416122534780426'; @@ -15,7 +17,7 @@ it('embeds facebook videos', function () { $url = 'https://www.facebook.com/username/videos/123456789'; - $parser = new FacebookVideo($url); + $parser = new FacebookVideo(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); $response = $parser->parse($match); @@ -27,7 +29,7 @@ }); it('matches', function ($url) { - $facebook = new FacebookVideo($url); + $facebook = new FacebookVideo(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); expect($facebook->match())->toBeArray(); })->with([ // with username diff --git a/tests/Unit/Embed/Platforms/GithubGistTest.php b/tests/Unit/Embed/Platforms/GithubGistTest.php index 5facf94..259922b 100644 --- a/tests/Unit/Embed/Platforms/GithubGistTest.php +++ b/tests/Unit/Embed/Platforms/GithubGistTest.php @@ -3,9 +3,11 @@ namespace Unit\Parsers; use Hyvor\Unfold\Embed\Platforms\GithubGist; +use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; it('github gist urls', function (string $url) { - $parser = new GithubGist($url); + $parser = new GithubGist(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match)->toBeArray(); })->with([ @@ -14,7 +16,7 @@ ]); it('returns js script', function () { - $parser = new GithubGist('https://gist.github.com/me/my'); + $parser = new GithubGist(UnfoldConfig::withUrlAndMethod('https://gist.github.com/me/my', UnfoldMethod::EMBED)); $html = $parser->getEmbedHtml($parser->match()); expect($html)->toBe(''); }); diff --git a/tests/Unit/Embed/Platforms/InstagramTest.php b/tests/Unit/Embed/Platforms/InstagramTest.php index 7f9e036..8a1211e 100644 --- a/tests/Unit/Embed/Platforms/InstagramTest.php +++ b/tests/Unit/Embed/Platforms/InstagramTest.php @@ -3,10 +3,12 @@ namespace Unit\Parsers; use Hyvor\Unfold\Embed\Platforms\Instagram; +use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; it('matches instagram post', function () { $url = 'https://www.instagram.com/p/DA5VlaMK1Wc/'; - $parser = new Instagram($url); + $parser = new Instagram(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match['id'])->toBe('DA5VlaMK1Wc'); @@ -25,7 +27,7 @@ // https://www.instagram.com/{username}/p/{post_id} $url = 'https://www.instagram.com/emmafarrarons/p/DA5VlaMK1Wc/'; - $parser = new Instagram($url); + $parser = new Instagram(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match['username'])->toBe('emmafarrarons'); expect($match['id'])->toBe('DA5VlaMK1Wc'); @@ -43,7 +45,7 @@ it('matches reel', function () { $url = 'https://www.instagram.com/reel/C6H039Ctw_b/'; - $parser = new Instagram($url); + $parser = new Instagram(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match['id'])->toBe('C6H039Ctw_b'); expect($match['type'])->toBe('reel'); @@ -67,7 +69,7 @@ //}); it('matches instagram URLs', function (string $url, array $matches) { - $parser = new Instagram($url); + $parser = new Instagram(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match)->toBeArray(); foreach ($matches as $key => $value) { diff --git a/tests/Unit/Embed/Platforms/RedditTest.php b/tests/Unit/Embed/Platforms/RedditTest.php index fcc1a3e..5f58ef2 100644 --- a/tests/Unit/Embed/Platforms/RedditTest.php +++ b/tests/Unit/Embed/Platforms/RedditTest.php @@ -3,16 +3,21 @@ namespace Unit\Parsers; use Hyvor\Unfold\Embed\Platforms\Reddit; +use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; it('configs', function () { - $youtube = new Reddit( - 'https://www.reddit.com/r/math/comments/66k3c0/ive_just_start_reading_this_1910_book_calculus/' + $reddit = new Reddit( + UnfoldConfig::withUrlAndMethod( + 'https://www.reddit.com/r/math/comments/66k3c0/ive_just_start_reading_this_1910_book_calculus/', + UnfoldMethod::EMBED + ) ); - expect($youtube->oEmbedUrl())->toBe('https://www.reddit.com/oembed'); + expect($reddit->oEmbedUrl())->toBe('https://www.reddit.com/oembed'); }); it('matches reddit URLs', function (string $url) { - $parser = new Reddit($url); + $parser = new Reddit(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match)->toBeArray(); })->with([ diff --git a/tests/Unit/Embed/Platforms/TiktokTest.php b/tests/Unit/Embed/Platforms/TiktokTest.php index 7c72a2d..7f212c1 100644 --- a/tests/Unit/Embed/Platforms/TiktokTest.php +++ b/tests/Unit/Embed/Platforms/TiktokTest.php @@ -3,16 +3,21 @@ namespace Unit\Parsers; use Hyvor\Unfold\Embed\Platforms\Tiktok; +use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; it('configs', function () { $youtube = new Tiktok( - 'https://www.tiktok.com/@scout2015/video/6969696969696969696' + UnfoldConfig::withUrlAndMethod( + 'https://www.tiktok.com/@scout2015/video/6969696969696969696', + UnfoldMethod::EMBED + ) ); expect($youtube->oEmbedUrl())->toBe('https://www.tiktok.com/oembed'); }); -it('matches reddit URLs', function (string $url) { - $parser = new Tiktok($url); +it('matches tiktok URLs', function (string $url) { + $parser = new Tiktok(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match)->toBeArray(); })->with([ diff --git a/tests/Unit/Embed/Platforms/TwitterTest.php b/tests/Unit/Embed/Platforms/TwitterTest.php index 86461cc..90937e1 100644 --- a/tests/Unit/Embed/Platforms/TwitterTest.php +++ b/tests/Unit/Embed/Platforms/TwitterTest.php @@ -3,16 +3,21 @@ namespace Unit\Parsers; use Hyvor\Unfold\Embed\Platforms\Twitter; +use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; it('configs', function () { $youtube = new Twitter( - 'https://twitter.com/HyvorBlogs/status/1839476383136747730' + UnfoldConfig::withUrlAndMethod( + 'https://twitter.com/HyvorBlogs/status/1839476383136747730', + UnfoldMethod::EMBED + ) ); expect($youtube->oEmbedUrl())->toBe('https://publish.twitter.com/oembed'); }); it('matches reddit URLs', function (string $url) { - $parser = new Twitter($url); + $parser = new Twitter(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match)->toBeArray(); })->with([ diff --git a/tests/Unit/Embed/Platforms/YoutubeTest.php b/tests/Unit/Embed/Platforms/YoutubeTest.php index 720c307..3ece727 100644 --- a/tests/Unit/Embed/Platforms/YoutubeTest.php +++ b/tests/Unit/Embed/Platforms/YoutubeTest.php @@ -3,10 +3,12 @@ namespace Unit\Parsers; use Hyvor\Unfold\Embed\Platforms\Youtube; +use Hyvor\Unfold\UnfoldConfig; +use Hyvor\Unfold\UnfoldMethod; it('matches video', function () { $url = 'https://www.youtube.com/watch?v=TdrL3QxjyVw'; - $parser = new Youtube($url); + $parser = new Youtube(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match[1])->toBe('TdrL3QxjyVw'); @@ -19,7 +21,7 @@ it('matches playlist', function () { $url = 'https://www.youtube.com/playlist?list=PLAqhIrjkxbuWI23v9cThsA9GvCAUhRvKZ'; - $parser = new Youtube($url); + $parser = new Youtube(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match[1])->toBe('PLAqhIrjkxbuWI23v9cThsA9GvCAUhRvKZ'); @@ -32,7 +34,7 @@ it('matches user', function () { $url = 'https://www.youtube.com/user/GoogleDevelopers'; - $parser = new Youtube($url); + $parser = new Youtube(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match[1])->toBe('GoogleDevelopers'); @@ -45,7 +47,7 @@ it('matches short', function () { $url = 'https://www.youtube.com/shorts/uehCDW1fxUw'; - $parser = new Youtube($url); + $parser = new Youtube(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match[1])->toBe('uehCDW1fxUw'); @@ -58,7 +60,7 @@ it('matches youtube URLs', function (string $url) { - $parser = new Youtube($url); + $parser = new Youtube(UnfoldConfig::withUrlAndMethod($url, UnfoldMethod::EMBED)); $match = $parser->match(); expect($match)->toBeArray(); expect($match[1])->toBe('lHZwlzOUOZ4');