diff options
17 files changed, 560 insertions, 479 deletions
diff --git a/app/AppKernel.php b/app/AppKernel.php index 7d19e9ab..4a54da29 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php | |||
@@ -34,6 +34,7 @@ class AppKernel extends Kernel | |||
34 | new FOS\JsRoutingBundle\FOSJsRoutingBundle(), | 34 | new FOS\JsRoutingBundle\FOSJsRoutingBundle(), |
35 | new BD\GuzzleSiteAuthenticatorBundle\BDGuzzleSiteAuthenticatorBundle(), | 35 | new BD\GuzzleSiteAuthenticatorBundle\BDGuzzleSiteAuthenticatorBundle(), |
36 | new OldSound\RabbitMqBundle\OldSoundRabbitMqBundle(), | 36 | new OldSound\RabbitMqBundle\OldSoundRabbitMqBundle(), |
37 | new Http\HttplugBundle\HttplugBundle(), | ||
37 | 38 | ||
38 | // wallabag bundles | 39 | // wallabag bundles |
39 | new Wallabag\CoreBundle\WallabagCoreBundle(), | 40 | new Wallabag\CoreBundle\WallabagCoreBundle(), |
diff --git a/app/config/config.yml b/app/config/config.yml index 078f277a..bbcc682f 100644 --- a/app/config/config.yml +++ b/app/config/config.yml | |||
@@ -370,3 +370,25 @@ jms_serializer: | |||
370 | sensio_framework_extra: | 370 | sensio_framework_extra: |
371 | router: | 371 | router: |
372 | annotations: false | 372 | annotations: false |
373 | |||
374 | httplug: | ||
375 | clients: | ||
376 | wallabag_core: | ||
377 | factory: 'wallabag_core.http_client_factory' | ||
378 | config: | ||
379 | defaults: | ||
380 | timeout: 10 | ||
381 | plugins: ['httplug.plugin.logger'] | ||
382 | wallabag_core.entry.download_images: | ||
383 | factory: 'httplug.factory.auto' | ||
384 | plugins: ['httplug.plugin.logger'] | ||
385 | wallabag_import.pocket.client: | ||
386 | factory: 'httplug.factory.auto' | ||
387 | plugins: | ||
388 | - 'httplug.plugin.logger' | ||
389 | - header_defaults: | ||
390 | headers: | ||
391 | 'content-type': 'application/json' | ||
392 | 'X-Accept': 'application/json' | ||
393 | discovery: | ||
394 | client: false | ||
diff --git a/composer.json b/composer.json index b1c144c7..55e7f765 100644 --- a/composer.json +++ b/composer.json | |||
@@ -66,8 +66,9 @@ | |||
66 | "simplepie/simplepie": "~1.5", | 66 | "simplepie/simplepie": "~1.5", |
67 | "willdurand/hateoas-bundle": "~1.3", | 67 | "willdurand/hateoas-bundle": "~1.3", |
68 | "liip/theme-bundle": "^1.4.6", | 68 | "liip/theme-bundle": "^1.4.6", |
69 | "lexik/form-filter-bundle": "^5.0", | 69 | "lexik/form-filter-bundle": "^5.0.4", |
70 | "j0k3r/graby": "^1.0", | 70 | "j0k3r/graby": "^2.0", |
71 | "php-http/guzzle5-adapter": "^2.0", | ||
71 | "friendsofsymfony/user-bundle": "2.0.*", | 72 | "friendsofsymfony/user-bundle": "2.0.*", |
72 | "friendsofsymfony/oauth-server-bundle": "^1.5", | 73 | "friendsofsymfony/oauth-server-bundle": "^1.5", |
73 | "stof/doctrine-extensions-bundle": "^1.2", | 74 | "stof/doctrine-extensions-bundle": "^1.2", |
@@ -89,7 +90,8 @@ | |||
89 | "bdunogier/guzzle-site-authenticator": "^1.0.0", | 90 | "bdunogier/guzzle-site-authenticator": "^1.0.0", |
90 | "defuse/php-encryption": "^2.1", | 91 | "defuse/php-encryption": "^2.1", |
91 | "html2text/html2text": "^4.1", | 92 | "html2text/html2text": "^4.1", |
92 | "pragmarx/recovery": "^0.1.0" | 93 | "pragmarx/recovery": "^0.1.0", |
94 | "php-http/httplug-bundle": "^1.14" | ||
93 | }, | 95 | }, |
94 | "require-dev": { | 96 | "require-dev": { |
95 | "doctrine/doctrine-fixtures-bundle": "~3.0", | 97 | "doctrine/doctrine-fixtures-bundle": "~3.0", |
@@ -101,7 +103,9 @@ | |||
101 | "phpstan/phpstan": "^0.11.0", | 103 | "phpstan/phpstan": "^0.11.0", |
102 | "phpstan/phpstan-phpunit": "^0.11.0", | 104 | "phpstan/phpstan-phpunit": "^0.11.0", |
103 | "phpstan/phpstan-symfony": "^0.11.0", | 105 | "phpstan/phpstan-symfony": "^0.11.0", |
104 | "phpstan/phpstan-doctrine": "^0.11.0" | 106 | "phpstan/phpstan-doctrine": "^0.11.0", |
107 | "php-http/mock-client": "^1.0", | ||
108 | "guzzlehttp/psr7": "^1.0" | ||
105 | }, | 109 | }, |
106 | "suggest": { | 110 | "suggest": { |
107 | "ext-imagick": "To keep GIF animation when downloading image is enabled" | 111 | "ext-imagick": "To keep GIF animation when downloading image is enabled" |
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index bdd02129..aaacdcdc 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -365,9 +365,7 @@ class EntryRestController extends WallabagRestController | |||
365 | 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), | 365 | 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), |
366 | 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), | 366 | 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), |
367 | // faking the open graph preview picture | 367 | // faking the open graph preview picture |
368 | 'open_graph' => [ | 368 | 'image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(), |
369 | 'og_image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(), | ||
370 | ], | ||
371 | 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), | 369 | 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), |
372 | ] | 370 | ] |
373 | ); | 371 | ); |
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index bc257ffb..c6fa0d98 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php | |||
@@ -12,8 +12,8 @@ use Wallabag\CoreBundle\Entity\Entry; | |||
12 | use Wallabag\CoreBundle\Tools\Utils; | 12 | use Wallabag\CoreBundle\Tools\Utils; |
13 | 13 | ||
14 | /** | 14 | /** |
15 | * This kind of proxy class take care of getting the content from an url | 15 | * This kind of proxy class takes care of getting the content from an url |
16 | * and update the entry with what it found. | 16 | * and updates the entry with what it found. |
17 | */ | 17 | */ |
18 | class ContentProxy | 18 | class ContentProxy |
19 | { | 19 | { |
@@ -54,7 +54,11 @@ class ContentProxy | |||
54 | 54 | ||
55 | if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) { | 55 | if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) { |
56 | $fetchedContent = $this->graby->fetchContent($url); | 56 | $fetchedContent = $this->graby->fetchContent($url); |
57 | $fetchedContent['title'] = $this->sanitizeContentTitle($fetchedContent['title'], $fetchedContent['content_type']); | 57 | |
58 | $fetchedContent['title'] = $this->sanitizeContentTitle( | ||
59 | $fetchedContent['title'], | ||
60 | isset($fetchedContent['headers']['content-type']) ? $fetchedContent['headers']['content-type'] : '' | ||
61 | ); | ||
58 | 62 | ||
59 | // when content is imported, we have information in $content | 63 | // when content is imported, we have information in $content |
60 | // in case fetching content goes bad, we'll keep the imported information instead of overriding them | 64 | // in case fetching content goes bad, we'll keep the imported information instead of overriding them |
@@ -188,8 +192,8 @@ class ContentProxy | |||
188 | /** | 192 | /** |
189 | * Try to sanitize the title of the fetched content from wrong character encodings and invalid UTF-8 character. | 193 | * Try to sanitize the title of the fetched content from wrong character encodings and invalid UTF-8 character. |
190 | * | 194 | * |
191 | * @param $title | 195 | * @param string $title |
192 | * @param $contentType | 196 | * @param string $contentType |
193 | * | 197 | * |
194 | * @return string | 198 | * @return string |
195 | */ | 199 | */ |
@@ -253,16 +257,14 @@ class ContentProxy | |||
253 | 257 | ||
254 | if (!empty($content['title'])) { | 258 | if (!empty($content['title'])) { |
255 | $entry->setTitle($content['title']); | 259 | $entry->setTitle($content['title']); |
256 | } elseif (!empty($content['open_graph']['og_title'])) { | ||
257 | $entry->setTitle($content['open_graph']['og_title']); | ||
258 | } | 260 | } |
259 | 261 | ||
260 | if (empty($content['html'])) { | 262 | if (empty($content['html'])) { |
261 | $content['html'] = $this->fetchingErrorMessage; | 263 | $content['html'] = $this->fetchingErrorMessage; |
262 | 264 | ||
263 | if (!empty($content['open_graph']['og_description'])) { | 265 | if (!empty($content['description'])) { |
264 | $content['html'] .= '<p><i>But we found a short description: </i></p>'; | 266 | $content['html'] .= '<p><i>But we found a short description: </i></p>'; |
265 | $content['html'] .= $content['open_graph']['og_description']; | 267 | $content['html'] .= $content['description']; |
266 | } | 268 | } |
267 | } | 269 | } |
268 | 270 | ||
@@ -277,8 +279,8 @@ class ContentProxy | |||
277 | $entry->setPublishedBy($content['authors']); | 279 | $entry->setPublishedBy($content['authors']); |
278 | } | 280 | } |
279 | 281 | ||
280 | if (!empty($content['all_headers']) && $this->storeArticleHeaders) { | 282 | if (!empty($content['headers'])) { |
281 | $entry->setHeaders($content['all_headers']); | 283 | $entry->setHeaders($content['headers']); |
282 | } | 284 | } |
283 | 285 | ||
284 | if (!empty($content['date'])) { | 286 | if (!empty($content['date'])) { |
@@ -289,17 +291,30 @@ class ContentProxy | |||
289 | $this->updateLanguage($entry, $content['language']); | 291 | $this->updateLanguage($entry, $content['language']); |
290 | } | 292 | } |
291 | 293 | ||
292 | if (!empty($content['open_graph']['og_image'])) { | 294 | $previewPictureUrl = ''; |
293 | $this->updatePreviewPicture($entry, $content['open_graph']['og_image']); | 295 | if (!empty($content['image'])) { |
296 | $previewPictureUrl = $content['image']; | ||
294 | } | 297 | } |
295 | 298 | ||
296 | // if content is an image, define it as a preview too | 299 | // if content is an image, define it as a preview too |
297 | if (!empty($content['content_type']) && \in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { | 300 | if (!empty($content['headers']['content-type']) && \in_array($this->mimeGuesser->guess($content['headers']['content-type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { |
298 | $this->updatePreviewPicture($entry, $content['url']); | 301 | $previewPictureUrl = $content['url']; |
302 | } elseif (empty($previewPictureUrl)) { | ||
303 | $this->logger->debug('Extracting images from content to provide a default preview picture'); | ||
304 | $imagesUrls = DownloadImages::extractImagesUrlsFromHtml($content['html']); | ||
305 | $this->logger->debug(\count($imagesUrls) . ' pictures found'); | ||
306 | |||
307 | if (!empty($imagesUrls)) { | ||
308 | $previewPictureUrl = $imagesUrls[0]; | ||
309 | } | ||
310 | } | ||
311 | |||
312 | if (!empty($content['headers']['content-type'])) { | ||
313 | $entry->setMimetype($content['headers']['content-type']); | ||
299 | } | 314 | } |
300 | 315 | ||
301 | if (!empty($content['content_type'])) { | 316 | if (!empty($previewPictureUrl)) { |
302 | $entry->setMimetype($content['content_type']); | 317 | $this->updatePreviewPicture($entry, $previewPictureUrl); |
303 | } | 318 | } |
304 | 319 | ||
305 | try { | 320 | try { |
diff --git a/src/Wallabag/CoreBundle/Helper/DownloadImages.php b/src/Wallabag/CoreBundle/Helper/DownloadImages.php index 9a7e9828..7a39a2e4 100644 --- a/src/Wallabag/CoreBundle/Helper/DownloadImages.php +++ b/src/Wallabag/CoreBundle/Helper/DownloadImages.php | |||
@@ -2,8 +2,13 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | 3 | namespace Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use GuzzleHttp\Client; | 5 | use Http\Client\Common\HttpMethodsClient; |
6 | use GuzzleHttp\Message\Response; | 6 | use Http\Client\Common\Plugin\ErrorPlugin; |
7 | use Http\Client\Common\PluginClient; | ||
8 | use Http\Client\HttpClient; | ||
9 | use Http\Discovery\MessageFactoryDiscovery; | ||
10 | use Http\Message\MessageFactory; | ||
11 | use Psr\Http\Message\ResponseInterface; | ||
7 | use Psr\Log\LoggerInterface; | 12 | use Psr\Log\LoggerInterface; |
8 | use Symfony\Component\DomCrawler\Crawler; | 13 | use Symfony\Component\DomCrawler\Crawler; |
9 | use Symfony\Component\Finder\Finder; | 14 | use Symfony\Component\Finder\Finder; |
@@ -19,9 +24,9 @@ class DownloadImages | |||
19 | private $mimeGuesser; | 24 | private $mimeGuesser; |
20 | private $wallabagUrl; | 25 | private $wallabagUrl; |
21 | 26 | ||
22 | public function __construct(Client $client, $baseFolder, $wallabagUrl, LoggerInterface $logger) | 27 | public function __construct(HttpClient $client, $baseFolder, $wallabagUrl, LoggerInterface $logger, MessageFactory $messageFactory = null) |
23 | { | 28 | { |
24 | $this->client = $client; | 29 | $this->client = new HttpMethodsClient(new PluginClient($client, [new ErrorPlugin()]), $messageFactory ?: MessageFactoryDiscovery::find()); |
25 | $this->baseFolder = $baseFolder; | 30 | $this->baseFolder = $baseFolder; |
26 | $this->wallabagUrl = rtrim($wallabagUrl, '/'); | 31 | $this->wallabagUrl = rtrim($wallabagUrl, '/'); |
27 | $this->logger = $logger; | 32 | $this->logger = $logger; |
@@ -31,23 +36,36 @@ class DownloadImages | |||
31 | } | 36 | } |
32 | 37 | ||
33 | /** | 38 | /** |
34 | * Process the html and extract image from it, save them to local and return the updated html. | 39 | * Process the html and extract images URLs from it. |
35 | * | 40 | * |
36 | * @param int $entryId ID of the entry | ||
37 | * @param string $html | 41 | * @param string $html |
38 | * @param string $url Used as a base path for relative image and folder | ||
39 | * | 42 | * |
40 | * @return string | 43 | * @return string[] |
41 | */ | 44 | */ |
42 | public function processHtml($entryId, $html, $url) | 45 | public static function extractImagesUrlsFromHtml($html) |
43 | { | 46 | { |
44 | $crawler = new Crawler($html); | 47 | $crawler = new Crawler($html); |
45 | $imagesCrawler = $crawler | 48 | $imagesCrawler = $crawler |
46 | ->filterXpath('//img'); | 49 | ->filterXpath('//img'); |
47 | $imagesUrls = $imagesCrawler | 50 | $imagesUrls = $imagesCrawler |
48 | ->extract(['src']); | 51 | ->extract(['src']); |
49 | $imagesSrcsetUrls = $this->getSrcsetUrls($imagesCrawler); | 52 | $imagesSrcsetUrls = self::getSrcsetUrls($imagesCrawler); |
50 | $imagesUrls = array_unique(array_merge($imagesUrls, $imagesSrcsetUrls)); | 53 | |
54 | return array_unique(array_merge($imagesUrls, $imagesSrcsetUrls)); | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * Process the html and extract image from it, save them to local and return the updated html. | ||
59 | * | ||
60 | * @param int $entryId ID of the entry | ||
61 | * @param string $html | ||
62 | * @param string $url Used as a base path for relative image and folder | ||
63 | * | ||
64 | * @return string | ||
65 | */ | ||
66 | public function processHtml($entryId, $html, $url) | ||
67 | { | ||
68 | $imagesUrls = self::extractImagesUrlsFromHtml($html); | ||
51 | 69 | ||
52 | $relativePath = $this->getRelativePath($entryId); | 70 | $relativePath = $this->getRelativePath($entryId); |
53 | 71 | ||
@@ -122,7 +140,7 @@ class DownloadImages | |||
122 | $localPath = $folderPath . '/' . $hashImage . '.' . $ext; | 140 | $localPath = $folderPath . '/' . $hashImage . '.' . $ext; |
123 | 141 | ||
124 | try { | 142 | try { |
125 | $im = imagecreatefromstring($res->getBody()); | 143 | $im = imagecreatefromstring((string) $res->getBody()); |
126 | } catch (\Exception $e) { | 144 | } catch (\Exception $e) { |
127 | $im = false; | 145 | $im = false; |
128 | } | 146 | } |
@@ -199,7 +217,7 @@ class DownloadImages | |||
199 | * | 217 | * |
200 | * @return array An array of urls | 218 | * @return array An array of urls |
201 | */ | 219 | */ |
202 | private function getSrcsetUrls(Crawler $imagesCrawler) | 220 | private static function getSrcsetUrls(Crawler $imagesCrawler) |
203 | { | 221 | { |
204 | $urls = []; | 222 | $urls = []; |
205 | $iterator = $imagesCrawler | 223 | $iterator = $imagesCrawler |
@@ -293,14 +311,14 @@ class DownloadImages | |||
293 | /** | 311 | /** |
294 | * Retrieve and validate the extension from the response of the url of the image. | 312 | * Retrieve and validate the extension from the response of the url of the image. |
295 | * | 313 | * |
296 | * @param Response $res Guzzle Response | 314 | * @param ResponseInterface $res Http Response |
297 | * @param string $imagePath Path from the src image from the content (used for log only) | 315 | * @param string $imagePath Path from the src image from the content (used for log only) |
298 | * | 316 | * |
299 | * @return string|false Extension name or false if validation failed | 317 | * @return string|false Extension name or false if validation failed |
300 | */ | 318 | */ |
301 | private function getExtensionFromResponse(Response $res, $imagePath) | 319 | private function getExtensionFromResponse(ResponseInterface $res, $imagePath) |
302 | { | 320 | { |
303 | $ext = $this->mimeGuesser->guess($res->getHeader('content-type')); | 321 | $ext = $this->mimeGuesser->guess(current($res->getHeader('content-type'))); |
304 | $this->logger->debug('DownloadImages: Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]); | 322 | $this->logger->debug('DownloadImages: Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]); |
305 | 323 | ||
306 | // ok header doesn't have the extension, try a different way | 324 | // ok header doesn't have the extension, try a different way |
diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php index 4602a684..b8e95381 100644 --- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | |||
@@ -2,16 +2,18 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | 3 | namespace Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use Graby\Ring\Client\SafeCurlHandler; | 5 | use GuzzleHttp\Client as GuzzleClient; |
6 | use GuzzleHttp\Client; | ||
7 | use GuzzleHttp\Cookie\CookieJar; | 6 | use GuzzleHttp\Cookie\CookieJar; |
8 | use GuzzleHttp\Event\SubscriberInterface; | 7 | use GuzzleHttp\Event\SubscriberInterface; |
8 | use Http\Adapter\Guzzle5\Client as GuzzleAdapter; | ||
9 | use Http\Client\HttpClient; | ||
10 | use Http\HttplugBundle\ClientFactory\ClientFactory; | ||
9 | use Psr\Log\LoggerInterface; | 11 | use Psr\Log\LoggerInterface; |
10 | 12 | ||
11 | /** | 13 | /** |
12 | * Builds and configures the Guzzle HTTP client. | 14 | * Builds and configures the HTTP client. |
13 | */ | 15 | */ |
14 | class HttpClientFactory | 16 | class HttpClientFactory implements ClientFactory |
15 | { | 17 | { |
16 | /** @var [\GuzzleHttp\Event\SubscriberInterface] */ | 18 | /** @var [\GuzzleHttp\Event\SubscriberInterface] */ |
17 | private $subscribers = []; | 19 | private $subscribers = []; |
@@ -37,35 +39,42 @@ class HttpClientFactory | |||
37 | } | 39 | } |
38 | 40 | ||
39 | /** | 41 | /** |
40 | * @return \GuzzleHttp\Client|null | 42 | * Adds a subscriber to the HTTP client. |
43 | * | ||
44 | * @param SubscriberInterface $subscriber | ||
45 | */ | ||
46 | public function addSubscriber(SubscriberInterface $subscriber) | ||
47 | { | ||
48 | $this->subscribers[] = $subscriber; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * Input an array of configuration to be able to create a HttpClient. | ||
53 | * | ||
54 | * @param array $config | ||
55 | * | ||
56 | * @return HttpClient | ||
41 | */ | 57 | */ |
42 | public function buildHttpClient() | 58 | public function createClient(array $config = []) |
43 | { | 59 | { |
44 | $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]); | 60 | $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]); |
45 | 61 | ||
46 | if (0 === (int) $this->restrictedAccess) { | 62 | if (0 === (int) $this->restrictedAccess) { |
47 | return; | 63 | return new GuzzleAdapter(new GuzzleClient($config)); |
48 | } | 64 | } |
49 | 65 | ||
50 | // we clear the cookie to avoid websites who use cookies for analytics | 66 | // we clear the cookie to avoid websites who use cookies for analytics |
51 | $this->cookieJar->clear(); | 67 | $this->cookieJar->clear(); |
52 | // need to set the (shared) cookie jar | 68 | if (!isset($config['defaults']['cookies'])) { |
53 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); | 69 | // need to set the (shared) cookie jar |
70 | $config['defaults']['cookies'] = $this->cookieJar; | ||
71 | } | ||
54 | 72 | ||
73 | $guzzle = new GuzzleClient($config); | ||
55 | foreach ($this->subscribers as $subscriber) { | 74 | foreach ($this->subscribers as $subscriber) { |
56 | $client->getEmitter()->attach($subscriber); | 75 | $guzzle->getEmitter()->attach($subscriber); |
57 | } | 76 | } |
58 | 77 | ||
59 | return $client; | 78 | return new GuzzleAdapter($guzzle); |
60 | } | ||
61 | |||
62 | /** | ||
63 | * Adds a subscriber to the HTTP client. | ||
64 | * | ||
65 | * @param SubscriberInterface $subscriber | ||
66 | */ | ||
67 | public function addSubscriber(SubscriberInterface $subscriber) | ||
68 | { | ||
69 | $this->subscribers[] = $subscriber; | ||
70 | } | 79 | } |
71 | } | 80 | } |
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 280d779d..31986951 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -42,7 +42,7 @@ services: | |||
42 | - | 42 | - |
43 | error_message: '%wallabag_core.fetching_error_message%' | 43 | error_message: '%wallabag_core.fetching_error_message%' |
44 | error_message_title: '%wallabag_core.fetching_error_message_title%' | 44 | error_message_title: '%wallabag_core.fetching_error_message_title%' |
45 | - "@wallabag_core.guzzle.http_client" | 45 | - "@wallabag_core.http_client" |
46 | - "@wallabag_core.graby.config_builder" | 46 | - "@wallabag_core.graby.config_builder" |
47 | calls: | 47 | calls: |
48 | - [ setLogger, [ "@logger" ] ] | 48 | - [ setLogger, [ "@logger" ] ] |
@@ -55,9 +55,8 @@ services: | |||
55 | - {} | 55 | - {} |
56 | - "@logger" | 56 | - "@logger" |
57 | 57 | ||
58 | wallabag_core.guzzle.http_client: | 58 | wallabag_core.http_client: |
59 | class: GuzzleHttp\ClientInterface | 59 | alias: 'httplug.client.wallabag_core' |
60 | factory: ["@wallabag_core.guzzle.http_client_factory", buildHttpClient] | ||
61 | 60 | ||
62 | wallabag_core.guzzle_authenticator.config_builder: | 61 | wallabag_core.guzzle_authenticator.config_builder: |
63 | class: Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder | 62 | class: Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder |
@@ -73,7 +72,7 @@ services: | |||
73 | bd_guzzle_site_authenticator.site_config_builder: | 72 | bd_guzzle_site_authenticator.site_config_builder: |
74 | alias: wallabag_core.guzzle_authenticator.config_builder | 73 | alias: wallabag_core.guzzle_authenticator.config_builder |
75 | 74 | ||
76 | wallabag_core.guzzle.http_client_factory: | 75 | wallabag_core.http_client_factory: |
77 | class: Wallabag\CoreBundle\Helper\HttpClientFactory | 76 | class: Wallabag\CoreBundle\Helper\HttpClientFactory |
78 | arguments: | 77 | arguments: |
79 | - "@wallabag_core.guzzle.cookie_jar" | 78 | - "@wallabag_core.guzzle.cookie_jar" |
@@ -212,7 +211,7 @@ services: | |||
212 | - "@logger" | 211 | - "@logger" |
213 | 212 | ||
214 | wallabag_core.entry.download_images.client: | 213 | wallabag_core.entry.download_images.client: |
215 | class: GuzzleHttp\Client | 214 | alias: 'httplug.client.wallabag_core.entry.download_images' |
216 | 215 | ||
217 | wallabag_core.helper.crypto_proxy: | 216 | wallabag_core.helper.crypto_proxy: |
218 | class: Wallabag\CoreBundle\Helper\CryptoProxy | 217 | class: Wallabag\CoreBundle\Helper\CryptoProxy |
diff --git a/src/Wallabag/ImportBundle/Import/InstapaperImport.php b/src/Wallabag/ImportBundle/Import/InstapaperImport.php index 439c978c..f7bee9ef 100644 --- a/src/Wallabag/ImportBundle/Import/InstapaperImport.php +++ b/src/Wallabag/ImportBundle/Import/InstapaperImport.php | |||
@@ -93,6 +93,10 @@ class InstapaperImport extends AbstractImport | |||
93 | return false; | 93 | return false; |
94 | } | 94 | } |
95 | 95 | ||
96 | // most recent articles are first, which means we should create them at the end so they will show up first | ||
97 | // as Instapaper doesn't export the creation date of the article | ||
98 | $entries = array_reverse($entries); | ||
99 | |||
96 | if ($this->producer) { | 100 | if ($this->producer) { |
97 | $this->parseEntriesForProducer($entries); | 101 | $this->parseEntriesForProducer($entries); |
98 | 102 | ||
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index a39d8156..746120af 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php | |||
@@ -2,13 +2,22 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\Import; | 3 | namespace Wallabag\ImportBundle\Import; |
4 | 4 | ||
5 | use GuzzleHttp\Client; | 5 | use Http\Client\Common\HttpMethodsClient; |
6 | use GuzzleHttp\Exception\RequestException; | 6 | use Http\Client\Common\Plugin\ErrorPlugin; |
7 | use Http\Client\Common\PluginClient; | ||
8 | use Http\Client\Exception\RequestException; | ||
9 | use Http\Client\HttpClient; | ||
10 | use Http\Discovery\MessageFactoryDiscovery; | ||
11 | use Http\Message\MessageFactory; | ||
12 | use Psr\Http\Message\ResponseInterface; | ||
7 | use Wallabag\CoreBundle\Entity\Entry; | 13 | use Wallabag\CoreBundle\Entity\Entry; |
8 | 14 | ||
9 | class PocketImport extends AbstractImport | 15 | class PocketImport extends AbstractImport |
10 | { | 16 | { |
11 | const NB_ELEMENTS = 5000; | 17 | const NB_ELEMENTS = 5000; |
18 | /** | ||
19 | * @var HttpMethodsClient | ||
20 | */ | ||
12 | private $client; | 21 | private $client; |
13 | private $accessToken; | 22 | private $accessToken; |
14 | 23 | ||
@@ -55,24 +64,18 @@ class PocketImport extends AbstractImport | |||
55 | */ | 64 | */ |
56 | public function getRequestToken($redirectUri) | 65 | public function getRequestToken($redirectUri) |
57 | { | 66 | { |
58 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', | ||
59 | [ | ||
60 | 'body' => json_encode([ | ||
61 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
62 | 'redirect_uri' => $redirectUri, | ||
63 | ]), | ||
64 | ] | ||
65 | ); | ||
66 | |||
67 | try { | 67 | try { |
68 | $response = $this->client->send($request); | 68 | $response = $this->client->post('https://getpocket.com/v3/oauth/request', [], json_encode([ |
69 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
70 | 'redirect_uri' => $redirectUri, | ||
71 | ])); | ||
69 | } catch (RequestException $e) { | 72 | } catch (RequestException $e) { |
70 | $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); | 73 | $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); |
71 | 74 | ||
72 | return false; | 75 | return false; |
73 | } | 76 | } |
74 | 77 | ||
75 | return $response->json()['code']; | 78 | return $this->jsonDecode($response)['code']; |
76 | } | 79 | } |
77 | 80 | ||
78 | /** | 81 | /** |
@@ -85,24 +88,18 @@ class PocketImport extends AbstractImport | |||
85 | */ | 88 | */ |
86 | public function authorize($code) | 89 | public function authorize($code) |
87 | { | 90 | { |
88 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', | ||
89 | [ | ||
90 | 'body' => json_encode([ | ||
91 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
92 | 'code' => $code, | ||
93 | ]), | ||
94 | ] | ||
95 | ); | ||
96 | |||
97 | try { | 91 | try { |
98 | $response = $this->client->send($request); | 92 | $response = $this->client->post('https://getpocket.com/v3/oauth/authorize', [], json_encode([ |
93 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
94 | 'code' => $code, | ||
95 | ])); | ||
99 | } catch (RequestException $e) { | 96 | } catch (RequestException $e) { |
100 | $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]); | 97 | $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]); |
101 | 98 | ||
102 | return false; | 99 | return false; |
103 | } | 100 | } |
104 | 101 | ||
105 | $this->accessToken = $response->json()['access_token']; | 102 | $this->accessToken = $this->jsonDecode($response)['access_token']; |
106 | 103 | ||
107 | return true; | 104 | return true; |
108 | } | 105 | } |
@@ -114,29 +111,23 @@ class PocketImport extends AbstractImport | |||
114 | { | 111 | { |
115 | static $run = 0; | 112 | static $run = 0; |
116 | 113 | ||
117 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', | ||
118 | [ | ||
119 | 'body' => json_encode([ | ||
120 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
121 | 'access_token' => $this->accessToken, | ||
122 | 'detailType' => 'complete', | ||
123 | 'state' => 'all', | ||
124 | 'sort' => 'newest', | ||
125 | 'count' => self::NB_ELEMENTS, | ||
126 | 'offset' => $offset, | ||
127 | ]), | ||
128 | ] | ||
129 | ); | ||
130 | |||
131 | try { | 114 | try { |
132 | $response = $this->client->send($request); | 115 | $response = $this->client->post('https://getpocket.com/v3/get', [], json_encode([ |
116 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
117 | 'access_token' => $this->accessToken, | ||
118 | 'detailType' => 'complete', | ||
119 | 'state' => 'all', | ||
120 | 'sort' => 'newest', | ||
121 | 'count' => self::NB_ELEMENTS, | ||
122 | 'offset' => $offset, | ||
123 | ])); | ||
133 | } catch (RequestException $e) { | 124 | } catch (RequestException $e) { |
134 | $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]); | 125 | $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]); |
135 | 126 | ||
136 | return false; | 127 | return false; |
137 | } | 128 | } |
138 | 129 | ||
139 | $entries = $response->json(); | 130 | $entries = $this->jsonDecode($response); |
140 | 131 | ||
141 | if ($this->producer) { | 132 | if ($this->producer) { |
142 | $this->parseEntriesForProducer($entries['list']); | 133 | $this->parseEntriesForProducer($entries['list']); |
@@ -159,13 +150,14 @@ class PocketImport extends AbstractImport | |||
159 | } | 150 | } |
160 | 151 | ||
161 | /** | 152 | /** |
162 | * Set the Guzzle client. | 153 | * Set the Http client. |
163 | * | 154 | * |
164 | * @param Client $client | 155 | * @param HttpClient $client |
156 | * @param MessageFactory|null $messageFactory | ||
165 | */ | 157 | */ |
166 | public function setClient(Client $client) | 158 | public function setClient(HttpClient $client, MessageFactory $messageFactory = null) |
167 | { | 159 | { |
168 | $this->client = $client; | 160 | $this->client = new HttpMethodsClient(new PluginClient($client, [new ErrorPlugin()]), $messageFactory ?: MessageFactoryDiscovery::find()); |
169 | } | 161 | } |
170 | 162 | ||
171 | /** | 163 | /** |
@@ -252,4 +244,15 @@ class PocketImport extends AbstractImport | |||
252 | 244 | ||
253 | return $importedEntry; | 245 | return $importedEntry; |
254 | } | 246 | } |
247 | |||
248 | protected function jsonDecode(ResponseInterface $response) | ||
249 | { | ||
250 | $data = json_decode((string) $response->getBody(), true); | ||
251 | |||
252 | if (JSON_ERROR_NONE !== json_last_error()) { | ||
253 | throw new \InvalidArgumentException('Unable to parse JSON data: ' . json_last_error_msg()); | ||
254 | } | ||
255 | |||
256 | return $data; | ||
257 | } | ||
255 | } | 258 | } |
diff --git a/src/Wallabag/ImportBundle/Import/WallabagV2Import.php b/src/Wallabag/ImportBundle/Import/WallabagV2Import.php index 3e085ecf..2ba26003 100644 --- a/src/Wallabag/ImportBundle/Import/WallabagV2Import.php +++ b/src/Wallabag/ImportBundle/Import/WallabagV2Import.php | |||
@@ -35,7 +35,9 @@ class WallabagV2Import extends WallabagImport | |||
35 | { | 35 | { |
36 | return [ | 36 | return [ |
37 | 'html' => $entry['content'], | 37 | 'html' => $entry['content'], |
38 | 'content_type' => $entry['mimetype'], | 38 | 'headers' => [ |
39 | 'content-type' => $entry['mimetype'], | ||
40 | ], | ||
39 | 'is_archived' => (bool) ($entry['is_archived'] || $this->markAsRead), | 41 | 'is_archived' => (bool) ($entry['is_archived'] || $this->markAsRead), |
40 | 'is_starred' => (bool) $entry['is_starred'], | 42 | 'is_starred' => (bool) $entry['is_starred'], |
41 | ] + $entry; | 43 | ] + $entry; |
diff --git a/src/Wallabag/ImportBundle/Resources/config/services.yml b/src/Wallabag/ImportBundle/Resources/config/services.yml index 2dd7dff8..973c0d03 100644 --- a/src/Wallabag/ImportBundle/Resources/config/services.yml +++ b/src/Wallabag/ImportBundle/Resources/config/services.yml | |||
@@ -7,13 +7,7 @@ services: | |||
7 | class: Wallabag\ImportBundle\Import\ImportChain | 7 | class: Wallabag\ImportBundle\Import\ImportChain |
8 | 8 | ||
9 | wallabag_import.pocket.client: | 9 | wallabag_import.pocket.client: |
10 | class: GuzzleHttp\Client | 10 | alias: 'httplug.client.wallabag_import.pocket.client' |
11 | arguments: | ||
12 | - | ||
13 | defaults: | ||
14 | headers: | ||
15 | content-type: "application/json" | ||
16 | X-Accept: "application/json" | ||
17 | 11 | ||
18 | wallabag_import.pocket.import: | 12 | wallabag_import.pocket.import: |
19 | class: Wallabag\ImportBundle\Import\PocketImport | 13 | class: Wallabag\ImportBundle\Import\PocketImport |
diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index caa8929d..9dee9891 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php | |||
@@ -236,7 +236,7 @@ class EntryControllerTest extends WallabagCoreTestCase | |||
236 | $this->logInAs('admin'); | 236 | $this->logInAs('admin'); |
237 | $client = $this->getClient(); | 237 | $client = $this->getClient(); |
238 | 238 | ||
239 | $url = 'http://www.aritylabs.com/post/106091708292/des-contr%C3%B4leurs-optionnels-gr%C3%A2ce-%C3%A0-constmissing'; | 239 | $url = 'https://www.aritylabs.com/post/106091708292/des-contr%C3%B4leurs-optionnels-gr%C3%A2ce-%C3%A0-constmissing'; |
240 | 240 | ||
241 | $crawler = $client->request('GET', '/new'); | 241 | $crawler = $client->request('GET', '/new'); |
242 | 242 | ||
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index 508adb1b..9ce72c79 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | |||
@@ -36,7 +36,9 @@ class ContentProxyTest extends TestCase | |||
36 | 'html' => false, | 36 | 'html' => false, |
37 | 'title' => '', | 37 | 'title' => '', |
38 | 'url' => '', | 38 | 'url' => '', |
39 | 'content_type' => '', | 39 | 'headers' => [ |
40 | 'content-type' => '', | ||
41 | ], | ||
40 | 'language' => '', | 42 | 'language' => '', |
41 | ]); | 43 | ]); |
42 | 44 | ||
@@ -71,7 +73,9 @@ class ContentProxyTest extends TestCase | |||
71 | 'html' => false, | 73 | 'html' => false, |
72 | 'title' => '', | 74 | 'title' => '', |
73 | 'url' => '', | 75 | 'url' => '', |
74 | 'content_type' => '', | 76 | 'headers' => [ |
77 | 'content-type' => '', | ||
78 | ], | ||
75 | 'language' => '', | 79 | 'language' => '', |
76 | ]); | 80 | ]); |
77 | 81 | ||
@@ -104,15 +108,14 @@ class ContentProxyTest extends TestCase | |||
104 | ->method('fetchContent') | 108 | ->method('fetchContent') |
105 | ->willReturn([ | 109 | ->willReturn([ |
106 | 'html' => false, | 110 | 'html' => false, |
107 | 'title' => '', | 111 | 'title' => 'my title', |
108 | 'url' => '', | 112 | 'url' => '', |
109 | 'content_type' => '', | 113 | 'headers' => [ |
114 | 'content-type' => '', | ||
115 | ], | ||
110 | 'language' => '', | 116 | 'language' => '', |
111 | 'status' => '', | 117 | 'status' => '', |
112 | 'open_graph' => [ | 118 | 'description' => 'desc', |
113 | 'og_title' => 'my title', | ||
114 | 'og_description' => 'desc', | ||
115 | ], | ||
116 | ]); | 119 | ]); |
117 | 120 | ||
118 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); | 121 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
@@ -147,13 +150,12 @@ class ContentProxyTest extends TestCase | |||
147 | 'html' => str_repeat('this is my content', 325), | 150 | 'html' => str_repeat('this is my content', 325), |
148 | 'title' => 'this is my title', | 151 | 'title' => 'this is my title', |
149 | 'url' => 'http://1.1.1.1', | 152 | 'url' => 'http://1.1.1.1', |
150 | 'content_type' => 'text/html', | ||
151 | 'language' => 'fr', | 153 | 'language' => 'fr', |
152 | 'status' => '200', | 154 | 'status' => '200', |
153 | 'open_graph' => [ | 155 | 'description' => 'OG desc', |
154 | 'og_title' => 'my OG title', | 156 | 'image' => 'http://3.3.3.3/cover.jpg', |
155 | 'og_description' => 'OG desc', | 157 | 'headers' => [ |
156 | 'og_image' => 'http://3.3.3.3/cover.jpg', | 158 | 'content-type' => 'text/html', |
157 | ], | 159 | ], |
158 | ]); | 160 | ]); |
159 | 161 | ||
@@ -189,13 +191,12 @@ class ContentProxyTest extends TestCase | |||
189 | 'html' => str_repeat('this is my content', 325), | 191 | 'html' => str_repeat('this is my content', 325), |
190 | 'title' => 'this is my title', | 192 | 'title' => 'this is my title', |
191 | 'url' => 'http://1.1.1.1', | 193 | 'url' => 'http://1.1.1.1', |
192 | 'content_type' => 'text/html', | ||
193 | 'language' => 'fr', | 194 | 'language' => 'fr', |
194 | 'status' => '200', | 195 | 'status' => '200', |
195 | 'open_graph' => [ | 196 | 'description' => 'OG desc', |
196 | 'og_title' => 'my OG title', | 197 | 'image' => null, |
197 | 'og_description' => 'OG desc', | 198 | 'headers' => [ |
198 | 'og_image' => null, | 199 | 'content-type' => 'text/html', |
199 | ], | 200 | ], |
200 | ]); | 201 | ]); |
201 | 202 | ||
@@ -214,6 +215,86 @@ class ContentProxyTest extends TestCase | |||
214 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | 215 | $this->assertSame('1.1.1.1', $entry->getDomainName()); |
215 | } | 216 | } |
216 | 217 | ||
218 | public function testWithContentAndContentImage() | ||
219 | { | ||
220 | $tagger = $this->getTaggerMock(); | ||
221 | $tagger->expects($this->once()) | ||
222 | ->method('tag'); | ||
223 | |||
224 | $graby = $this->getMockBuilder('Graby\Graby') | ||
225 | ->setMethods(['fetchContent']) | ||
226 | ->disableOriginalConstructor() | ||
227 | ->getMock(); | ||
228 | |||
229 | $graby->expects($this->any()) | ||
230 | ->method('fetchContent') | ||
231 | ->willReturn([ | ||
232 | 'html' => "<h1>Test</h1><p><img src='http://3.3.3.3/cover.jpg'/></p>", | ||
233 | 'title' => 'this is my title', | ||
234 | 'url' => 'http://1.1.1.1', | ||
235 | 'headers' => [ | ||
236 | 'content-type' => 'text/html', | ||
237 | ], | ||
238 | 'language' => 'fr', | ||
239 | 'status' => '200', | ||
240 | 'image' => null, | ||
241 | ]); | ||
242 | |||
243 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); | ||
244 | $entry = new Entry(new User()); | ||
245 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
246 | |||
247 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); | ||
248 | $this->assertSame('this is my title', $entry->getTitle()); | ||
249 | $this->assertSame("<h1>Test</h1><p><img src='http://3.3.3.3/cover.jpg'/></p>", $entry->getContent()); | ||
250 | $this->assertSame('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); | ||
251 | $this->assertSame('text/html', $entry->getMimetype()); | ||
252 | $this->assertSame('fr', $entry->getLanguage()); | ||
253 | $this->assertSame('200', $entry->getHttpStatus()); | ||
254 | $this->assertSame(0.0, $entry->getReadingTime()); | ||
255 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | ||
256 | } | ||
257 | |||
258 | public function testWithContentImageAndOgImage() | ||
259 | { | ||
260 | $tagger = $this->getTaggerMock(); | ||
261 | $tagger->expects($this->once()) | ||
262 | ->method('tag'); | ||
263 | |||
264 | $graby = $this->getMockBuilder('Graby\Graby') | ||
265 | ->setMethods(['fetchContent']) | ||
266 | ->disableOriginalConstructor() | ||
267 | ->getMock(); | ||
268 | |||
269 | $graby->expects($this->any()) | ||
270 | ->method('fetchContent') | ||
271 | ->willReturn([ | ||
272 | 'html' => "<h1>Test</h1><p><img src='http://3.3.3.3/nevermind.jpg'/></p>", | ||
273 | 'title' => 'this is my title', | ||
274 | 'url' => 'http://1.1.1.1', | ||
275 | 'headers' => [ | ||
276 | 'content-type' => 'text/html', | ||
277 | ], | ||
278 | 'language' => 'fr', | ||
279 | 'status' => '200', | ||
280 | 'image' => 'http://3.3.3.3/cover.jpg', | ||
281 | ]); | ||
282 | |||
283 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); | ||
284 | $entry = new Entry(new User()); | ||
285 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
286 | |||
287 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); | ||
288 | $this->assertSame('this is my title', $entry->getTitle()); | ||
289 | $this->assertSame("<h1>Test</h1><p><img src='http://3.3.3.3/nevermind.jpg'/></p>", $entry->getContent()); | ||
290 | $this->assertSame('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); | ||
291 | $this->assertSame('text/html', $entry->getMimetype()); | ||
292 | $this->assertSame('fr', $entry->getLanguage()); | ||
293 | $this->assertSame('200', $entry->getHttpStatus()); | ||
294 | $this->assertSame(0.0, $entry->getReadingTime()); | ||
295 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | ||
296 | } | ||
297 | |||
217 | public function testWithContentAndBadLanguage() | 298 | public function testWithContentAndBadLanguage() |
218 | { | 299 | { |
219 | $tagger = $this->getTaggerMock(); | 300 | $tagger = $this->getTaggerMock(); |
@@ -236,9 +317,11 @@ class ContentProxyTest extends TestCase | |||
236 | 'html' => str_repeat('this is my content', 325), | 317 | 'html' => str_repeat('this is my content', 325), |
237 | 'title' => 'this is my title', | 318 | 'title' => 'this is my title', |
238 | 'url' => 'http://1.1.1.1', | 319 | 'url' => 'http://1.1.1.1', |
239 | 'content_type' => 'text/html', | ||
240 | 'language' => 'dontexist', | 320 | 'language' => 'dontexist', |
241 | 'status' => '200', | 321 | 'status' => '200', |
322 | 'headers' => [ | ||
323 | 'content-type' => 'text/html', | ||
324 | ], | ||
242 | ]); | 325 | ]); |
243 | 326 | ||
244 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); | 327 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); |
@@ -280,14 +363,13 @@ class ContentProxyTest extends TestCase | |||
280 | 'html' => str_repeat('this is my content', 325), | 363 | 'html' => str_repeat('this is my content', 325), |
281 | 'title' => 'this is my title', | 364 | 'title' => 'this is my title', |
282 | 'url' => 'http://1.1.1.1', | 365 | 'url' => 'http://1.1.1.1', |
283 | 'content_type' => 'text/html', | 366 | 'headers' => [ |
367 | 'content-type' => 'text/html', | ||
368 | ], | ||
284 | 'language' => 'fr', | 369 | 'language' => 'fr', |
285 | 'status' => '200', | 370 | 'status' => '200', |
286 | 'open_graph' => [ | 371 | 'description' => 'OG desc', |
287 | 'og_title' => 'my OG title', | 372 | 'image' => 'https://', |
288 | 'og_description' => 'OG desc', | ||
289 | 'og_image' => 'https://', | ||
290 | ], | ||
291 | ]); | 373 | ]); |
292 | 374 | ||
293 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); | 375 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); |
@@ -320,12 +402,12 @@ class ContentProxyTest extends TestCase | |||
320 | 'html' => str_repeat('this is my content', 325), | 402 | 'html' => str_repeat('this is my content', 325), |
321 | 'title' => 'this is my title', | 403 | 'title' => 'this is my title', |
322 | 'url' => 'http://1.1.1.1', | 404 | 'url' => 'http://1.1.1.1', |
323 | 'content_type' => 'text/html', | ||
324 | 'language' => 'fr', | 405 | 'language' => 'fr', |
325 | 'date' => '1395635872', | 406 | 'date' => '1395635872', |
326 | 'authors' => ['Jeremy', 'Nico', 'Thomas'], | 407 | 'authors' => ['Jeremy', 'Nico', 'Thomas'], |
327 | 'all_headers' => [ | 408 | 'headers' => [ |
328 | 'Cache-Control' => 'no-cache', | 409 | 'cache-control' => 'no-cache', |
410 | 'content-type' => 'text/html', | ||
329 | ], | 411 | ], |
330 | ] | 412 | ] |
331 | ); | 413 | ); |
@@ -363,9 +445,11 @@ class ContentProxyTest extends TestCase | |||
363 | 'html' => str_repeat('this is my content', 325), | 445 | 'html' => str_repeat('this is my content', 325), |
364 | 'title' => 'this is my title', | 446 | 'title' => 'this is my title', |
365 | 'url' => 'http://1.1.1.1', | 447 | 'url' => 'http://1.1.1.1', |
366 | 'content_type' => 'text/html', | ||
367 | 'language' => 'fr', | 448 | 'language' => 'fr', |
368 | 'date' => '2016-09-08T11:55:58+0200', | 449 | 'date' => '2016-09-08T11:55:58+0200', |
450 | 'headers' => [ | ||
451 | 'content-type' => 'text/html', | ||
452 | ], | ||
369 | ] | 453 | ] |
370 | ); | 454 | ); |
371 | 455 | ||
@@ -398,9 +482,11 @@ class ContentProxyTest extends TestCase | |||
398 | 'html' => str_repeat('this is my content', 325), | 482 | 'html' => str_repeat('this is my content', 325), |
399 | 'title' => 'this is my title', | 483 | 'title' => 'this is my title', |
400 | 'url' => 'http://1.1.1.1', | 484 | 'url' => 'http://1.1.1.1', |
401 | 'content_type' => 'text/html', | ||
402 | 'language' => 'fr', | 485 | 'language' => 'fr', |
403 | 'date' => '01 02 2012', | 486 | 'date' => '01 02 2012', |
487 | 'headers' => [ | ||
488 | 'content-type' => 'text/html', | ||
489 | ], | ||
404 | ] | 490 | ] |
405 | ); | 491 | ); |
406 | 492 | ||
@@ -415,7 +501,7 @@ class ContentProxyTest extends TestCase | |||
415 | 501 | ||
416 | $records = $handler->getRecords(); | 502 | $records = $handler->getRecords(); |
417 | 503 | ||
418 | $this->assertCount(1, $records); | 504 | $this->assertCount(3, $records); |
419 | $this->assertContains('Error while defining date', $records[0]['message']); | 505 | $this->assertContains('Error while defining date', $records[0]['message']); |
420 | } | 506 | } |
421 | 507 | ||
@@ -435,8 +521,10 @@ class ContentProxyTest extends TestCase | |||
435 | 'html' => str_repeat('this is my content', 325), | 521 | 'html' => str_repeat('this is my content', 325), |
436 | 'title' => 'this is my title', | 522 | 'title' => 'this is my title', |
437 | 'url' => 'http://1.1.1.1', | 523 | 'url' => 'http://1.1.1.1', |
438 | 'content_type' => 'text/html', | ||
439 | 'language' => 'fr', | 524 | 'language' => 'fr', |
525 | 'headers' => [ | ||
526 | 'content-type' => 'text/html', | ||
527 | ], | ||
440 | ] | 528 | ] |
441 | ); | 529 | ); |
442 | 530 | ||
@@ -475,13 +563,13 @@ class ContentProxyTest extends TestCase | |||
475 | 'html' => $html, | 563 | 'html' => $html, |
476 | 'title' => 'this is my title', | 564 | 'title' => 'this is my title', |
477 | 'url' => 'http://1.1.1.1', | 565 | 'url' => 'http://1.1.1.1', |
478 | 'content_type' => 'text/html', | ||
479 | 'language' => 'fr', | 566 | 'language' => 'fr', |
480 | 'status' => '200', | 567 | 'status' => '200', |
481 | 'open_graph' => [ | 568 | //'og_title' => 'my OG title', |
482 | 'og_title' => 'my OG title', | 569 | 'description' => 'OG desc', |
483 | 'og_description' => 'OG desc', | 570 | 'image' => 'http://3.3.3.3/cover.jpg', |
484 | 'og_image' => 'http://3.3.3.3/cover.jpg', | 571 | 'headers' => [ |
572 | 'content-type' => 'text/html', | ||
485 | ], | 573 | ], |
486 | ] | 574 | ] |
487 | ); | 575 | ); |
@@ -513,9 +601,10 @@ class ContentProxyTest extends TestCase | |||
513 | 'html' => '<p><img src="http://1.1.1.1/image.jpg" /></p>', | 601 | 'html' => '<p><img src="http://1.1.1.1/image.jpg" /></p>', |
514 | 'title' => 'this is my title', | 602 | 'title' => 'this is my title', |
515 | 'url' => 'http://1.1.1.1/image.jpg', | 603 | 'url' => 'http://1.1.1.1/image.jpg', |
516 | 'content_type' => 'image/jpeg', | ||
517 | 'status' => '200', | 604 | 'status' => '200', |
518 | 'open_graph' => [], | 605 | 'headers' => [ |
606 | 'content-type' => 'image/jpeg', | ||
607 | ], | ||
519 | ]); | 608 | ]); |
520 | 609 | ||
521 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); | 610 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
@@ -553,7 +642,9 @@ class ContentProxyTest extends TestCase | |||
553 | 'html' => false, | 642 | 'html' => false, |
554 | 'title' => $actualTitle, | 643 | 'title' => $actualTitle, |
555 | 'url' => '', | 644 | 'url' => '', |
556 | 'content_type' => 'text/html', | 645 | 'headers' => [ |
646 | 'content-type' => 'text/html', | ||
647 | ], | ||
557 | 'language' => '', | 648 | 'language' => '', |
558 | ]); | 649 | ]); |
559 | 650 | ||
@@ -588,7 +679,9 @@ class ContentProxyTest extends TestCase | |||
588 | 'html' => false, | 679 | 'html' => false, |
589 | 'title' => $actualTitle, | 680 | 'title' => $actualTitle, |
590 | 'url' => '', | 681 | 'url' => '', |
591 | 'content_type' => 'text/html', | 682 | 'headers' => [ |
683 | 'content-type' => 'text/html', | ||
684 | ], | ||
592 | 'language' => '', | 685 | 'language' => '', |
593 | ]); | 686 | ]); |
594 | 687 | ||
@@ -622,7 +715,9 @@ class ContentProxyTest extends TestCase | |||
622 | 'html' => false, | 715 | 'html' => false, |
623 | 'title' => $actualTitle, | 716 | 'title' => $actualTitle, |
624 | 'url' => '', | 717 | 'url' => '', |
625 | 'content_type' => 'application/pdf', | 718 | 'headers' => [ |
719 | 'content-type' => 'application/pdf', | ||
720 | ], | ||
626 | 'language' => '', | 721 | 'language' => '', |
627 | ]); | 722 | ]); |
628 | 723 | ||
@@ -656,7 +751,9 @@ class ContentProxyTest extends TestCase | |||
656 | 'html' => false, | 751 | 'html' => false, |
657 | 'title' => $actualTitle, | 752 | 'title' => $actualTitle, |
658 | 'url' => '', | 753 | 'url' => '', |
659 | 'content_type' => 'application/pdf', | 754 | 'headers' => [ |
755 | 'content-type' => 'application/pdf', | ||
756 | ], | ||
660 | 'language' => '', | 757 | 'language' => '', |
661 | ]); | 758 | ]); |
662 | 759 | ||
@@ -690,7 +787,9 @@ class ContentProxyTest extends TestCase | |||
690 | 'html' => false, | 787 | 'html' => false, |
691 | 'title' => $actualTitle, | 788 | 'title' => $actualTitle, |
692 | 'url' => '', | 789 | 'url' => '', |
693 | 'content_type' => 'application/pdf', | 790 | 'headers' => [ |
791 | 'content-type' => 'application/pdf', | ||
792 | ], | ||
694 | 'language' => '', | 793 | 'language' => '', |
695 | ]); | 794 | ]); |
696 | 795 | ||
@@ -725,7 +824,9 @@ class ContentProxyTest extends TestCase | |||
725 | 'html' => false, | 824 | 'html' => false, |
726 | 'title' => $actualTitle, | 825 | 'title' => $actualTitle, |
727 | 'url' => '', | 826 | 'url' => '', |
728 | 'content_type' => 'application/pdf', | 827 | 'headers' => [ |
828 | 'content-type' => 'application/pdf', | ||
829 | ], | ||
729 | 'language' => '', | 830 | 'language' => '', |
730 | ]); | 831 | ]); |
731 | 832 | ||
@@ -855,7 +956,9 @@ class ContentProxyTest extends TestCase | |||
855 | 'html' => false, | 956 | 'html' => false, |
856 | 'title' => '', | 957 | 'title' => '', |
857 | 'url' => $content_url, | 958 | 'url' => $content_url, |
858 | 'content_type' => '', | 959 | 'headers' => [ |
960 | 'content-type' => '', | ||
961 | ], | ||
859 | 'language' => '', | 962 | 'language' => '', |
860 | ], | 963 | ], |
861 | true | 964 | true |
@@ -886,7 +989,9 @@ class ContentProxyTest extends TestCase | |||
886 | } | 989 | } |
887 | 990 | ||
888 | /** | 991 | /** |
889 | * https://stackoverflow.com/a/18506801. | 992 | * Convert hex to string. |
993 | * | ||
994 | * @see https://stackoverflow.com/a/18506801 | ||
890 | * | 995 | * |
891 | * @param $hex | 996 | * @param $hex |
892 | * | 997 | * |
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php index cda5f843..3c720425 100644 --- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | |||
@@ -2,10 +2,8 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use GuzzleHttp\Client; | 5 | use GuzzleHttp\Psr7\Response; |
6 | use GuzzleHttp\Message\Response; | 6 | use Http\Mock\Client as HttpMockClient; |
7 | use GuzzleHttp\Stream\Stream; | ||
8 | use GuzzleHttp\Subscriber\Mock; | ||
9 | use Monolog\Handler\TestHandler; | 7 | use Monolog\Handler\TestHandler; |
10 | use Monolog\Logger; | 8 | use Monolog\Logger; |
11 | use PHPUnit\Framework\TestCase; | 9 | use PHPUnit\Framework\TestCase; |
@@ -32,18 +30,14 @@ class DownloadImagesTest extends TestCase | |||
32 | */ | 30 | */ |
33 | public function testProcessHtml($html, $url) | 31 | public function testProcessHtml($html, $url) |
34 | { | 32 | { |
35 | $client = new Client(); | 33 | $httpMockClient = new HttpMockClient(); |
36 | 34 | ||
37 | $mock = new Mock([ | 35 | $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))); |
38 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))), | ||
39 | ]); | ||
40 | |||
41 | $client->getEmitter()->attach($mock); | ||
42 | 36 | ||
43 | $logHandler = new TestHandler(); | 37 | $logHandler = new TestHandler(); |
44 | $logger = new Logger('test', [$logHandler]); | 38 | $logger = new Logger('test', [$logHandler]); |
45 | 39 | ||
46 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 40 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
47 | 41 | ||
48 | $res = $download->processHtml(123, $html, $url); | 42 | $res = $download->processHtml(123, $html, $url); |
49 | 43 | ||
@@ -53,18 +47,13 @@ class DownloadImagesTest extends TestCase | |||
53 | 47 | ||
54 | public function testProcessHtmlWithBadImage() | 48 | public function testProcessHtmlWithBadImage() |
55 | { | 49 | { |
56 | $client = new Client(); | 50 | $httpMockClient = new HttpMockClient(); |
57 | 51 | $httpMockClient->addResponse(new Response(200, ['content-type' => 'application/json'], '')); | |
58 | $mock = new Mock([ | ||
59 | new Response(200, ['content-type' => 'application/json'], Stream::factory('')), | ||
60 | ]); | ||
61 | |||
62 | $client->getEmitter()->attach($mock); | ||
63 | 52 | ||
64 | $logHandler = new TestHandler(); | 53 | $logHandler = new TestHandler(); |
65 | $logger = new Logger('test', [$logHandler]); | 54 | $logger = new Logger('test', [$logHandler]); |
66 | 55 | ||
67 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 56 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
68 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); | 57 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); |
69 | 58 | ||
70 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); | 59 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); |
@@ -85,18 +74,13 @@ class DownloadImagesTest extends TestCase | |||
85 | */ | 74 | */ |
86 | public function testProcessSingleImage($header, $extension) | 75 | public function testProcessSingleImage($header, $extension) |
87 | { | 76 | { |
88 | $client = new Client(); | 77 | $httpMockClient = new HttpMockClient(); |
89 | 78 | $httpMockClient->addResponse(new Response(200, ['content-type' => $header], file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))); | |
90 | $mock = new Mock([ | ||
91 | new Response(200, ['content-type' => $header], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))), | ||
92 | ]); | ||
93 | |||
94 | $client->getEmitter()->attach($mock); | ||
95 | 79 | ||
96 | $logHandler = new TestHandler(); | 80 | $logHandler = new TestHandler(); |
97 | $logger = new Logger('test', [$logHandler]); | 81 | $logger = new Logger('test', [$logHandler]); |
98 | 82 | ||
99 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 83 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
100 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | 84 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
101 | 85 | ||
102 | $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res); | 86 | $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res); |
@@ -104,18 +88,13 @@ class DownloadImagesTest extends TestCase | |||
104 | 88 | ||
105 | public function testProcessSingleImageWithBadUrl() | 89 | public function testProcessSingleImageWithBadUrl() |
106 | { | 90 | { |
107 | $client = new Client(); | 91 | $httpMockClient = new HttpMockClient(); |
108 | 92 | $httpMockClient->addResponse(new Response(404, [])); | |
109 | $mock = new Mock([ | ||
110 | new Response(404, []), | ||
111 | ]); | ||
112 | |||
113 | $client->getEmitter()->attach($mock); | ||
114 | 93 | ||
115 | $logHandler = new TestHandler(); | 94 | $logHandler = new TestHandler(); |
116 | $logger = new Logger('test', [$logHandler]); | 95 | $logger = new Logger('test', [$logHandler]); |
117 | 96 | ||
118 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 97 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
119 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | 98 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
120 | 99 | ||
121 | $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); | 100 | $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); |
@@ -123,18 +102,13 @@ class DownloadImagesTest extends TestCase | |||
123 | 102 | ||
124 | public function testProcessSingleImageWithBadImage() | 103 | public function testProcessSingleImageWithBadImage() |
125 | { | 104 | { |
126 | $client = new Client(); | 105 | $httpMockClient = new HttpMockClient(); |
127 | 106 | $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], '')); | |
128 | $mock = new Mock([ | ||
129 | new Response(200, ['content-type' => 'image/png'], Stream::factory('')), | ||
130 | ]); | ||
131 | |||
132 | $client->getEmitter()->attach($mock); | ||
133 | 107 | ||
134 | $logHandler = new TestHandler(); | 108 | $logHandler = new TestHandler(); |
135 | $logger = new Logger('test', [$logHandler]); | 109 | $logger = new Logger('test', [$logHandler]); |
136 | 110 | ||
137 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 111 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
138 | $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | 112 | $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
139 | 113 | ||
140 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); | 114 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); |
@@ -142,18 +116,13 @@ class DownloadImagesTest extends TestCase | |||
142 | 116 | ||
143 | public function testProcessSingleImageFailAbsolute() | 117 | public function testProcessSingleImageFailAbsolute() |
144 | { | 118 | { |
145 | $client = new Client(); | 119 | $httpMockClient = new HttpMockClient(); |
146 | 120 | $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))); | |
147 | $mock = new Mock([ | ||
148 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))), | ||
149 | ]); | ||
150 | |||
151 | $client->getEmitter()->attach($mock); | ||
152 | 121 | ||
153 | $logHandler = new TestHandler(); | 122 | $logHandler = new TestHandler(); |
154 | $logger = new Logger('test', [$logHandler]); | 123 | $logger = new Logger('test', [$logHandler]); |
155 | 124 | ||
156 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 125 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
157 | $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY'); | 126 | $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY'); |
158 | 127 | ||
159 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); | 128 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); |
@@ -161,18 +130,13 @@ class DownloadImagesTest extends TestCase | |||
161 | 130 | ||
162 | public function testProcessRealImage() | 131 | public function testProcessRealImage() |
163 | { | 132 | { |
164 | $client = new Client(); | 133 | $httpMockClient = new HttpMockClient(); |
165 | 134 | $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))); | |
166 | $mock = new Mock([ | ||
167 | new Response(200, ['content-type' => null], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | ||
168 | ]); | ||
169 | |||
170 | $client->getEmitter()->attach($mock); | ||
171 | 135 | ||
172 | $logHandler = new TestHandler(); | 136 | $logHandler = new TestHandler(); |
173 | $logger = new Logger('test', [$logHandler]); | 137 | $logger = new Logger('test', [$logHandler]); |
174 | 138 | ||
175 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 139 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
176 | 140 | ||
177 | $res = $download->processSingleImage( | 141 | $res = $download->processSingleImage( |
178 | 123, | 142 | 123, |
@@ -186,20 +150,15 @@ class DownloadImagesTest extends TestCase | |||
186 | 150 | ||
187 | public function testProcessImageWithSrcset() | 151 | public function testProcessImageWithSrcset() |
188 | { | 152 | { |
189 | $client = new Client(); | 153 | $httpMockClient = new HttpMockClient(); |
190 | 154 | $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))); | |
191 | $mock = new Mock([ | 155 | $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))); |
192 | new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | 156 | $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))); |
193 | new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | ||
194 | new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | ||
195 | ]); | ||
196 | |||
197 | $client->getEmitter()->attach($mock); | ||
198 | 157 | ||
199 | $logHandler = new TestHandler(); | 158 | $logHandler = new TestHandler(); |
200 | $logger = new Logger('test', [$logHandler]); | 159 | $logger = new Logger('test', [$logHandler]); |
201 | 160 | ||
202 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 161 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
203 | $res = $download->processHtml(123, '<p><img class="alignnone wp-image-1153" src="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg" alt="" width="628" height="444" srcset="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg 530w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-768x543.jpg 768w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-900x636.jpg 900w" sizes="(max-width: 628px) 100vw, 628px" /></p>', 'http://piketty.blog.lemonde.fr/2017/10/12/budget-2018-la-jeunesse-sacrifiee/'); | 162 | $res = $download->processHtml(123, '<p><img class="alignnone wp-image-1153" src="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg" alt="" width="628" height="444" srcset="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg 530w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-768x543.jpg 768w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-900x636.jpg 900w" sizes="(max-width: 628px) 100vw, 628px" /></p>', 'http://piketty.blog.lemonde.fr/2017/10/12/budget-2018-la-jeunesse-sacrifiee/'); |
204 | 163 | ||
205 | $this->assertNotContains('http://piketty.blog.lemonde.fr/', $res, 'Image srcset attribute were not replaced'); | 164 | $this->assertNotContains('http://piketty.blog.lemonde.fr/', $res, 'Image srcset attribute were not replaced'); |
@@ -207,20 +166,15 @@ class DownloadImagesTest extends TestCase | |||
207 | 166 | ||
208 | public function testProcessImageWithTrickySrcset() | 167 | public function testProcessImageWithTrickySrcset() |
209 | { | 168 | { |
210 | $client = new Client(); | 169 | $httpMockClient = new HttpMockClient(); |
211 | 170 | $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))); | |
212 | $mock = new Mock([ | 171 | $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))); |
213 | new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | 172 | $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))); |
214 | new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | ||
215 | new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | ||
216 | ]); | ||
217 | |||
218 | $client->getEmitter()->attach($mock); | ||
219 | 173 | ||
220 | $logHandler = new TestHandler(); | 174 | $logHandler = new TestHandler(); |
221 | $logger = new Logger('test', [$logHandler]); | 175 | $logger = new Logger('test', [$logHandler]); |
222 | 176 | ||
223 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 177 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
224 | $res = $download->processHtml(123, '<figure id="post-257260" class="align-none media-257260"><img src="https://cdn.css-tricks.com/wp-content/uploads/2017/08/the-critical-request.png" srcset="https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_1000,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 1000w, https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_200,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 200w" sizes="(min-width: 1850px) calc( (100vw - 555px) / 3 ) | 178 | $res = $download->processHtml(123, '<figure id="post-257260" class="align-none media-257260"><img src="https://cdn.css-tricks.com/wp-content/uploads/2017/08/the-critical-request.png" srcset="https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_1000,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 1000w, https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_200,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 200w" sizes="(min-width: 1850px) calc( (100vw - 555px) / 3 ) |
225 | (min-width: 1251px) calc( (100vw - 530px) / 2 ) | 179 | (min-width: 1251px) calc( (100vw - 530px) / 2 ) |
226 | (min-width: 1086px) calc(100vw - 480px) | 180 | (min-width: 1086px) calc(100vw - 480px) |
@@ -232,18 +186,13 @@ class DownloadImagesTest extends TestCase | |||
232 | 186 | ||
233 | public function testProcessImageWithNullPath() | 187 | public function testProcessImageWithNullPath() |
234 | { | 188 | { |
235 | $client = new Client(); | 189 | $httpMockClient = new HttpMockClient(); |
236 | 190 | $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))); | |
237 | $mock = new Mock([ | ||
238 | new Response(200, ['content-type' => null], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | ||
239 | ]); | ||
240 | |||
241 | $client->getEmitter()->attach($mock); | ||
242 | 191 | ||
243 | $logHandler = new TestHandler(); | 192 | $logHandler = new TestHandler(); |
244 | $logger = new Logger('test', [$logHandler]); | 193 | $logger = new Logger('test', [$logHandler]); |
245 | 194 | ||
246 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 195 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
247 | 196 | ||
248 | $res = $download->processSingleImage( | 197 | $res = $download->processSingleImage( |
249 | 123, | 198 | 123, |
diff --git a/tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php b/tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php index 1f57939d..2a8e7c89 100644 --- a/tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php +++ b/tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php | |||
@@ -121,7 +121,7 @@ class WallabagV1ControllerTest extends WallabagCoreTestCase | |||
121 | 121 | ||
122 | $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content); | 122 | $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content); |
123 | $this->assertEmpty($content->getMimetype(), 'Mimetype for http://www.framablog.org is empty'); | 123 | $this->assertEmpty($content->getMimetype(), 'Mimetype for http://www.framablog.org is empty'); |
124 | $this->assertEmpty($content->getPreviewPicture(), 'Preview picture for http://www.framablog.org is empty'); | 124 | $this->assertSame($content->getPreviewPicture(), 'http://www.framablog.org/public/_img/framablog/wallaby_baby.jpg'); |
125 | $this->assertEmpty($content->getLanguage(), 'Language for http://www.framablog.org is empty'); | 125 | $this->assertEmpty($content->getLanguage(), 'Language for http://www.framablog.org is empty'); |
126 | 126 | ||
127 | $tags = $content->getTags(); | 127 | $tags = $content->getTags(); |
diff --git a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php index 8083f1a8..40e1626b 100644 --- a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php | |||
@@ -2,10 +2,8 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\ImportBundle\Import; | 3 | namespace Tests\Wallabag\ImportBundle\Import; |
4 | 4 | ||
5 | use GuzzleHttp\Client; | 5 | use GuzzleHttp\Psr7\Response; |
6 | use GuzzleHttp\Message\Response; | 6 | use Http\Mock\Client as HttpMockClient; |
7 | use GuzzleHttp\Stream\Stream; | ||
8 | use GuzzleHttp\Subscriber\Mock; | ||
9 | use M6Web\Component\RedisMock\RedisMockFactory; | 7 | use M6Web\Component\RedisMock\RedisMockFactory; |
10 | use Monolog\Handler\TestHandler; | 8 | use Monolog\Handler\TestHandler; |
11 | use Monolog\Logger; | 9 | use Monolog\Logger; |
@@ -38,16 +36,11 @@ class PocketImportTest extends TestCase | |||
38 | 36 | ||
39 | public function testOAuthRequest() | 37 | public function testOAuthRequest() |
40 | { | 38 | { |
41 | $client = new Client(); | 39 | $httpMockClient = new HttpMockClient(); |
42 | 40 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode(['code' => 'wunderbar_code']))); | |
43 | $mock = new Mock([ | ||
44 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar_code']))), | ||
45 | ]); | ||
46 | |||
47 | $client->getEmitter()->attach($mock); | ||
48 | 41 | ||
49 | $pocketImport = $this->getPocketImport(); | 42 | $pocketImport = $this->getPocketImport(); |
50 | $pocketImport->setClient($client); | 43 | $pocketImport->setClient($httpMockClient); |
51 | 44 | ||
52 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); | 45 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); |
53 | 46 | ||
@@ -56,16 +49,11 @@ class PocketImportTest extends TestCase | |||
56 | 49 | ||
57 | public function testOAuthRequestBadResponse() | 50 | public function testOAuthRequestBadResponse() |
58 | { | 51 | { |
59 | $client = new Client(); | 52 | $httpMockClient = new HttpMockClient(); |
60 | 53 | $httpMockClient->addResponse(new Response(403)); | |
61 | $mock = new Mock([ | ||
62 | new Response(403), | ||
63 | ]); | ||
64 | |||
65 | $client->getEmitter()->attach($mock); | ||
66 | 54 | ||
67 | $pocketImport = $this->getPocketImport(); | 55 | $pocketImport = $this->getPocketImport(); |
68 | $pocketImport->setClient($client); | 56 | $pocketImport->setClient($httpMockClient); |
69 | 57 | ||
70 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); | 58 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); |
71 | 59 | ||
@@ -78,16 +66,11 @@ class PocketImportTest extends TestCase | |||
78 | 66 | ||
79 | public function testOAuthAuthorize() | 67 | public function testOAuthAuthorize() |
80 | { | 68 | { |
81 | $client = new Client(); | 69 | $httpMockClient = new HttpMockClient(); |
82 | 70 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'wunderbar_token']))); | |
83 | $mock = new Mock([ | ||
84 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
85 | ]); | ||
86 | |||
87 | $client->getEmitter()->attach($mock); | ||
88 | 71 | ||
89 | $pocketImport = $this->getPocketImport(); | 72 | $pocketImport = $this->getPocketImport(); |
90 | $pocketImport->setClient($client); | 73 | $pocketImport->setClient($httpMockClient); |
91 | 74 | ||
92 | $res = $pocketImport->authorize('wunderbar_code'); | 75 | $res = $pocketImport->authorize('wunderbar_code'); |
93 | 76 | ||
@@ -97,16 +80,11 @@ class PocketImportTest extends TestCase | |||
97 | 80 | ||
98 | public function testOAuthAuthorizeBadResponse() | 81 | public function testOAuthAuthorizeBadResponse() |
99 | { | 82 | { |
100 | $client = new Client(); | 83 | $httpMockClient = new HttpMockClient(); |
101 | 84 | $httpMockClient->addResponse(new Response(403)); | |
102 | $mock = new Mock([ | ||
103 | new Response(403), | ||
104 | ]); | ||
105 | |||
106 | $client->getEmitter()->attach($mock); | ||
107 | 85 | ||
108 | $pocketImport = $this->getPocketImport(); | 86 | $pocketImport = $this->getPocketImport(); |
109 | $pocketImport->setClient($client); | 87 | $pocketImport->setClient($httpMockClient); |
110 | 88 | ||
111 | $res = $pocketImport->authorize('wunderbar_code'); | 89 | $res = $pocketImport->authorize('wunderbar_code'); |
112 | 90 | ||
@@ -122,94 +100,90 @@ class PocketImportTest extends TestCase | |||
122 | */ | 100 | */ |
123 | public function testImport() | 101 | public function testImport() |
124 | { | 102 | { |
125 | $client = new Client(); | 103 | $httpMockClient = new HttpMockClient(); |
126 | 104 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'wunderbar_token']))); | |
127 | $mock = new Mock([ | 105 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' |
128 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | 106 | { |
129 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | 107 | "status": 1, |
130 | { | 108 | "list": { |
131 | "status": 1, | 109 | "229279689": { |
132 | "list": { | 110 | "item_id": "229279689", |
133 | "229279689": { | 111 | "resolved_id": "229279689", |
134 | "item_id": "229279689", | 112 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", |
135 | "resolved_id": "229279689", | 113 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
136 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 114 | "favorite": "1", |
137 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 115 | "status": "1", |
138 | "favorite": "1", | 116 | "time_added": "1473020899", |
139 | "status": "1", | 117 | "time_updated": "1473020899", |
140 | "time_added": "1473020899", | 118 | "time_read": "0", |
141 | "time_updated": "1473020899", | 119 | "time_favorited": "0", |
142 | "time_read": "0", | 120 | "sort_id": 0, |
143 | "time_favorited": "0", | 121 | "resolved_title": "The Massive Ryder Cup Preview", |
144 | "sort_id": 0, | 122 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", |
145 | "resolved_title": "The Massive Ryder Cup Preview", | 123 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
146 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 124 | "is_article": "1", |
147 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | 125 | "is_index": "0", |
148 | "is_article": "1", | 126 | "has_video": "1", |
149 | "is_index": "0", | 127 | "has_image": "1", |
150 | "has_video": "1", | 128 | "word_count": "3197", |
151 | "has_image": "1", | 129 | "images": { |
152 | "word_count": "3197", | 130 | "1": { |
153 | "images": { | 131 | "item_id": "229279689", |
154 | "1": { | 132 | "image_id": "1", |
155 | "item_id": "229279689", | 133 | "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360", |
156 | "image_id": "1", | 134 | "width": "0", |
157 | "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360", | 135 | "height": "0", |
158 | "width": "0", | 136 | "credit": "Jamie Squire/Getty Images", |
159 | "height": "0", | 137 | "caption": "" |
160 | "credit": "Jamie Squire/Getty Images", | ||
161 | "caption": "" | ||
162 | } | ||
163 | }, | ||
164 | "videos": { | ||
165 | "1": { | ||
166 | "item_id": "229279689", | ||
167 | "video_id": "1", | ||
168 | "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0", | ||
169 | "width": "420", | ||
170 | "height": "315", | ||
171 | "type": "1", | ||
172 | "vid": "Er34PbFkVGk" | ||
173 | } | ||
174 | }, | ||
175 | "tags": { | ||
176 | "grantland": { | ||
177 | "item_id": "1147652870", | ||
178 | "tag": "grantland" | ||
179 | }, | ||
180 | "Ryder Cup": { | ||
181 | "item_id": "1147652870", | ||
182 | "tag": "Ryder Cup" | ||
183 | } | ||
184 | } | 138 | } |
185 | }, | 139 | }, |
186 | "229279690": { | 140 | "videos": { |
187 | "item_id": "229279689", | 141 | "1": { |
188 | "resolved_id": "229279689", | 142 | "item_id": "229279689", |
189 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 143 | "video_id": "1", |
190 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 144 | "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0", |
191 | "favorite": "1", | 145 | "width": "420", |
192 | "status": "1", | 146 | "height": "315", |
193 | "time_added": "1473020899", | 147 | "type": "1", |
194 | "time_updated": "1473020899", | 148 | "vid": "Er34PbFkVGk" |
195 | "time_read": "0", | 149 | } |
196 | "time_favorited": "0", | 150 | }, |
197 | "sort_id": 1, | 151 | "tags": { |
198 | "resolved_title": "The Massive Ryder Cup Preview", | 152 | "grantland": { |
199 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 153 | "item_id": "1147652870", |
200 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | 154 | "tag": "grantland" |
201 | "is_article": "1", | 155 | }, |
202 | "is_index": "0", | 156 | "Ryder Cup": { |
203 | "has_video": "0", | 157 | "item_id": "1147652870", |
204 | "has_image": "0", | 158 | "tag": "Ryder Cup" |
205 | "word_count": "3197" | 159 | } |
206 | } | 160 | } |
161 | }, | ||
162 | "229279690": { | ||
163 | "item_id": "229279689", | ||
164 | "resolved_id": "229279689", | ||
165 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
166 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
167 | "favorite": "1", | ||
168 | "status": "1", | ||
169 | "time_added": "1473020899", | ||
170 | "time_updated": "1473020899", | ||
171 | "time_read": "0", | ||
172 | "time_favorited": "0", | ||
173 | "sort_id": 1, | ||
174 | "resolved_title": "The Massive Ryder Cup Preview", | ||
175 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
176 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | ||
177 | "is_article": "1", | ||
178 | "is_index": "0", | ||
179 | "has_video": "0", | ||
180 | "has_image": "0", | ||
181 | "word_count": "3197" | ||
207 | } | 182 | } |
208 | } | 183 | } |
209 | ')), | 184 | } |
210 | ]); | 185 | JSON |
211 | 186 | )); | |
212 | $client->getEmitter()->attach($mock); | ||
213 | 187 | ||
214 | $pocketImport = $this->getPocketImport('ConsumerKey', 1); | 188 | $pocketImport = $this->getPocketImport('ConsumerKey', 1); |
215 | 189 | ||
@@ -240,7 +214,7 @@ class PocketImportTest extends TestCase | |||
240 | ->method('updateEntry') | 214 | ->method('updateEntry') |
241 | ->willReturn($entry); | 215 | ->willReturn($entry); |
242 | 216 | ||
243 | $pocketImport->setClient($client); | 217 | $pocketImport->setClient($httpMockClient); |
244 | $pocketImport->authorize('wunderbar_code'); | 218 | $pocketImport->authorize('wunderbar_code'); |
245 | 219 | ||
246 | $res = $pocketImport->import(); | 220 | $res = $pocketImport->import(); |
@@ -254,56 +228,52 @@ class PocketImportTest extends TestCase | |||
254 | */ | 228 | */ |
255 | public function testImportAndMarkAllAsRead() | 229 | public function testImportAndMarkAllAsRead() |
256 | { | 230 | { |
257 | $client = new Client(); | 231 | $httpMockClient = new HttpMockClient(); |
258 | 232 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'wunderbar_token']))); | |
259 | $mock = new Mock([ | 233 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' |
260 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | 234 | { |
261 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | 235 | "status": 1, |
262 | { | 236 | "list": { |
263 | "status": 1, | 237 | "229279689": { |
264 | "list": { | 238 | "item_id": "229279689", |
265 | "229279689": { | 239 | "resolved_id": "229279689", |
266 | "item_id": "229279689", | 240 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", |
267 | "resolved_id": "229279689", | 241 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
268 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 242 | "favorite": "1", |
269 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 243 | "status": "1", |
270 | "favorite": "1", | 244 | "time_added": "1473020899", |
271 | "status": "1", | 245 | "time_updated": "1473020899", |
272 | "time_added": "1473020899", | 246 | "time_read": "0", |
273 | "time_updated": "1473020899", | 247 | "time_favorited": "0", |
274 | "time_read": "0", | 248 | "sort_id": 0, |
275 | "time_favorited": "0", | 249 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
276 | "sort_id": 0, | 250 | "is_article": "1", |
277 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | 251 | "has_video": "1", |
278 | "is_article": "1", | 252 | "has_image": "1", |
279 | "has_video": "1", | 253 | "word_count": "3197" |
280 | "has_image": "1", | 254 | }, |
281 | "word_count": "3197" | 255 | "229279690": { |
282 | }, | 256 | "item_id": "229279689", |
283 | "229279690": { | 257 | "resolved_id": "229279689", |
284 | "item_id": "229279689", | 258 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2", |
285 | "resolved_id": "229279689", | 259 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
286 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2", | 260 | "favorite": "1", |
287 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 261 | "status": "0", |
288 | "favorite": "1", | 262 | "time_added": "1473020899", |
289 | "status": "0", | 263 | "time_updated": "1473020899", |
290 | "time_added": "1473020899", | 264 | "time_read": "0", |
291 | "time_updated": "1473020899", | 265 | "time_favorited": "0", |
292 | "time_read": "0", | 266 | "sort_id": 1, |
293 | "time_favorited": "0", | 267 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
294 | "sort_id": 1, | 268 | "is_article": "1", |
295 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | 269 | "has_video": "0", |
296 | "is_article": "1", | 270 | "has_image": "0", |
297 | "has_video": "0", | 271 | "word_count": "3197" |
298 | "has_image": "0", | ||
299 | "word_count": "3197" | ||
300 | } | ||
301 | } | 272 | } |
302 | } | 273 | } |
303 | ')), | 274 | } |
304 | ]); | 275 | JSON |
305 | 276 | )); | |
306 | $client->getEmitter()->attach($mock); | ||
307 | 277 | ||
308 | $pocketImport = $this->getPocketImport('ConsumerKey', 2); | 278 | $pocketImport = $this->getPocketImport('ConsumerKey', 2); |
309 | 279 | ||
@@ -335,7 +305,7 @@ class PocketImportTest extends TestCase | |||
335 | ->method('updateEntry') | 305 | ->method('updateEntry') |
336 | ->willReturn($entry); | 306 | ->willReturn($entry); |
337 | 307 | ||
338 | $pocketImport->setClient($client); | 308 | $pocketImport->setClient($httpMockClient); |
339 | $pocketImport->authorize('wunderbar_code'); | 309 | $pocketImport->authorize('wunderbar_code'); |
340 | 310 | ||
341 | $res = $pocketImport->setMarkAsRead(true)->import(); | 311 | $res = $pocketImport->setMarkAsRead(true)->import(); |
@@ -349,7 +319,7 @@ class PocketImportTest extends TestCase | |||
349 | */ | 319 | */ |
350 | public function testImportWithRabbit() | 320 | public function testImportWithRabbit() |
351 | { | 321 | { |
352 | $client = new Client(); | 322 | $httpMockClient = new HttpMockClient(); |
353 | 323 | ||
354 | $body = <<<'JSON' | 324 | $body = <<<'JSON' |
355 | { | 325 | { |
@@ -374,19 +344,16 @@ class PocketImportTest extends TestCase | |||
374 | } | 344 | } |
375 | JSON; | 345 | JSON; |
376 | 346 | ||
377 | $mock = new Mock([ | 347 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'wunderbar_token']))); |
378 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | 348 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], <<<JSON |
379 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | 349 | { |
380 | { | 350 | "status": 1, |
381 | "status": 1, | 351 | "list": { |
382 | "list": { | 352 | "229279690": $body |
383 | "229279690": ' . $body . ' | ||
384 | } | ||
385 | } | 353 | } |
386 | ')), | 354 | } |
387 | ]); | 355 | JSON |
388 | 356 | )); | |
389 | $client->getEmitter()->attach($mock); | ||
390 | 357 | ||
391 | $pocketImport = $this->getPocketImport(); | 358 | $pocketImport = $this->getPocketImport(); |
392 | 359 | ||
@@ -420,7 +387,7 @@ JSON; | |||
420 | ->method('publish') | 387 | ->method('publish') |
421 | ->with(json_encode($bodyAsArray)); | 388 | ->with(json_encode($bodyAsArray)); |
422 | 389 | ||
423 | $pocketImport->setClient($client); | 390 | $pocketImport->setClient($httpMockClient); |
424 | $pocketImport->setProducer($producer); | 391 | $pocketImport->setProducer($producer); |
425 | $pocketImport->authorize('wunderbar_code'); | 392 | $pocketImport->authorize('wunderbar_code'); |
426 | 393 | ||
@@ -435,7 +402,7 @@ JSON; | |||
435 | */ | 402 | */ |
436 | public function testImportWithRedis() | 403 | public function testImportWithRedis() |
437 | { | 404 | { |
438 | $client = new Client(); | 405 | $httpMockClient = new HttpMockClient(); |
439 | 406 | ||
440 | $body = <<<'JSON' | 407 | $body = <<<'JSON' |
441 | { | 408 | { |
@@ -460,19 +427,16 @@ JSON; | |||
460 | } | 427 | } |
461 | JSON; | 428 | JSON; |
462 | 429 | ||
463 | $mock = new Mock([ | 430 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'wunderbar_token']))); |
464 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | 431 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], <<<JSON |
465 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | 432 | { |
466 | { | 433 | "status": 1, |
467 | "status": 1, | 434 | "list": { |
468 | "list": { | 435 | "229279690": $body |
469 | "229279690": ' . $body . ' | ||
470 | } | ||
471 | } | 436 | } |
472 | ')), | 437 | } |
473 | ]); | 438 | JSON |
474 | 439 | )); | |
475 | $client->getEmitter()->attach($mock); | ||
476 | 440 | ||
477 | $pocketImport = $this->getPocketImport(); | 441 | $pocketImport = $this->getPocketImport(); |
478 | 442 | ||
@@ -499,7 +463,7 @@ JSON; | |||
499 | $queue = new RedisQueue($redisMock, 'pocket'); | 463 | $queue = new RedisQueue($redisMock, 'pocket'); |
500 | $producer = new Producer($queue); | 464 | $producer = new Producer($queue); |
501 | 465 | ||
502 | $pocketImport->setClient($client); | 466 | $pocketImport->setClient($httpMockClient); |
503 | $pocketImport->setProducer($producer); | 467 | $pocketImport->setProducer($producer); |
504 | $pocketImport->authorize('wunderbar_code'); | 468 | $pocketImport->authorize('wunderbar_code'); |
505 | 469 | ||
@@ -513,17 +477,13 @@ JSON; | |||
513 | 477 | ||
514 | public function testImportBadResponse() | 478 | public function testImportBadResponse() |
515 | { | 479 | { |
516 | $client = new Client(); | 480 | $httpMockClient = new HttpMockClient(); |
517 | 481 | ||
518 | $mock = new Mock([ | 482 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'wunderbar_token']))); |
519 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | 483 | $httpMockClient->addResponse(new Response(403)); |
520 | new Response(403), | ||
521 | ]); | ||
522 | |||
523 | $client->getEmitter()->attach($mock); | ||
524 | 484 | ||
525 | $pocketImport = $this->getPocketImport(); | 485 | $pocketImport = $this->getPocketImport(); |
526 | $pocketImport->setClient($client); | 486 | $pocketImport->setClient($httpMockClient); |
527 | $pocketImport->authorize('wunderbar_code'); | 487 | $pocketImport->authorize('wunderbar_code'); |
528 | 488 | ||
529 | $res = $pocketImport->import(); | 489 | $res = $pocketImport->import(); |
@@ -537,25 +497,23 @@ JSON; | |||
537 | 497 | ||
538 | public function testImportWithExceptionFromGraby() | 498 | public function testImportWithExceptionFromGraby() |
539 | { | 499 | { |
540 | $client = new Client(); | 500 | $httpMockClient = new HttpMockClient(); |
541 | 501 | ||
542 | $mock = new Mock([ | 502 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'wunderbar_token']))); |
543 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | 503 | $httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' |
544 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | 504 | { |
545 | { | 505 | "status": 1, |
546 | "status": 1, | 506 | "list": { |
547 | "list": { | 507 | "229279689": { |
548 | "229279689": { | 508 | "status": "1", |
549 | "status": "1", | 509 | "favorite": "1", |
550 | "favorite": "1", | 510 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" |
551 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" | ||
552 | } | ||
553 | } | 511 | } |
554 | } | 512 | } |
555 | ')), | 513 | } |
556 | ]); | 514 | |
557 | 515 | JSON | |
558 | $client->getEmitter()->attach($mock); | 516 | )); |
559 | 517 | ||
560 | $pocketImport = $this->getPocketImport('ConsumerKey', 1); | 518 | $pocketImport = $this->getPocketImport('ConsumerKey', 1); |
561 | 519 | ||
@@ -579,7 +537,7 @@ JSON; | |||
579 | ->method('updateEntry') | 537 | ->method('updateEntry') |
580 | ->will($this->throwException(new \Exception())); | 538 | ->will($this->throwException(new \Exception())); |
581 | 539 | ||
582 | $pocketImport->setClient($client); | 540 | $pocketImport->setClient($httpMockClient); |
583 | $pocketImport->authorize('wunderbar_code'); | 541 | $pocketImport->authorize('wunderbar_code'); |
584 | 542 | ||
585 | $res = $pocketImport->import(); | 543 | $res = $pocketImport->import(); |