diff options
author | adev <adev2000@gmail.com> | 2017-10-24 22:55:40 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2019-05-28 11:40:41 +0200 |
commit | bf9ace0643f654e7ccd9c020b8b501ad56cd19de (patch) | |
tree | 4ff47ca915eb326307127b1b4caf69beda4036e3 /tests/Wallabag/CoreBundle | |
parent | 92a66835624acf6fd14f5adc5f8aab399658592e (diff) | |
download | wallabag-bf9ace0643f654e7ccd9c020b8b501ad56cd19de.tar.gz wallabag-bf9ace0643f654e7ccd9c020b8b501ad56cd19de.tar.zst wallabag-bf9ace0643f654e7ccd9c020b8b501ad56cd19de.zip |
Use httplug
Diffstat (limited to 'tests/Wallabag/CoreBundle')
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | 79 |
1 files changed, 23 insertions, 56 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php index cda5f843..53758364 100644 --- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | |||
@@ -3,9 +3,10 @@ | |||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use GuzzleHttp\Client; | 5 | use GuzzleHttp\Client; |
6 | use GuzzleHttp\Message\Response; | ||
7 | use GuzzleHttp\Stream\Stream; | 6 | use GuzzleHttp\Stream\Stream; |
8 | use GuzzleHttp\Subscriber\Mock; | 7 | use GuzzleHttp\Subscriber\Mock; |
8 | use Http\Mock\Client as HttpMockClient; | ||
9 | use GuzzleHttp\Psr7\Response; | ||
9 | use Monolog\Handler\TestHandler; | 10 | use Monolog\Handler\TestHandler; |
10 | use Monolog\Logger; | 11 | use Monolog\Logger; |
11 | use PHPUnit\Framework\TestCase; | 12 | use PHPUnit\Framework\TestCase; |
@@ -32,18 +33,14 @@ class DownloadImagesTest extends TestCase | |||
32 | */ | 33 | */ |
33 | public function testProcessHtml($html, $url) | 34 | public function testProcessHtml($html, $url) |
34 | { | 35 | { |
35 | $client = new Client(); | 36 | $httpMockClient = new HttpMockClient(); |
36 | 37 | ||
37 | $mock = new Mock([ | 38 | $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 | 39 | ||
43 | $logHandler = new TestHandler(); | 40 | $logHandler = new TestHandler(); |
44 | $logger = new Logger('test', [$logHandler]); | 41 | $logger = new Logger('test', [$logHandler]); |
45 | 42 | ||
46 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 43 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
47 | 44 | ||
48 | $res = $download->processHtml(123, $html, $url); | 45 | $res = $download->processHtml(123, $html, $url); |
49 | 46 | ||
@@ -53,18 +50,13 @@ class DownloadImagesTest extends TestCase | |||
53 | 50 | ||
54 | public function testProcessHtmlWithBadImage() | 51 | public function testProcessHtmlWithBadImage() |
55 | { | 52 | { |
56 | $client = new Client(); | 53 | $httpMockClient = new HttpMockClient(); |
57 | 54 | $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 | 55 | ||
64 | $logHandler = new TestHandler(); | 56 | $logHandler = new TestHandler(); |
65 | $logger = new Logger('test', [$logHandler]); | 57 | $logger = new Logger('test', [$logHandler]); |
66 | 58 | ||
67 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 59 | $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'); | 60 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); |
69 | 61 | ||
70 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); | 62 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); |
@@ -85,18 +77,13 @@ class DownloadImagesTest extends TestCase | |||
85 | */ | 77 | */ |
86 | public function testProcessSingleImage($header, $extension) | 78 | public function testProcessSingleImage($header, $extension) |
87 | { | 79 | { |
88 | $client = new Client(); | 80 | $httpMockClient = new HttpMockClient(); |
89 | 81 | $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 | 82 | ||
96 | $logHandler = new TestHandler(); | 83 | $logHandler = new TestHandler(); |
97 | $logger = new Logger('test', [$logHandler]); | 84 | $logger = new Logger('test', [$logHandler]); |
98 | 85 | ||
99 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 86 | $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'); | 87 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
101 | 88 | ||
102 | $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res); | 89 | $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res); |
@@ -104,18 +91,13 @@ class DownloadImagesTest extends TestCase | |||
104 | 91 | ||
105 | public function testProcessSingleImageWithBadUrl() | 92 | public function testProcessSingleImageWithBadUrl() |
106 | { | 93 | { |
107 | $client = new Client(); | 94 | $httpMockClient = new HttpMockClient(); |
108 | 95 | $httpMockClient->addResponse(new Response(404, [])); | |
109 | $mock = new Mock([ | ||
110 | new Response(404, []), | ||
111 | ]); | ||
112 | |||
113 | $client->getEmitter()->attach($mock); | ||
114 | 96 | ||
115 | $logHandler = new TestHandler(); | 97 | $logHandler = new TestHandler(); |
116 | $logger = new Logger('test', [$logHandler]); | 98 | $logger = new Logger('test', [$logHandler]); |
117 | 99 | ||
118 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 100 | $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'); | 101 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
120 | 102 | ||
121 | $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); | 103 | $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); |
@@ -123,18 +105,13 @@ class DownloadImagesTest extends TestCase | |||
123 | 105 | ||
124 | public function testProcessSingleImageWithBadImage() | 106 | public function testProcessSingleImageWithBadImage() |
125 | { | 107 | { |
126 | $client = new Client(); | 108 | $httpMockClient = new HttpMockClient(); |
127 | 109 | $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 | 110 | ||
134 | $logHandler = new TestHandler(); | 111 | $logHandler = new TestHandler(); |
135 | $logger = new Logger('test', [$logHandler]); | 112 | $logger = new Logger('test', [$logHandler]); |
136 | 113 | ||
137 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 114 | $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'); | 115 | $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
139 | 116 | ||
140 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); | 117 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); |
@@ -142,18 +119,13 @@ class DownloadImagesTest extends TestCase | |||
142 | 119 | ||
143 | public function testProcessSingleImageFailAbsolute() | 120 | public function testProcessSingleImageFailAbsolute() |
144 | { | 121 | { |
145 | $client = new Client(); | 122 | $httpMockClient = new HttpMockClient(); |
146 | 123 | $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 | 124 | ||
153 | $logHandler = new TestHandler(); | 125 | $logHandler = new TestHandler(); |
154 | $logger = new Logger('test', [$logHandler]); | 126 | $logger = new Logger('test', [$logHandler]); |
155 | 127 | ||
156 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 128 | $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'); | 129 | $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY'); |
158 | 130 | ||
159 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); | 131 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); |
@@ -161,18 +133,13 @@ class DownloadImagesTest extends TestCase | |||
161 | 133 | ||
162 | public function testProcessRealImage() | 134 | public function testProcessRealImage() |
163 | { | 135 | { |
164 | $client = new Client(); | 136 | $httpMockClient = new HttpMockClient(); |
165 | 137 | $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 | 138 | ||
172 | $logHandler = new TestHandler(); | 139 | $logHandler = new TestHandler(); |
173 | $logger = new Logger('test', [$logHandler]); | 140 | $logger = new Logger('test', [$logHandler]); |
174 | 141 | ||
175 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | 142 | $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
176 | 143 | ||
177 | $res = $download->processSingleImage( | 144 | $res = $download->processSingleImage( |
178 | 123, | 145 | 123, |