Skip to content

Commit

Permalink
Merge pull request #9 from hyvor/more-tests
Browse files Browse the repository at this point in the history
testing done
  • Loading branch information
supun-io authored Oct 22, 2024
2 parents 076723c + aca8752 commit c7e0530
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 28 deletions.
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 @@ -43,7 +36,7 @@ public static function parse(
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);
}

/**
Expand All @@ -56,14 +49,6 @@ public static function getUnfoldedObject(
) {
$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,
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 @@ public function __construct(
* 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 @@ -76,9 +77,11 @@ public function parse(): EmbedResponseObject
} elseif ($this instanceof EmbedParserCustomInterface) {
return $this->parseCustom();
} else {
// @codeCoverageIgnoreStart
throw new \Exception(
'EmbedParserAbstract should be implemented with either EmbedParserOEmbedInterface or EmbedParserCustomInterface'
); // @codeCoverageIgnore
);
// @codeCoverageIgnoreEnd
}
}

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);
});

0 comments on commit c7e0530

Please sign in to comment.