Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing done #9

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}
},
"require": {
"php": ">=8.0",
"symfony/dom-crawler": "^7.1",
"psr/http-client": "^1.0",
"php-http/discovery": "^1.20",
Expand Down
17 changes: 1 addition & 16 deletions src/Embed/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

namespace Hyvor\Unfold\Embed;

//use Hyvor\Unfold\Embed\Platforms\GithubGist;
//use Hyvor\Unfold\Embed\Platforms\Reddit;
//use Hyvor\Unfold\Embed\Platforms\Tiktok;
//use Hyvor\Unfold\Embed\Platforms\Twitter;
//use Hyvor\Unfold\Embed\Platforms\Youtube;
use Hyvor\Unfold\Exception\EmbedUnableToResolveException;
use Hyvor\Unfold\Exception\EmbedParserException;
use Hyvor\Unfold\Exception\UnfoldException;
use Hyvor\Unfold\UnfoldCallContext;
use Hyvor\Unfold\UnfoldConfig;
use Hyvor\Unfold\Unfolded\Unfolded;
use Hyvor\Unfold\UnfoldMethod;

class Embed
{

/**
* @return string[]
*/
Expand All @@ -26,7 +19,7 @@
$namespace = __NAMESPACE__ . '\\Platforms\\';
return array_map(
fn($file) => $namespace . pathinfo($file, PATHINFO_FILENAME),
glob(__DIR__ . '/Platforms/*.php')

Check failure on line 22 in src/Embed/Embed.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Parameter #2 $array of function array_map expects array, array<int, string>|false given.
);
}

Expand All @@ -39,33 +32,25 @@
): ?EmbedResponseObject {
foreach (self::getParsers() as $parserClass) {
$parser = new $parserClass($url, $config);
if ($parser->match()) {

Check failure on line 35 in src/Embed/Embed.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Call to an undefined method object::match().
return $parser->parse();

Check failure on line 36 in src/Embed/Embed.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Call to an undefined method object::parse().
}
}
return null;
throw new EmbedUnableToResolveException($url);
}

