aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/http/MetadataRetrieverTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/http/MetadataRetrieverTest.php')
-rw-r--r--tests/http/MetadataRetrieverTest.php123
1 files changed, 123 insertions, 0 deletions
diff --git a/tests/http/MetadataRetrieverTest.php b/tests/http/MetadataRetrieverTest.php
new file mode 100644
index 00000000..2a1838e8
--- /dev/null
+++ b/tests/http/MetadataRetrieverTest.php
@@ -0,0 +1,123 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Http;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Config\ConfigManager;
9
10class MetadataRetrieverTest extends TestCase
11{
12 /** @var MetadataRetriever */
13 protected $retriever;
14
15 /** @var ConfigManager */
16 protected $conf;
17
18 /** @var HttpAccess */
19 protected $httpAccess;
20
21 public function setUp(): void
22 {
23 $this->conf = $this->createMock(ConfigManager::class);
24 $this->httpAccess = $this->createMock(HttpAccess::class);
25 $this->retriever = new MetadataRetriever($this->conf, $this->httpAccess);
26
27 $this->conf->method('get')->willReturnCallback(function (string $param, $default) {
28 return $default === null ? $param : $default;
29 });
30 }
31
32 /**
33 * Test metadata retrieve() with values returned
34 */
35 public function testFullRetrieval(): void
36 {
37 $url = 'https://domain.tld/link';
38 $remoteTitle = 'Remote Title ';
39 $remoteDesc = 'Sometimes the meta description is relevant.';
40 $remoteTags = 'abc def';
41
42 $expectedResult = [
43 'title' => $remoteTitle,
44 'description' => $remoteDesc,
45 'tags' => $remoteTags,
46 ];
47
48 $this->httpAccess
49 ->expects(static::once())
50 ->method('getCurlDownloadCallback')
51 ->willReturnCallback(
52 function (&$charset, &$title, &$description, &$tags) use (
53 $remoteTitle,
54 $remoteDesc,
55 $remoteTags
56 ): callable {
57 return function () use (
58 &$charset,
59 &$title,
60 &$description,
61 &$tags,
62 $remoteTitle,
63 $remoteDesc,
64 $remoteTags
65 ): void {
66 $charset = 'ISO-8859-1';
67 $title = $remoteTitle;
68 $description = $remoteDesc;
69 $tags = $remoteTags;
70 };
71 }
72 )
73 ;
74 $this->httpAccess
75 ->expects(static::once())
76 ->method('getHttpResponse')
77 ->with($url, 30, 4194304)
78 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
79 $callback();
80 })
81 ;
82
83 $result = $this->retriever->retrieve($url);
84
85 static::assertSame($expectedResult, $result);
86 }
87
88 /**
89 * Test metadata retrieve() without any value
90 */
91 public function testEmptyRetrieval(): void
92 {
93 $url = 'https://domain.tld/link';
94
95 $expectedResult = [
96 'title' => null,
97 'description' => null,
98 'tags' => null,
99 ];
100
101 $this->httpAccess
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 {};
107 }
108 )
109 ;
110 $this->httpAccess
111 ->expects(static::once())
112 ->method('getHttpResponse')
113 ->with($url, 30, 4194304)
114 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
115 $callback();
116 })
117 ;
118
119 $result = $this->retriever->retrieve($url);
120
121 static::assertSame($expectedResult, $result);
122 }
123}