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';
43 'title' => $remoteTitle,
44 'description' => $remoteDesc,
45 'tags' => $remoteTags,
49 ->expects(static::once())
50 ->method('getCurlDownloadCallback')
52 function (&$charset, &$title, &$description, &$tags) use (
57 return function () use (
66 $charset = 'ISO-8859-1';
67 $title = $remoteTitle;
68 $description = $remoteDesc;
75 ->expects(static::once())
76 ->method('getHttpResponse')
77 ->with($url, 30, 4194304)
78 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
83 $result = $this->retriever
->retrieve($url);
85 static::assertSame($expectedResult, $result);
89 * Test metadata retrieve() without any value
91 public function testEmptyRetrieval(): void
93 $url = 'https://domain.tld/link';
97 'description' => null,
102 ->expects(static::once())
103 ->method('getCurlDownloadCallback')
104 ->willReturnCallback(
105 function (&$charset, &$title, &$description, &$tags): callable
{
106 return function () use (&$charset, &$title, &$description, &$tags): void {};
111 ->expects(static::once())
112 ->method('getHttpResponse')
113 ->with($url, 30, 4194304)
114 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
119 $result = $this->retriever
->retrieve($url);
121 static::assertSame($expectedResult, $result);