/**
* @return $context->method is EmbedMethodEnum::EMBED ? Unfolded : ?Unfolded
* @throws UnfoldException
*/
public static function getUnfoldedObject(

Check failure on line 46 in src/Embed/Embed.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method Hyvor\Unfold\Embed\Embed::getUnfoldedObject() has no return type specified.

Check failure on line 46 in src/Embed/Embed.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

PHPDoc tag @return has invalid value ($context->method is EmbedMethodEnum::EMBED ? Unfolded : ?Unfolded): Unexpected token "$context", expected type at offset 19
string $url,
UnfoldCallContext $context,
) {
$oembed = self::parse($url, $context->config);

if ($oembed === null) {
if ($context->method === UnfoldMethod::EMBED) {
throw new EmbedUnableToResolveException();
} else {
return null;
}
}

return Unfolded::fromEmbed(
$oembed,

Check failure on line 53 in src/Embed/Embed.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Parameter #1 $embed of static method Hyvor\Unfold\Unfolded\Unfolded::fromEmbed() expects Hyvor\Unfold\Embed\EmbedResponseObject, Hyvor\Unfold\Embed\EmbedResponseObject|null given.
$url,
$context
);
Expand Down
5 changes: 4 additions & 1 deletion src/Embed/EmbedParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* 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
{
Expand Down Expand Up @@ -72,19 +73,21 @@
public function parse(): EmbedResponseObject
{
if ($this instanceof EmbedParserOEmbedInterface) {
return $this->parseOEmbed();

Check failure on line 76 in src/Embed/EmbedParserAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method Hyvor\Unfold\Embed\EmbedParserAbstract::parse() should return Hyvor\Unfold\Embed\EmbedResponseObject but returns Hyvor\Unfold\Embed\EmbedResponseObject|null.
} elseif ($this instanceof EmbedParserCustomInterface) {
return $this->parseCustom();
} else {
// @codeCoverageIgnoreStart
throw new \Exception(
'EmbedParserAbstract should be implemented with either EmbedParserOEmbedInterface or EmbedParserCustomInterface'
); // @codeCoverageIgnore
);
// @codeCoverageIgnoreEnd
}
}

public function parseOEmbed(): ?EmbedResponseObject
{
$oEmbedUrl = $this->oEmbedUrl();

Check failure on line 90 in src/Embed/EmbedParserAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Call to an undefined method Hyvor\Unfold\Embed\EmbedParserAbstract::oEmbedUrl().

$uri = Uri::withQueryValues(
new Uri($oEmbedUrl),
Expand Down Expand Up @@ -133,9 +136,9 @@
return EmbedResponseObject::fromArray($parsed);
}

private function parseCustom()

Check failure on line 139 in src/Embed/EmbedParserAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method Hyvor\Unfold\Embed\EmbedParserAbstract::parseCustom() has no return type specified.
{
$html = $this->getEmbedHtml();

Check failure on line 141 in src/Embed/EmbedParserAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Call to an undefined method Hyvor\Unfold\Embed\EmbedParserAbstract::getEmbedHtml().

return EmbedResponseObject::fromArray([
'type' => 'embed',
Expand Down
1 change: 0 additions & 1 deletion src/Link/Metadata/MetadataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

class MetadataParser
{
// TODO: Rich Schema

/**
* @var MetadataObject[]
Expand Down
6 changes: 3 additions & 3 deletions src/Link/Metadata/MetadataPriority.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ private function prioritizedAll(array $keys)
* 'OG_TITLE' => [MetadataObject],
* ]
*/
$keysNames = array_map(fn ($key) => $key->name, $keys);
$keysNames = array_map(fn($key) => $key->name, $keys);
uksort($keyedMetadata, function ($a, $b) use ($keysNames) {
return array_search($a, $keysNames) - array_search($b, $keysNames);
});

// index by 0,1,2
$keyedMetadata = array_values($keyedMetadata);
// return the values
return array_map(fn ($metadata) => $metadata->value, $keyedMetadata[0] ?? []);
return array_map(fn($metadata) => $metadata->value, $keyedMetadata[0] ?? []);
}

/**
Expand Down Expand Up @@ -144,7 +144,7 @@ public function siteUrl(string $url): ?string
return $host ? $scheme . '://' . $host : null;
}

return null;
return null; // @ignoreCodeCoverage
}


Expand Down
8 changes: 2 additions & 6 deletions src/Link/Metadata/Parsers/LinkParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ public function add(): void
$rel = $node->attr('rel');
$href = $node->attr('href');

if (!$rel && !$href) {
return;
}

if ($rel === 'canonical') {
if ($rel === 'canonical' && is_string($href)) {
$this->parser->addMetadata(new MetadataObject(MetadataKeyType::CANONICAL_URL, $href));
}

if ($rel === 'icon') {
if ($rel === 'icon' && is_string($href)) {
$this->parser->addMetadata(new MetadataObject(MetadataKeyType::FAVICON_URL, $href));
}
});
Expand Down
1 change: 1 addition & 0 deletions src/Unfold.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static function unfold(
return Embed::getUnfoldedObject($url, $context);
} else {
// both
// TODO:
}
}
}
2 changes: 1 addition & 1 deletion src/UnfoldCallContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(

public function duration(): int
{
return (microtime(true) - $this->startTime) * 1000;
return (int)((microtime(true) - $this->startTime) * 1000);
}

}
9 changes: 9 additions & 0 deletions tests/Feature/UnfoldEmbedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Hyvor\Unfold\Exception\EmbedUnableToResolveException;
use Hyvor\Unfold\Unfold;
use Hyvor\Unfold\UnfoldConfig;
use Hyvor\Unfold\UnfoldMethod;
Expand Down Expand Up @@ -61,4 +62,12 @@
method: UnfoldMethod::EMBED,
);
expect($response->embed)->toBe('<script src="https://gist.github.com/123.js"></script>');
});

it('on unable to resolve', function () {
expect(fn() => Unfold::unfold(
'https://hyvor.com',
method: UnfoldMethod::EMBED,
config: new UnfoldConfig()
))->toThrow(EmbedUnableToResolveException::class);
});
Loading