2020-09-25 13:29:36 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shaarli\Http;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shaarli\Config\ConfigManager;
|
|
|
|
|
|
|
|
class MetadataRetrieverTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var MetadataRetriever */
|
|
|
|
protected $retriever;
|
|
|
|
|
|
|
|
/** @var ConfigManager */
|
|
|
|
protected $conf;
|
|
|
|
|
|
|
|
/** @var HttpAccess */
|
|
|
|
protected $httpAccess;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->conf = $this->createMock(ConfigManager::class);
|
|
|
|
$this->httpAccess = $this->createMock(HttpAccess::class);
|
|
|
|
$this->retriever = new MetadataRetriever($this->conf, $this->httpAccess);
|
|
|
|
|
|
|
|
$this->conf->method('get')->willReturnCallback(function (string $param, $default) {
|
|
|
|
return $default === null ? $param : $default;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test metadata retrieve() with values returned
|
|
|
|
*/
|
|
|
|
public function testFullRetrieval(): void
|
|
|
|
{
|
|
|
|
$url = 'https://domain.tld/link';
|
|
|
|
$remoteTitle = 'Remote Title ';
|
|
|
|
$remoteDesc = 'Sometimes the meta description is relevant.';
|
|
|
|
$remoteTags = 'abc def';
|
2020-10-15 11:20:33 +02:00
|
|
|
$remoteCharset = 'utf-8';
|
2020-09-25 13:29:36 +02:00
|
|
|
|
|
|
|
$expectedResult = [
|
|
|
|
'title' => $remoteTitle,
|
|
|
|
'description' => $remoteDesc,
|
|
|
|
'tags' => $remoteTags,
|
|
|
|
];
|
|
|
|
|
2020-10-15 11:20:33 +02:00
|
|
|
$this->httpAccess
|
|
|
|
->expects(static::once())
|
|
|
|
->method('getCurlHeaderCallback')
|
|
|
|
->willReturnCallback(
|
|
|
|
function (&$charset) use (
|
|
|
|
$remoteCharset
|
|
|
|
): callable {
|
|
|
|
return function () use (
|
|
|
|
&$charset,
|
|
|
|
$remoteCharset
|
|
|
|
): void {
|
|
|
|
$charset = $remoteCharset;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
;
|
2020-09-25 13:29:36 +02:00
|
|
|
$this->httpAccess
|
|
|
|
->expects(static::once())
|
|
|
|
->method('getCurlDownloadCallback')
|
|
|
|
->willReturnCallback(
|
|
|
|
function (&$charset, &$title, &$description, &$tags) use (
|
2020-10-15 11:20:33 +02:00
|
|
|
$remoteCharset,
|
2020-09-25 13:29:36 +02:00
|
|
|
$remoteTitle,
|
|
|
|
$remoteDesc,
|
|
|
|
$remoteTags
|
|
|
|
): callable {
|
|
|
|
return function () use (
|
|
|
|
&$charset,
|
|
|
|
&$title,
|
|
|
|
&$description,
|
|
|
|
&$tags,
|
2020-10-15 11:20:33 +02:00
|
|
|
$remoteCharset,
|
2020-09-25 13:29:36 +02:00
|
|
|
$remoteTitle,
|
|
|
|
$remoteDesc,
|
|
|
|
$remoteTags
|
|
|
|
): void {
|
2020-10-15 11:20:33 +02:00
|
|
|
static::assertSame($remoteCharset, $charset);
|
|
|
|
|
2020-09-25 13:29:36 +02:00
|
|
|
$title = $remoteTitle;
|
|
|
|
$description = $remoteDesc;
|
|
|
|
$tags = $remoteTags;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
;
|
|
|
|
$this->httpAccess
|
|
|
|
->expects(static::once())
|
|
|
|
->method('getHttpResponse')
|
|
|
|
->with($url, 30, 4194304)
|
2020-10-15 11:20:33 +02:00
|
|
|
->willReturnCallback(function($url, $timeout, $maxBytes, $headerCallback, $dlCallback): void {
|
|
|
|
$headerCallback();
|
|
|
|
$dlCallback();
|
2020-09-25 13:29:36 +02:00
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
$result = $this->retriever->retrieve($url);
|
|
|
|
|
|
|
|
static::assertSame($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test metadata retrieve() without any value
|
|
|
|
*/
|
|
|
|
public function testEmptyRetrieval(): void
|
|
|
|
{
|
|
|
|
$url = 'https://domain.tld/link';
|
|
|
|
|
|
|
|
$expectedResult = [
|
|
|
|
'title' => null,
|
|
|
|
'description' => null,
|
|
|
|
'tags' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->httpAccess
|
|
|
|
->expects(static::once())
|
|
|
|
->method('getCurlDownloadCallback')
|
|
|
|
->willReturnCallback(
|
2020-10-15 11:20:33 +02:00
|
|
|
function (): callable {
|
|
|
|
return function (): void {};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
;
|
|
|
|
$this->httpAccess
|
|
|
|
->expects(static::once())
|
|
|
|
->method('getCurlHeaderCallback')
|
|
|
|
->willReturnCallback(
|
|
|
|
function (): callable {
|
|
|
|
return function (): void {};
|
2020-09-25 13:29:36 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
;
|
|
|
|
$this->httpAccess
|
|
|
|
->expects(static::once())
|
|
|
|
->method('getHttpResponse')
|
|
|
|
->with($url, 30, 4194304)
|
2020-10-15 11:20:33 +02:00
|
|
|
->willReturnCallback(function($url, $timeout, $maxBytes, $headerCallback, $dlCallback): void {
|
|
|
|
$headerCallback();
|
|
|
|
$dlCallback();
|
2020-09-25 13:29:36 +02:00
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
$result = $this->retriever->retrieve($url);
|
|
|
|
|
|
|
|
static::assertSame($expectedResult, $result);
|
|
|
|
}
|
|
|
|
}
|