2018-04-15 02:05:41 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\MetadataResolver;
|
|
|
|
|
|
|
|
use App\MetadataResolver\OGPResolver;
|
2019-09-10 22:34:15 +09:00
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
2018-04-15 02:05:41 +09:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class OGPResolverTest extends TestCase
|
|
|
|
{
|
2019-02-17 03:19:27 +09:00
|
|
|
use CreateMockedResolver;
|
|
|
|
|
2019-09-10 22:34:15 +09:00
|
|
|
public function testMissingUrl()
|
|
|
|
{
|
|
|
|
$this->createResolver(OGPResolver::class, '', [], 404);
|
|
|
|
|
|
|
|
$this->expectException(BadResponseException::class);
|
|
|
|
$this->resolver->resolve('http://example.com/404');
|
|
|
|
}
|
|
|
|
|
2018-04-15 02:05:41 +09:00
|
|
|
public function testResolve()
|
|
|
|
{
|
2019-02-17 03:19:27 +09:00
|
|
|
$response = <<< 'HTML'
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head prefix="og: http://ogp.me/ns#">
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>The Open Graph protocol</title>
|
|
|
|
<meta name="description" content="The Open Graph protocol enables any web page to become a rich object in a social graph.">
|
|
|
|
<link rel="stylesheet" href="base.css" type="text/css">
|
|
|
|
<meta property="og:title" content="Open Graph protocol">
|
|
|
|
<meta property="og:type" content="website">
|
|
|
|
<meta property="og:url" content="http://ogp.me/">
|
|
|
|
<meta property="og:image" content="http://ogp.me/logo.png">
|
|
|
|
<meta property="og:image:type" content="image/png">
|
|
|
|
<meta property="og:image:width" content="300">
|
|
|
|
<meta property="og:image:height" content="300">
|
|
|
|
<meta property="og:image:alt" content="The Open Graph logo">
|
|
|
|
<meta property="og:description" content="The Open Graph protocol enables any web page to become a rich object in a social graph.">
|
|
|
|
<meta prefix="fb: http://ogp.me/ns/fb#" property="fb:app_id" content="115190258555800">
|
|
|
|
<link rel="alternate" type="application/rdf+xml" href="http://ogp.me/ns/ogp.me.rdf">
|
|
|
|
<link rel="alternate" type="text/turtle" href="http://ogp.me/ns/ogp.me.ttl">
|
|
|
|
</head>
|
|
|
|
<body></body>
|
|
|
|
</html>
|
|
|
|
HTML;
|
|
|
|
$this->createResolver(OGPResolver::class, $response);
|
2018-04-15 02:05:41 +09:00
|
|
|
|
2019-02-17 03:19:27 +09:00
|
|
|
$resolver = $this->createResolver(OGPResolver::class, $response);
|
2018-04-15 02:05:41 +09:00
|
|
|
$metadata = $resolver->resolve('http://ogp.me');
|
|
|
|
$this->assertEquals('Open Graph protocol', $metadata->title);
|
|
|
|
$this->assertEquals('The Open Graph protocol enables any web page to become a rich object in a social graph.', $metadata->description);
|
|
|
|
$this->assertEquals('http://ogp.me/logo.png', $metadata->image);
|
|
|
|
}
|
2019-01-23 00:04:10 +09:00
|
|
|
|
|
|
|
public function testResolveTitleOnly()
|
|
|
|
{
|
2019-02-17 03:19:27 +09:00
|
|
|
$response = <<< 'HTML'
|
|
|
|
<!doctype html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Example Domain</title>
|
|
|
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
|
|
</head>
|
|
|
|
<body></body>
|
|
|
|
</html>
|
|
|
|
HTML;
|
|
|
|
$this->createResolver(OGPResolver::class, $response);
|
2019-01-23 00:04:10 +09:00
|
|
|
|
2019-02-17 03:19:27 +09:00
|
|
|
$metadata = $this->resolver->resolve('http://example.com');
|
2019-01-23 00:04:10 +09:00
|
|
|
$this->assertEquals('Example Domain', $metadata->title);
|
|
|
|
$this->assertEmpty($metadata->description);
|
|
|
|
$this->assertEmpty($metadata->image);
|
|
|
|
}
|
2019-02-12 22:45:51 +09:00
|
|
|
|
|
|
|
public function testResolveTitleAndDescription()
|
|
|
|
{
|
2019-02-17 03:19:27 +09:00
|
|
|
$resolver = $this->app->make(OGPResolver::class);
|
2019-02-12 22:45:51 +09:00
|
|
|
|
|
|
|
$html = <<<EOF
|
|
|
|
<title>Welcome to my homepage</title>
|
|
|
|
<meta name="description" content="This is my super hyper ultra homepage!!" />
|
|
|
|
EOF;
|
|
|
|
|
|
|
|
$metadata = $resolver->parse($html);
|
|
|
|
$this->assertEquals('Welcome to my homepage', $metadata->title);
|
|
|
|
$this->assertEquals('This is my super hyper ultra homepage!!', $metadata->description);
|
|
|
|
$this->assertEmpty($metadata->image);
|
|
|
|
}
|
2018-04-15 02:05:41 +09:00
|
|
|
}
|