diff options
Diffstat (limited to 'tests/Wallabag')
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | 123 | ||||
-rw-r--r-- | tests/Wallabag/CoreBundle/fixtures/unnamed.png | bin | 0 -> 3688 bytes |
2 files changed, 123 insertions, 0 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php new file mode 100644 index 00000000..0273693e --- /dev/null +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | |||
@@ -0,0 +1,123 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Wallabag\CoreBundle\Helper\DownloadImages; | ||
6 | use Psr\Log\NullLogger; | ||
7 | use Monolog\Logger; | ||
8 | use Monolog\Handler\TestHandler; | ||
9 | use GuzzleHttp\Client; | ||
10 | use GuzzleHttp\Subscriber\Mock; | ||
11 | use GuzzleHttp\Message\Response; | ||
12 | use GuzzleHttp\Stream\Stream; | ||
13 | |||
14 | class DownloadImagesTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | public function testProcessHtml() | ||
17 | { | ||
18 | $client = new Client(); | ||
19 | |||
20 | $mock = new Mock([ | ||
21 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | ||
22 | ]); | ||
23 | |||
24 | $client->getEmitter()->attach($mock); | ||
25 | |||
26 | $logHandler = new TestHandler(); | ||
27 | $logger = new Logger('test', array($logHandler)); | ||
28 | |||
29 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger); | ||
30 | $res = $download->processHtml('<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); | ||
31 | |||
32 | $this->assertContains('/assets/images/4/2/4258f71e/c638b4c2.png', $res); | ||
33 | } | ||
34 | |||
35 | public function testProcessHtmlWithBadImage() | ||
36 | { | ||
37 | $client = new Client(); | ||
38 | |||
39 | $mock = new Mock([ | ||
40 | new Response(200, ['content-type' => 'application/json'], Stream::factory('')), | ||
41 | ]); | ||
42 | |||
43 | $client->getEmitter()->attach($mock); | ||
44 | |||
45 | $logHandler = new TestHandler(); | ||
46 | $logger = new Logger('test', array($logHandler)); | ||
47 | |||
48 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger); | ||
49 | $res = $download->processHtml('<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); | ||
50 | |||
51 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); | ||
52 | } | ||
53 | |||
54 | public function singleImage() | ||
55 | { | ||
56 | return [ | ||
57 | ['image/pjpeg', 'jpeg'], | ||
58 | ['image/jpeg', 'jpeg'], | ||
59 | ['image/png', 'png'], | ||
60 | ['image/gif', 'gif'], | ||
61 | ]; | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * @dataProvider singleImage | ||
66 | */ | ||
67 | public function testProcessSingleImage($header, $extension) | ||
68 | { | ||
69 | $client = new Client(); | ||
70 | |||
71 | $mock = new Mock([ | ||
72 | new Response(200, ['content-type' => $header], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | ||
73 | ]); | ||
74 | |||
75 | $client->getEmitter()->attach($mock); | ||
76 | |||
77 | $logHandler = new TestHandler(); | ||
78 | $logger = new Logger('test', array($logHandler)); | ||
79 | |||
80 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger); | ||
81 | $res = $download->processSingleImage('T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | ||
82 | |||
83 | $this->assertContains('/assets/images/4/2/4258f71e/ebe60399.'.$extension, $res); | ||
84 | } | ||
85 | |||
86 | public function testProcessSingleImageWithBadImage() | ||
87 | { | ||
88 | $client = new Client(); | ||
89 | |||
90 | $mock = new Mock([ | ||
91 | new Response(200, ['content-type' => 'image/png'], Stream::factory('')), | ||
92 | ]); | ||
93 | |||
94 | $client->getEmitter()->attach($mock); | ||
95 | |||
96 | $logHandler = new TestHandler(); | ||
97 | $logger = new Logger('test', array($logHandler)); | ||
98 | |||
99 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger); | ||
100 | $res = $download->processSingleImage('http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | ||
101 | |||
102 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); | ||
103 | } | ||
104 | |||
105 | public function testProcessSingleImageFailAbsolute() | ||
106 | { | ||
107 | $client = new Client(); | ||
108 | |||
109 | $mock = new Mock([ | ||
110 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | ||
111 | ]); | ||
112 | |||
113 | $client->getEmitter()->attach($mock); | ||
114 | |||
115 | $logHandler = new TestHandler(); | ||
116 | $logger = new Logger('test', array($logHandler)); | ||
117 | |||
118 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger); | ||
119 | $res = $download->processSingleImage('/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY'); | ||
120 | |||
121 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); | ||
122 | } | ||
123 | } | ||
diff --git a/tests/Wallabag/CoreBundle/fixtures/unnamed.png b/tests/Wallabag/CoreBundle/fixtures/unnamed.png new file mode 100644 index 00000000..e6dd9caa --- /dev/null +++ b/tests/Wallabag/CoreBundle/fixtures/unnamed.png | |||
Binary files differ | |||