3 declare(strict_types
=1);
5 namespace Shaarli\Http
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Config\ConfigManager
;
10 class MetadataRetrieverTest
extends TestCase
12 /** @var MetadataRetriever */
15 /** @var ConfigManager */
18 /** @var HttpAccess */
19 protected $httpAccess;
21 public function setUp(): void
23 $this->conf
= $this->createMock(ConfigManager
::class);
24 $this->httpAccess
= $this->createMock(HttpAccess
::class);
25 $this->retriever
= new MetadataRetriever($this->conf
, $this->httpAccess
);
27 $this->conf
->method('get')->willReturnCallback(function (string $param, $default) {
28 return $default === null ? $param : $default;
33 * Test metadata retrieve() with values returned
35 public function testFullRetrieval(): void
37 $url = 'https://domain.tld/link';
38 $remoteTitle = 'Remote Title ';
39 $remoteDesc = 'Sometimes the meta description is relevant.';
40 $remoteTags = 'abc def';
41 $remoteCharset = 'utf-8';
44 'title' => trim($remoteTitle),
45 'description' => $remoteDesc,
46 'tags' => $remoteTags,
50 ->expects(static::once())
51 ->method('getCurlHeaderCallback')
53 function (&$charset) use (
56 return function () use (
60 $charset = $remoteCharset;
66 ->expects(static::once())
67 ->method('getCurlDownloadCallback')
69 function (&$charset, &$title, &$description, &$tags) use (
75 return function () use (
85 static::assertSame($remoteCharset, $charset);
87 $title = $remoteTitle;
88 $description = $remoteDesc;
95 ->expects(static::once())
96 ->method('getHttpResponse')
97 ->with($url, 30, 4194304)
98 ->willReturnCallback(function($url, $timeout, $maxBytes, $headerCallback, $dlCallback): void {
104 $result = $this->retriever
->retrieve($url);
106 static::assertSame($expectedResult, $result);
110 * Test metadata retrieve() without any value
112 public function testEmptyRetrieval(): void
114 $url = 'https://domain.tld/link';
118 'description' => null,
123 ->expects(static::once())
124 ->method('getCurlDownloadCallback')
125 ->willReturnCallback(
126 function (): callable
{
127 return function (): void {};
132 ->expects(static::once())
133 ->method('getCurlHeaderCallback')
134 ->willReturnCallback(
135 function (): callable
{
136 return function (): void {};
141 ->expects(static::once())
142 ->method('getHttpResponse')
143 ->with($url, 30, 4194304)
144 ->willReturnCallback(function($url, $timeout, $maxBytes, $headerCallback, $dlCallback): void {
150 $result = $this->retriever
->retrieve($url);
152 static::assertSame($expectedResult, $result);