]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/ImagesTest.php
spelling: cached
[friendica.git] / tests / src / Util / ImagesTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util;
23
24 use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
25 use Friendica\Test\DiceHttpMockHandlerTrait;
26 use Friendica\Test\MockedTest;
27 use Friendica\Util\Images;
28 use GuzzleHttp\Handler\MockHandler;
29 use GuzzleHttp\Psr7\Response;
30
31 class ImagesTest extends MockedTest
32 {
33         use DiceHttpMockHandlerTrait;
34         use ArraySubsetAsserts;
35
36         protected function setUp(): void
37         {
38                 parent::setUp();
39
40                 $this->setupHttpMockHandler();
41         }
42
43         protected function tearDown(): void
44         {
45                 $this->tearDownFixtures();
46
47                 parent::tearDown();
48         }
49
50         public function dataImages()
51         {
52                 return [
53                         'image' => [
54                                 'url'     => 'https://pbs.twimg.com/profile_images/2365515285/9re7kx4xmc0eu9ppmado.png',
55                                 'headers' => [
56                                         'Server'                        => 'tsa_b',
57                                         'Content-Type'                  => 'image/png',
58                                         'Cache-Control'                 => 'max-age=604800,must-revalidate',
59                                         'Last-Modified'                 => 'Thu,04Nov201001:42:54GMT',
60                                         'Content-Length'                => '24875',
61                                         'Access-Control-Allow-Origin'   => '*',
62                                         'Access-Control-Expose-Headers' => 'Content-Length',
63                                         'Date'                          => 'Mon,23Aug202112:39:00GMT',
64                                         'Connection'                    => 'keep-alive',
65                                 ],
66                                 'data'      => file_get_contents(__DIR__ . '/../../datasets/curl/image.content'),
67                                 'assertion' => [
68                                         '0'    => '400',
69                                         '1'    => '400',
70                                         '2'    => '3',
71                                         '3'    => 'width="400" height="400"',
72                                         'bits' => '8',
73                                         'mime' => 'image/png',
74                                         'size' => '24875',
75                                 ]
76                         ],
77                         'emptyUrl' => [
78                                 'url'       => '',
79                                 'headers'   => [],
80                                 'data'      => '',
81                                 'assertion' => [],
82                         ],
83                 ];
84         }
85
86         /**
87          * Test the Images::getInfoFromURL() method (only remote images, not local/relative!)
88          *
89          * @dataProvider dataImages
90          */
91         public function testGetInfoFromRemotURL(string $url, array $headers, string $data, array $assertion)
92         {
93                 $this->httpRequestHandler->setHandler(new MockHandler([
94                         new Response(200, $headers, $data),
95                 ]));
96
97                 self::assertArraySubset($assertion, Images::getInfoFromURL($url));
98         }
99 }