1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
<?php
declare(strict_types = 1);
namespace Embed\Tests;
use Brick\VarExporter\VarExporter;
use DateTime;
use Embed\Embed;
use Embed\Extractor;
use Embed\ExtractorFactory;
use Embed\Http\Crawler;
use JsonSerializable;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface;
abstract class PagesTestCase extends TestCase
{
const CACHE = 0;
const FIXTURES = 0;
private const DETECTORS = [
'authorName',
'authorUrl',
'cms',
'code',
'description',
'favicon',
'feeds',
'icon',
'image',
'keywords',
'language',
'languages',
'license',
'providerName',
'providerUrl',
'publishedTime',
'redirect',
'title',
'url',
];
private static Embed $embed;
private static function getEmbed(): Embed
{
if (isset(self::$embed)) {
return self::$embed;
}
$dispatcher = new FileClient(__DIR__.'/cache');
$dispatcher->setMode(static::CACHE);
return self::$embed = new Embed(new Crawler($dispatcher), self::getExtractorFactory());
}
protected function assertEmbed(string $url)
{
$embed = self::getEmbed();
$extractor = $embed->get($url);
$uri = $extractor->getRequest()->getUri();
$data = self::getData($extractor);
$expected = self::readData($uri);
if (!$expected || static::FIXTURES === 1) {
self::writeData($uri, $data);
echo PHP_EOL."Save fixture: {$url}";
$this->markTestSkipped('Skipped assertion for '.$url);
} else {
$this->assertEquals($expected, $data, $url);
}
}
private static function writeData(UriInterface $uri, array $data): void
{
$filename = __DIR__.'/fixtures/'.FileClient::getFileName($uri);
file_put_contents(
$filename,
sprintf("<?php\ndeclare(strict_types = 1);\n\nreturn %s;\n", VarExporter::export($data))
);
}
private static function readData(UriInterface $uri): ?array
{
$filename = __DIR__.'/fixtures/'.FileClient::getFileName($uri);
if (is_file($filename)) {
return require $filename;
}
return null;
}
private static function getData(Extractor $extractor): array
{
$data = [];
foreach (self::DETECTORS as $name) {
$data[$name] = self::convert($extractor->$name);
}
$data['linkedData'] = $extractor->getLinkedData()->all();
$data['oEmbed'] = $extractor->getOEmbed()->all();
if (method_exists($extractor, 'getApi')) {
$data['api'] = $extractor->getApi()->all();
}
$data['allLinkedData'] = $extractor->getLinkedData()->getAll();
return $data;
}
private static function convert($value)
{
if (!$value) {
return $value;
}
if ($value instanceof JsonSerializable) {
return $value->jsonSerialize();
}
if ($value instanceof UriInterface) {
return (string) $value;
}
if ($value instanceof DateTime) {
return $value->format('Y-m-d H:i:s');
}
if (is_array($value)) {
return array_map(__METHOD__, $value);
}
return $value;
}
private static function getExtractorFactory()
{
$settings = [
'instagram:token' => $_ENV['INSTAGRAM_TOKEN'] ?? null,
'facebook:token' => $_ENV['FACEBOOK_TOKEN'] ?? null,
];
return new ExtractorFactory($settings);
}
}
|